我可以发出命令来关闭只有受信任的成员才能使用的 discord 机器人吗? [PYTHON/DISCORD.PY]

Can I make a command to shutdown a discord bot that only trusted members can use? [PYTHON/DISCORD.PY]

我们尝试了很多解决方案,但没有任何效果。 这是我们目前所拥有的(出于安全原因删除了个人详细信息)。

@bot.command(aliases=['disconnect', 'close', 'stopbot'])
async def logout(ctx):
    if ctx.guild.id is not serverIdHere:
        return
    else:
        role = discord.utils.get(ctx.guild.roles, name="Trusted Developer")
        if role in ctx.author.roles:
            await ctx.send(f"Ok")
            await bot.logout()
        else:
            return

如有任何帮助,我们将不胜感激。

如果您希望用户具有特定角色

@bot.command()
@commands.has_role('id_here or name')
async def logout(ctx):
    await ctx.send('Shutting down')
    await bot.logout()

如果需要,只有所有者才能使用它

@bot.command()
@commands.is_owner()
async def logout(ctx):
    await ctx.send('Shutting down')
    await bot.logout()

如果您想要特定的用户 ID,有三个选项

1.

def is_valid_user(ctx):
    return ctx.author.id in my_list_of_ids

@bot.command()
@commands.check(is_valid_user)
async def logout(ctx):
    await ctx.send('Shutting down')
    await bot.logout()
def can_use_this_command():
    async def predicate(ctx):
        return ctx.author.id in my_list_of_ids
    return commands.check(predicate)

@bot.command()
@can_use_this_command()
async def logout(ctx):
    await ctx.send('Shutting down')
    await bot.logout()
@bot.command()
async def logout(ctx):
    if ctx.author.id not in my_list_if_ids:
        return

    await ctx.send('Shutting down')
    await bot.logout()

参考: