繁体   English   中英

未找到 Python discord 应用程序命令

[英]Python discord application command not found

我注册了一个应用程序命令。 它显示在 discord 服务器中。

注册的应用命令图像

我尝试像这样设置命令。 注意:我也试过@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)

我最终得到这个错误。

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

此代码在命令未注册为应用程序命令时有效,但我不确定如何使用已注册的应用程序命令。

我想到了。 网上这样的例子并不多。 我希望这对某人有所帮助。 这是您使用 discord 应用程序注册应用程序命令的方法。

应用程序命令位于CommandTree中。 您创建一个客户端并初始化一个命令树。

初始化 CommandTree 后,您可以使用async def huncho(interaction)创建@tree_cls.command(name='huncho') ) 。 这是对交互class 的参考。 当您使用它时,您将获得使用interaction.data初始化应用程序命令的选项。 这是一个包含每个选项的值和一些元数据的字典。

这是一个例子:

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