DiscordPy Disable Command:禁用特定服务器中特定命令的命令

问题描述

嗨,这是我对StackOverflow的第一个问题。 我想创建一个命令来禁用特定服务器中的特定命令,这就像:-disable ping si这将禁用服务器中的ping命令。我还希望机器人发送命令{command}在{guild}中已禁用,所以让我向您发送一个启动器-

@bot.command
@has_permissions(administrator=True)
async def disable(ctx,command)
command = ...

就这样,抱歉,我要求提供完整的代码,希望对您来说很清楚。

解决方法

为每个服务器创建字典可能是最直接的方法之一。使用这种方法,您只需要在每个命令之前添加3行代码来检查其是否已启用。请注意,此示例不包括存储数据,也不说明需要将更多命令添加到commandsEnabled字典中。

commandsEnabled = {}

@bot.event
async def on_guild_join(guild):
    commandsEnabled[str(guild.id)] = {}
    for cmd in bot.commands:
        commandsEnabled[str(guild.id)][cmd.name] = True
    
@bot.command
@has_permissions(administrator=True)
async def Toggle(ctx,command):
    try:
        commandsEnabled[str(guild.id)][cmd.name] = not commandsEnabled[str(guild.id)][cmd.name]
        await ctx.send(f"{command} command {['disabled','enabled'][int()]}")
    except KeyError:
        await ctx.send(":x:Command with that name not found")

@bot.command
async def Example(ctx):
    if not commandsEnabled[str(ctx.guild.id)]["Example"]:
        await ctx.send(":x:This command has been disabled")
        return
    await ctx.send("Hello world!")