如何使discord.py命令检查用户角色?

时间:2020-06-25 17:05:51

标签: python roles discord.py-rewrite

例如,我可能只希望管理员能够使用以下命令:

if ('!hello') in message.content.lower():
        await message.channel.send('Hello friend!')

如何做到这一点,以便只有具有“管理员”角色的用户才能使用此命令?我似乎在文档中找不到这个。

1 个答案:

答案 0 :(得分:1)

您可以找到管理员权限here的discord.py文档。

您正在寻找的方法是这样:

ctx.message.author.guild_permissions.administrator

下面,我编写了一个模拟示例,该示例清除了该通道,并且仅对管理员可用。

@client.command() #Clear (purge) messages from channel (can only be used by users with administrator permissions)
    async def clear(ctx, amount=100):
        channel = ctx.message.channel
        if ctx.message.author.guild_permissions.administrator:
            await channel.purge(limit=amount,check=None,bulk=True)
        else:
            await ctx.send("You can't use that command, you are not an administrator!")
相关问题