简体   繁体   English

未找到 Python discord 应用程序命令

[英]Python discord application command not found

I registered an application command.我注册了一个应用程序命令。 It shows up in the discord server as so.它显示在 discord 服务器中。

Registered App Command Image注册的应用命令图像

I try to set up the command like this.我尝试像这样设置命令。 Note: I have also tried @bot.command(name='huncho')注意:我也试过@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.这是您使用 discord 应用程序注册应用程序命令的方法。

The application command is found in a CommandTree .应用程序命令位于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) .初始化 CommandTree 后,您可以使用async def huncho(interaction)创建@tree_cls.command(name='huncho') ) 。 Here is a reference to the Interaction class.这是对交互class 的参考。 When you use this, you get the options that you initialized your application command with interaction.data .当您使用它时,您将获得使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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