简体   繁体   中英

Python discord application command not found

I registered an application command. It shows up in the discord server as so.

Registered App Command Image

I try to set up the command like this. Note: I have also tried @bot.command(name='huncho')

from auth import Auth 
import discord 
from discord.ext import commands


bot = commands.Bot('/', intents=discord.Intents.all())


@bot.command()
async def huncho(ctx, *f):
    print('hello')
    

if __name__=='__main__':
    bot.run(Auth.token)

I end up getting this error.

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.8/site-packages/discord/app_commands/tree.py", line 1089, in wrapper
    await self._call(interaction)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/discord/app_commands/tree.py", line 1219, in _call
    command, options = self._get_app_command_options(data)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/discord/app_commands/tree.py", line 1125, in _get_app_command_options
    raise CommandNotFound(name, parents)
discord.app_commands.errors.CommandNotFound: Application command 'huncho' not found

This code works when the command is not registered as an application command, but I'm not sure how to use a registered application command.

I figured it out. There are not very many examples of this online. I hope this helps somebody. Here is what you use to register an application command with your discord application.

The application command is found in a CommandTree . You create a client and initialize aa command tree.

Once you initialize a CommandTree, you can create @tree_cls.command(name='huncho') with an async def huncho(interaction) . Here is a reference to the Interaction class. When you use this, you get the options that you initialized your application command with interaction.data . This is a dictionary with the values of each option and some metadata.

Here is an example:

from auth import Auth 
import discord 
from discord import app_commands


client = discord.Client(intents=discord.Intents.all())
tree_cls = app_commands.CommandTree(client)


@tree_cls.command(name='huncho')
async def huncho(interaction):
    value = 'Huncho'+' '+interaction.data['options'][0]['value']
    await interaction.response.send_message(value)
    

if __name__=='__main__':
    client.run(Auth.token)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM