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

DiscordPy Disable Command: Command Which Disable A Specific Command In A Specific Server

嗨,这是我关于 Whosebug 的第一个问题,哈哈。 我想制作一个命令来禁用特定服务器中的特定命令,它就像:- !disable ping si 这将禁用服务器中的 ping 命令。我还希望机器人发送 Command {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!")