我需要帮助判断是否存在Slash命令. 我的目标是添加一个斜杠命令,让你禁用其他斜杠命令.因此,我需要判断一下这个命令是否存在. 这是我目前所拥有的代码:

@tree.command(guild = discord.Object(id=1129737425653071992), name='disable', description='Disable a specified Command')
async def disable(interaction: discord.Interaction, command: str):
    print(tree.get_commands(guild=discord.Object(id=1129737425653071992)))
    if command in tree.get_commands(guild=discord.Object(id=1129737425653071992)):
        await tree.remove_command(command)
        await interaction.response.send_message(f"{command} was disabled. You can reenable it by running /enable {command}")
    else:
        await interaction.response.send_message(f"Command could not be found: {command}")

当命令不存在的时候,他就说了.但当它这样做时,它仍然说它不存在.

我试过,正如你在上面看到的代码,

tree.get_commands(guild=discord.Object(id=1129737425653071992)

打印,就是这样的: <discord.app_commands. commands.命令对象0x000002A6994B22D0>

如果存在斜杠命令,我如何进行工作判断?

推荐答案

第一件小事,tree.remove_command(command)就是not.

无论如何,你真正想做的是禁用命令无论如何,

How to create a disable command in discord.py

判断是否存在

tree.get_commands个退货:

List[Union[ContextMenu, Command, Group]]

这意味着,如果你想具体判断Command,你必须解析它.command参数也是一个字符串,这就是为什么它最初没有返回true的原因.但是,我们可以在命令树中使用一个更简单的命令.

应该怎么做

tree.get_command(单数不复数)返回:

Optional[Union[Command, ContextMenu, Group]]

是的,这是一回事.但是,参数是不同的.

命令(Str)-要获取的根命令的名称.

行会(可选[snowflake])-要获得命令的行会.如果未给出或无,则它将获得一个全局命令.

type(AppCommandType)—要获取的命令类型.切换到chat_input,即斜杠命令.

您可以将之前的command字符串参数传入此命令.然后你可以判断它是否返回任何东西.这比解析快得多(如前所述).您也不会判断是否存在多个命令,因此这是最佳 Select .

下面我有一个代码示例(作为参考,我之前做了另一个命令):

@tree.command(guild=discord.Object(id=guild_id), name='disable', description='Disable a specified Command')
async def disable(interaction: discord.Interaction, command: str):
    if tree.get_command(command, guild=discord.Object(id=guild_id)):
        tree.remove_command(command, guild=discord.Object(id=guild_id))  # remove the command
        await interaction.response.send_message(
            f"{command} was disabled. You can reenable it by running /enable {command}")
    else:
        await interaction.response.send_message(f"Command could not be found: {command}")

Image of it working

禁用命令

但是,这实际上并不会禁用该命令.这是为什么?如果是tree.remove_command,则需要同步树.只需添加一行:

await tree.sync(guild=discord.Object(id=guild_id))

它应该会奏效的!

完整代码:

@tree.command(guild=discord.Object(id=guild_id), name='disable', description='Disable a specified Command')
async def disable(interaction: discord.Interaction, command: str):
    await interaction.response.defer()
    if tree.get_command(command, guild=discord.Object(id=guild_id)):  # check if the command exists
        tree.remove_command(command, guild=discord.Object(id=guild_id))  # remove the command
        print("command removed!")
        await tree.sync(guild=discord.Object(id=guild_id))  # sync the tree
        print("tree synced!")
        await interaction.followup.send(
            f"{command} was disabled. You can reenable it by running /enable {command}")
    else:
        await interaction.followup.send(f"Command could not be found: {command}")

Tested this locally & the slash command say doesn't appear anymore. It's also gone from integrations: say command is gone

Python相关问答推荐

如何在具有重复数据的pandas中对groupby进行总和,同时保留其他列

使用numpy提取数据块

根据不同列的值在收件箱中移动数据

输出中带有南的亚麻神经网络

Pandas - groupby字符串字段并按时间范围 Select

在Python中管理打开对话框

无法定位元素错误404

在ubuntu上安装dlib时出错

Geopandas未返回正确的缓冲区(单位:米)

matplotlib图中的复杂箭头形状

用SymPy在Python中求解指数函数

PYTHON、VLC、RTSP.屏幕截图不起作用

无法在Spyder上的Pandas中将本地CSV转换为数据帧

有没有办法在不先将文件写入内存的情况下做到这一点?

我什么时候应该使用帆布和标签?

操作布尔值的Series时出现索引问题

如何在不不断遇到ChromeDriver版本错误的情况下使用Selify?

用LAKEF划分实木地板AWS Wrangler

PYODBC错误(SQL包含-26272个参数标记,但提供了235872个参数,HY 000)

正则表达式反向查找