python - 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
好像看不懂这个角色。任何想法?
解决方案
每个公会的用户可以有不同的角色,在 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`")
推荐阅读
- sql - 如何将 R 会话中的值替换为 SQL 绑定变量占位符?
- scrapy - 使用css使用scrapy抓取下一页
- android - 保存的图片base64是错误的
- javascript - 承诺完成后如何在axios中调用数组元素
- r - 如何避免separate_rows()-function 的“No common size”错误
- google-oauth - 需要采取的行动:在 9 月 30 日之前确认您的 OAuth 客户端设置,以便继续访问 Google API
- python - 儒略日期函数未知字符串日期错误
- pandas - 带有 .shift 的 Pandas 中的简单返回索引
- android - 在 Fragment 之间传递方法
- javascript - 根据匹配键添加对象值