python pycord -对象没有属性'children'

fcg9iug3  于 2022-12-21  发布在  Python
关注(0)|答案(1)|浏览(113)

我正在尝试使用pycord创建一个discord bot。我有一个视图类如下:

class MyView(discord.ui.View):
    def __init__(self, link):
        self.link = link

    @discord.ui.button(label='480p', style=discord.ButtonStyle.gray, emoji='🎥')
    async def button_callback_480(self, button, interaction):
        button.disable = True
        await MyView.video_finder()

        await interaction.response.send_message('downloading the file. Check the server for file')
        await interaction.followup.send('follow up added to the queue')

    @discord.ui.button(label='720p', style=discord.ButtonStyle.danger, emoji='🎥')
    async def button_callback_720(self, button, interaction):
        button.disable = True
        await interaction.response.send_message('downloading the file. Check the server for file')
        await interaction.followup.send('follow up added to the queue')

使用上面的类,我创建了一个斜杠命令。斜杠命令的代码如下:

@bot.command(description="Choose the quality of video you want to download")
async def ytdl(ctx, link: str):

    view = MyView(link)
    await ctx.respond('Choose the quality of video you want to download', view = view)

如果我将link作为参数传递给类,则会得到以下错误:我也没有得到按钮渲染太多。

Ignoring exception in command ytdl:
Traceback (most recent call last):
  File "/home/siddhant/dev/rick/theHomieRick/venv/lib/python3.10/site-packages/discord/commands/core.py", line 124, in wrapped
    ret = await coro(arg)
  File "/home/siddhant/dev/rick/theHomieRick/venv/lib/python3.10/site-packages/discord/commands/core.py", line 980, in _invoke
    await self.callback(ctx, **kwargs)
  File "/home/siddhant/dev/rick/theHomieRick/rick/commands.py", line 42, in ytdl
    await ctx.respond('Choose the quality of video you want to download', view = view)
  File "/home/siddhant/dev/rick/theHomieRick/venv/lib/python3.10/site-packages/discord/commands/context.py", line 282, in respond
    return await self.interaction.response.send_message(
  File "/home/siddhant/dev/rick/theHomieRick/venv/lib/python3.10/site-packages/discord/interactions.py", line 789, in send_message
    payload["components"] = view.to_components()
  File "/home/siddhant/dev/rick/theHomieRick/venv/lib/python3.10/site-packages/discord/ui/view.py", line 215, in to_components
    children = sorted(self.children, key=key)
AttributeError: 'MyView' object has no attribute 'children'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/siddhant/dev/rick/theHomieRick/venv/lib/python3.10/site-packages/discord/bot.py", line 1114, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "/home/siddhant/dev/rick/theHomieRick/venv/lib/python3.10/site-packages/discord/commands/core.py", line 375, in invoke
    await injected(ctx)
  File "/home/siddhant/dev/rick/theHomieRick/venv/lib/python3.10/site-packages/discord/commands/core.py", line 132, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: 'MyView' object has no attribute 'children'

如果我不通过论证“链接”,它的工作完美。

wvt8vs2t

wvt8vs2t1#

self.link = link之前由super().__init__()解决从其继承属性的类未初始化,因为当前类中的__init__()正在覆盖discord.ui.View类的__init__()

相关问题