我正在我的不和谐机器人中创建自动删除消息功能,当我使用从审查员列表中删除单词命令时,我搁浅了。因为我想从列表中删除的单词在 msg get 被机器人删除的列表中。我想通过在我的消息删除消息事件中添加一个额外的 if 来规避这一点,该事件检查用户是否具有 Admin 角色或 Mod 角色。我想要的结果是,如果他们确实拥有该角色,它将返回并且 msg delete 永远不会执行
if any(word in message.content for word in censorlist):
if message.author == client.user:
return
elif
return
else:
await message.delete()
await client.process_commands(message)```
答案 0 :(得分:0)
主要有两种获取角色的方式,discord.utils.get
,还有guild.get_role
。无论如何这里都是两个例子
get_role(需要 ID)
# Somewhere above all the if statements
admin_role = message.guild.get_role(id_here)
mod_role = message.guild.get_role(id_here)
# And then in your elif
elif mod_role in message.author.roles or admin_role in message.author.roles:
这通过 id 获取角色并检查成员是否具有角色
utils.get(需要名称)
# Somewhere above all the if statements
admin_role = discord.utils.get(message.guild.roles, name="Admin")
mod_role =
# And then in your elif
elif mod_role in message.author.roles or admin_role in message.author.roles:
这通过名称获取角色并检查成员是否具有角色
或者也可以在一行中完成(看起来很糟糕)
elif discord.utils.get(message.author.roles, name="Mod") or discord.utils.get(messages.author.roles, name="Admin"):
这利用了 utils.get 如果未找到事物则返回 None 的事实。所以它正在为名为 Admin/Mod 的角色搜索成员的角色,如果没有找到,则成员没有该角色
一些不推荐的更短的方式
elif any(role.id in [mod_role_id, admin_role_id] for role in message.author.roles):
这将成员拥有的每个角色的 id 角色与 mod 和 admin 角色的角色 id 进行比较