python - 检查用户是否有角色
问题描述
我正在尝试检查用户是否具有特定角色。我知道@commands.has_role(),但这不适用于我正在制作的内容。我在下面有示例代码,我不确定如何获取它来检查用户是否具有角色。
@commands.command()
async def hasthisroletest(self, ctx):
if ctx.author.role.id == 746831268683186268:
await ctx.send("You did it!")
else:
await ctx.send("Nope :(")
解决方案
如果想要的角色在那里,您可以检查Member.roles 。
使用discord.utils.get获取角色本身。
@commands.command()
async def hasthisroletest(ctx):
# you can also use name = 'Test_role'
role = discord.utils.get(ctx.guild.roles, id=746831268683186268)
if role in ctx.author.roles:
await ctx.send("You did it!")
else:
await ctx.send("Nope :(")
推荐阅读
- javascript - 如何从 php 运行 javascript 代码(提交后重新显示隐藏字段)
- yaml - 在 YAML 中使用 Fn::GetAtt 时,流集合条目之间缺少逗号
- php - PHP | 如何从远程服务器下载和提取 ZIP 文件?
- reactjs - 反应选择 - setData
- react-native - 在反应本机应用程序中呈现大型列表的问题
- javascript - 使用 react-player API 导致无法在控制台中的“DOMWindow”错误上执行“postMessage”?
- python - 如何在 Django 中创建一个模型的对象,同时创建另一个不同的模型?
- php - Laravel 7:读取单个资源不起作用
- ruby-on-rails - 如何测试具有早期回报的haml部分?
- jquery - Jquery 无法更改路线(Svelte,Sapper)