You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人报错:to_components()缺少必填参数'self'

问题描述

尝试在Embed下方添加标签为“Open Channel”、样式为primary的按钮时触发错误,仅发送Embed时无问题。

自定义View类代码:

class NegotiationButton(discord.ui.View):

    @discord.ui.button(label="Open Channel", style=discord.ButtonStyle.primary)
    async def negotiate(self, interaction: discord.Interaction, button: discord.ui.Button):
        button.disabled=True
        button.style=discord.ButtonStyle.danger
        button.label="Already Taken"

        ntcnameverify=True

        while ntcnameverify:
            newchannelname=f"trade-{random.randint(0,99999)}"
            for channel in mainguild.text_channels:
                if newchannelname==channel.name:
                    continue
                else:
                    ntcnameverify=False

        await mainguild.create_text_channel(
            name=newchannelname,
            overwrites={ #TODO Change these IDs
                mainguild.get_member(702419006010949693): discord.PermissionOverwrite(read_messages=True, send_messages=True)

            },
            category=bot.get_channel(1000099256998297681),
            postition=500
        )

发送消息的代码:

await bot.get_channel(876620293249048666).send(embed=embed, view=NegotiationButton)

触发的错误信息:

Traceback (most recent call last):
  File "C:\Users\User\anaconda3\lib\site-packages\discord\ext\commands\core.py", line 200, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\User\OneDrive\Documents\Python Projects\Other Stuff\uc_systems_bot\bot.py", line 77, in offer
    await bot.get_channel(876620293249048666).send(embed=embed, view=NegotiationButton) #TODO Change channel ID and add logic for buy and sell channels
  File "C:\Users\User\anaconda3\lib\site-packages\discord\abc.py", line 1521, in send
    with handle_message_parameters(
  File "C:\Users\User\anaconda3\lib\site-packages\discord\http.py", line 188, in handle_message_parameters
    payload['components'] = view.to_components()

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

Traceback (most recent call last):
  File "C:\Users\User\anaconda3\lib\site-packages\discord\ext\commands\bot.py", line 1330, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\User\anaconda3\lib\site-packages\discord\ext\commands\core.py", line 995, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
  File "C:\Users\User\anaconda3\lib\site-packages\discord\ext\commands\core.py", line 209, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: to_components() missing 1 required positional argument: 'self'
解决方案

错误核心原因是传入了View类本身,而非类的实例对象。discord.ui.View是类,调用send方法时必须传入该类的实例,不能直接传类名。

修改发送消息的代码,将NegotiationButton改为NegotiationButton():

await bot.get_channel(876620293249048666).send(embed=embed, view=NegotiationButton())

另外,频道创建代码里存在拼写错误:postition应改为position,建议一并修正:

await mainguild.create_text_channel(
    name=newchannelname,
    overwrites={ #TODO Change these IDs
        mainguild.get_member(702419006010949693): discord.PermissionOverwrite(read_messages=True, send_messages=True)

    },
    category=bot.get_channel(1000099256998297681),
    position=500  # 修正拼写错误
)

内容的提问来源于stack exchange,提问作者undonepotato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 22:39:19