Reputation: 1
I would like to make a test button but it writes me an error shows which I did not find anywhere on the internet
code:
@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!!')
error:
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'
Upvotes: 0
Views: 2174
Reputation: 11
I don't know if you solved the problem or not. Maybe it will help someone.
This error occurs due to slash commands, or rather local ctx. The problem is solved by creating an async def
function in which messages will be sent. Just do not pass ctx
to this function, pass only the necessary parameters, if you need to send a message, pass ctx.channel
(then use await channel.send()
), if you need a user, then ctx.author
and ect
Corrected code:
@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!!')
Upvotes: 1