#๐Ÿ”’ Why does it say "app_commands" is not defined?

34 messages ยท Page 1 of 1 (latest)

rare bay
#

I have this command in my discord bot, when I try to run my bot it says app_commands is not defined, am I supposed to define it somehow?

@app_commands.command(name="test-command", description="Test command with 2 choices.")
@app_commands.choices(choice=[
    app_commands.Choice(name='choice1', value=1),
    app_commands.Choice(name='choice2', value=2)
])
async def test_command(interaction: discord.Interaction, choice: str):
    if choice == "choice1":
        embed1 = discord.Embed(title="Choice 1", description="This is the response for choice 1.", color=0x2b2d31)
        await interaction.response.send_message(embed=embed1)
    elif choice == "choice2":
        embed2 = discord.Embed(title="Choice 2", description="This is the response for choice 2.", color=0x2b2d31)
        await interaction.response.send_message(embed=embed2)```
royal widgetBOT
#

@rare bay

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

slim moon
slim moon
#

could you provide the full error?

rare bay
# slim moon could you provide the full error?
31.03 19:11:24 [Bot] Traceback (most recent call last):
31.03 19:11:24 [Bot] File "/main.py", line 2, in <module>
31.03 19:11:24 [Bot] from Bot import bot
31.03 19:11:24 [Bot] File "/Bot.py", line 218, in <module>
31.03 19:11:24 [Bot] @app_commands.command(name="test-command", description="Test command with 2 choices.")
31.03 19:11:24 [Bot] ^^^^^^^^^^^^
31.03 19:11:24 [Bot] NameError: name 'app_commands' is not defined```
slim moon
#

are you importing app_commands correctly?
from discord import app_commands

slim moon
rare bay
#

Does that have smh to do with this error

slim moon
#

I don't use discord.py, so I can't help too much - but try converting your values into strings;

app_commands.Choice(name='choice1', value='1'),
app_commands.Choice(name='choice2', value='2')
#

though, if you want it to stay as an integer value, you should change your choice: str to int

rare bay
#

Alright thanks, that might be helpful to my situation

slim moon
#

lmk if you need anymore help

rare bay
# slim moon lmk if you need anymore help

Not sure if you know what could cause this but I edited the code a bit:

@app_commands.command(name="test-command", description="Test command with 2 choices.")
@app_commands.describe(servers='Game servers to choose.')
@app_commands.choices(servers=[
    Choice(name='choice1', value=1),
    Choice(name='choice2', value=2)
])
async def test_command(interaction: discord.Interaction, Choice: str):
    if choice == "choice1":
        embed1 = discord.Embed(title="Choice 1", description="This is the response for choice 1.", color=0x2b2d31)
        await interaction.response.send_message(embed=embed1)
    elif choice == "choice2":
        embed2 = discord.Embed(title="Choice 2", description="This is the response for choice 2.", color=0x2b2d31)
        await interaction.response.send_message(embed=embed2)```

Now it says: 

31.03 19:37:54 [Bot] TypeError: unknown parameter given: servers```

rare bay
#

I just dont get where it is

slim moon
#

servers: Choice: str this would be your issue

#

as seen in the github gist, you should use something like:
servers: Choice[int] (int because your value is =1 and =2)

then you can access the chosen name by doing servers.name

#

that should work, since the syntax error looks like it is because you have nested the annotation servers: and choice:

rare bay
#

Oh yes that worked, now it didnt give any errors.
But the command doesnt show on my server even tho I always sync them

#

uhmmm

slim moon
#

The slash command doesn't appear?

rare bay
#

nvm I just changed it to @bot.tree.command
now it works fine

Thanks alot for your assistance

slim moon
#

alrighty

#

no problem

rare bay
# slim moon Have a good day

One more question, if your available
How can I see users roles through interaction, I tried this but seems like interaction doesnt have attribute author:

interaction.author.roles```
slim moon
#

Try interaction.user

rare bay
slim moon
#

No problem

royal widgetBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.