属性错误:"按钮"对象没有属性"获取"按钮错误 [discord.py]



我想做一个测试按钮,但它给我写了一个错误显示,我没有在互联网上找到任何地方

代码:

@slash.slash(
name="ButtonTest",
description='Test a button function (this not work)',
guild_ids=[894190915097337876]
)
async def _ButtonTest(ctx: SlashContext):
await ctx.send("test", components = [Button(style=ButtonStyle.blue, label='sus')])
instruction=await bot.wait_for("button_click", check=lambda i: i.component.label.startswith("Click"))
await instruction.send(content='Test button function succeeded!!')

错误:

An exception has occurred while executing command `buttontest`:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/client.py", line 1352, in invoke_command
await func.invoke(ctx, **args)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/model.py", line 210, in invoke
return await self.func(*args, **kwargs)
File "main.py", line 42, in _ButtonTest
await ctx.send("test", components = [Button(style=ButtonStyle.blue, label='sus')])
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/context.py", line 215, in send
if components and not all(comp.get("type") == 1 for comp in components):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/context.py", line 215, in <genexpr>
if components and not all(comp.get("type") == 1 for comp in components):
AttributeError: 'Button' object has no attribute 'get'

我不知道你是否解决了问题。也许它会帮助别人。

这个错误是由斜杠命令引起的,或者更确切地说,是由本地的ctx引起的。通过创建将在其中发送消息的async def函数来解决这个问题。只是不传递ctx到这个函数,只传递必要的参数,如果你需要发送消息,传递ctx.channel(然后使用await channel.send()),如果你需要一个用户,然后ctx.author和ect

纠正代码:

@slash.slash(
name="ButtonTest",
description='Test a button function (this not work)',
guild_ids=[894190915097337876]
)
async def _ButtonTest(ctx: SlashContext):
for_Button_Test(ctx.channel)

async def for_Button_Test(ch):
await ch.send("test", components=[Button(style=ButtonStyle.blue, label='sus')])
instruction = await bot.wait_for("button_click", check=lambda i: i.component.label.startswith("Click"))
await instruction.send(content='Test button function succeeded!!')