在调用之前取消命令,但不打印错误消息(discord.py)

2024-10-05 15:18:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个discord bot,我想阻止它运行,除非填充了某个参数

以下是bot的当前代码:

@bot.before_invoke
async def checkguild(message):
    command = message.command
    if command != 'settings':
        if not message.guild.id in globaldb.servers_setup.val:
            for required_setting in REQUIRED_SETTINGS:  # Iterates through the required settings to make sure they're all defined.
                if not get_guild_db(message.guild).settings.has(required_setting):
                    # Prevent command from running, and call the on_command_error function instead of just throwing an error.
            globaldb.set('servers_setup', globaldb.servers_setup.val + [message.guild.id])
            globaldb.save()  # Otherwise add the server to the database as set up.
我想这样做,正如中间的评论所说的,

# Prevent command from running, and call the on_command_error function instead of just throwing an error.

但是我该怎么做呢?(检查下面的答案)


Tags: theinidmessageifsettingsbotsetup
1条回答
网友
1楼 · 发布于 2024-10-05 15:18:16

为了使用discord.ext.commands提供的错误系统,我们必须引发它的CommandError异常

这将按如下方式实施:

@bot.before_invoke
async def checkguild(message):
    command = message.command
    if command != 'settings':
        if not message.guild.id in globaldb.servers_setup.val:
            for required_setting in REQUIRED_SETTINGS:  # Iterates through the required settings to make sure they're all defined.
                if not get_guild_db(message.guild).settings.has(required_setting):
                    # Prevent command from running, and call the on_command_error function instead of just throwing an error.
                    raise discord.ext.commands.CommandError(f'Before using this bot, please set the `{required_setting}` setting.')
            globaldb.set('servers_setup', globaldb.servers_setup.val + [message.guild.id])
            globaldb.save()  # Otherwise add the server to the database as set up.

相关问题 更多 >