【问题标题】:DiscordPy Disable Command: Command Which Disable A Specific Command In A Specific ServerDiscordPy 禁用命令:在特定服务器中禁用特定命令的命令
【发布时间】:2020-12-09 11:39:55
【问题描述】:

嗨,这是我在 StackOverflow 上的第一个问题,哈哈。 我想创建一个命令来禁用特定服务器中的特定命令,它就像:- !disable ping si 这将禁用服务器中的 ping 命令。我还希望机器人发送 Command {command} Is Disabled in {guild} 所以让我给你发送一个启动器-

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

就是这样,对不起,我要求完整的代码,希望你清楚。

【问题讨论】:

    标签: python command discord


    【解决方案1】:

    为每个服务器创建一个字典可能是最直接的方法之一。使用此方法,您只需在每个命令前添加 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!")
    

    【讨论】:

      猜你喜欢
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      相关资源
      最近更新 更多