how do I check if a user has a specific role in discord(如何检查用户是否在不和谐中具有特定角色)
问题描述
这应该检查特定的人是否具有静音角色
This should check if the specific person does or doesn't have the mute role
@bot.command(pass_context=True)
@commands.has_role("Admin")
async def unmute(ctx, user: discord.Member):
role = discord.utils.find(lambda r: r.name == 'Member',
ctx.message.server.roles)
if user.has_role(role):
await bot.say("{} is not muted".format(user))
else:
await bot.add_roles(user, role)
抛出此错误
命令引发异常:AttributeError: 'Member' object has no attribute 'has_role'
Command raised an exception: AttributeError: 'Member' object has no attribute 'has_role'
我不知道该怎么做,所以我非常感谢我能得到的每一个帮助
I don't know how to do it so i would really appreciate every help I can get
推荐答案
成员没有 .has_role()
方法,但是您可以使用 获取他们所有角色的列表.角色
.
Member does not have a .has_role()
method, you can however get a list of all their roles using .roles
.
要查看用户是否具有给定角色,我们可以使用 role in user.roles
.
To see if a user has a given role we can use role in user.roles
.
@bot.command(pass_context=True)
@commands.has_role("Admin")
async def unmute(ctx, user: discord.Member):
role = discord.utils.find(lambda r: r.name == 'Member', ctx.message.guild.roles)
if role in user.roles:
await bot.say("{} is not muted".format(user))
else:
await bot.add_roles(user, role)
参考文档:https://discordpy.readthedocs.io/en/最新/api.html#member
注意: ctx.message.guild.roles
使用 ctx.message.server.roles
.由于 API 更改而更新.
Note: ctx.message.guild.roles
use to be ctx.message.server.roles
. Updated due to API change.
这篇关于如何检查用户是否在不和谐中具有特定角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查用户是否在不和谐中具有特定角色


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01