首页 > 解决方案 > discord.py has_role 通过 DM

问题描述

我正在尝试使用has_role但在 DM 中但无法正常工作。

@commands.check_any(commands.has_role(role_id))
@commands.dm_only()
elif isinstance(error, commands.CheckAnyFailure):
        await ctx.send("`ERROR: Permission denied`")

在服务器工作!

在 DM 中总是返回ERROR: Permission denied

好像看不懂这个角色。任何想法?

标签: pythondiscord.pyroles

解决方案


每个公会的用户可以有不同的角色,在 DM 中它没有。您应该检查特定公会中用户的角色:

@bot.command()
@commands.dm_only()
async def whatever(ctx):
    guild = bot.get_guild(GUILD_ID)
    member = guild.get_member(ctx.author.id)
    role_id = 123123123

    if any(role.id == role_id for role in member.roles):
        # has necessary role
    else:
        await ctx.send("`ERROR: Permission denied`")

推荐阅读