找不到Python discord应用程序命令


我注册了一个应用程序命令。它也显示在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')。这里是对Interaction类的引用。使用此选项时,将获得使用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)

相关内容

最新更新