允许具有特定角色的用户访问命令

时间:2018-10-14 12:19:54

标签: python-3.x discord discord.py

由于某种原因,它不允许我在某些具有特定角色“ DJ”的用户访问命令的情况下使用该命令。我已经尝试了一切,但仍然无法正常工作。

@commands.command(pass_context=True, no_pm=True)
async def leave(self, ctx):
    if ctx.message.author.roles == 'DJ':
        try:
            server = ctx.message.server
            voice_client = self.client.voice_client_in(server)
            await voice_client.disconnect()
        except Exception as error:
            await self.client.say('{}'.format(error))
    else:
        await self.client.say('You require the role `DJ` to use this command.')

1 个答案:

答案 0 :(得分:0)

author.roles返回一个Role对象的列表,因此您必须遍历该列表并找到一个名称为“ DJ”的对象

您可以这样做

if "DJ" in [role.name.upper() for role in ctx.message.author.roles]: