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

2024-09-30 14:17:12 发布

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

嗨,这是我关于StackOverflow的第一个问题。 我想发出一个命令,在特定的服务器上禁用特定的命令,就像:-!禁用pingsi这将禁用服务器中的ping命令。我还想让机器人发送命令{Command}在{guild}中被禁用,所以让我给你发送一个初学者-

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

就像这样,对不起,我要的是完整的代码,我希望你明白


Tags: 命令服务器truepermissionsbot机器人pingstackoverflow
1条回答
网友
1楼 · 发布于 2024-09-30 14:17:12

为每台服务器创建字典可能是最简单的方法之一。使用此方法,您只需在每个命令之前添加3行代码,以检查它是否已启用。请注意,此示例不包括存储数据,也不包括需要添加到commandsEnabled dictionary的更多命令

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!")

相关问题 更多 >

    热门问题