在 on_command 事件中取消命令?

时间:2021-05-25 19:44:30

标签: python python-3.x asynchronous discord discord.py

我正在向我的 Discord 机器人添加一项功能,允许您禁用特定频道中的命令。除了实际禁用命令部分外,一切正常。

@bot.event
async def on_command(ctx):
    try:
        with open('serverdata.json', 'r') as f:
            data = json.load(f)

        await update_server_data(data, ctx.channel.guild)

        storage = data[f'{ctx.channel.guild.id}']["channels"][f'{ctx.channel.id}']["disabled_commands"]

        if str(ctx.command) in storage:
            await ctx.channel.send(f'{ctx.command} is disabled in this channel!')
            # somehow stop command from working
    except:
        pass

是否有某种内置函数,或者我必须使用不同的方法?我宁愿不必为每个命令添加新代码。感谢所有帮助

1 个答案:

答案 0 :(得分:0)

您可以通过在命令定义本身中编写 if 语句来禁用该命令,if 语句可以向用户写入消息并以 return 结束函数。 示例:

@bot.command()
async def an_example_comand(ctx, *, some_parameters):
    if not command_allowed():
        await ctx.send("This command is not allowed in this channel.")
        return
    
    # Here should be the code to be executed if the command is allowed.


# The command_allowed() function should return a boolean.
# If it returns False then the command won't run.
    
相关问题