bots - 如何让机器人识别何时提到角色,然后做出回应?
问题描述
我希望机器人能够识别何时标记了@Community Manager、@Admin、@Moderator 角色,在同一条消息中标记了单个或多个角色,然后向提及用户名的频道发送消息。
我可以让机器人识别何时使用此代码对其进行标记:
if client.user.mentioned_in(message) and message.mention_everyone is False:
await message.delete()
我一生都无法弄清楚如何查看是否标记了其他角色。
我试过了
if message.role_mentions.name('Admin'):
#do stuff
但得到这个错误: AttributeError: 'list' object has no attribute 'name'
解决方案
message.role_mentions
返回一个列表Role
。
然后您可以使用从公会获取角色message.guild.get_role(id)
,以便与您从消息中获得的角色列表进行比较。
应该会产生类似这样的结果:
# Create a list of roles to check against
rolesToCheckAgainst [
# Fetches the role from the guild which the message was sent from
message.guild.get_role("The ID of the role"),
message.guild.get_role("The ID of the second role")
#etc...
]
# 'rolesWerePinged' will be true if any of the roles were pinged
rolesWerePinged = any(item in rolesToCheckAgainst for item in message.role_mentions)
if rolesWerePinged:
# Do something, some of the roles was pinged.
Also, I used any()
to check if any of the roles mentioned contained any of the roles that needed to be check against.
You can use a double-loop instead if you need different actions to be done depending on the type of roles mentioned.
推荐阅读
- arduino - Arduino 中断
- node.js - 当我将 Procfile 推送到 master 以部署应用程序时,Dyno 不会出现
- android - 具有多种视图类型的 RecyclerView 和具有边界回调的分页库
- r - 区域统计数据以获取 R 中每个多边形的多数像素值?
- c# - 我在我的 Asp.NET Core 2.2 项目中使用 ReflectionIT.Mvc.Paging 进行分页时遇到问题
- wordpress - 如何通过 ACF 选择标签而不是值进行排序?
- javascript - javascript - `` 在同一行中。其他在表单之外生成的元素
- c# - A* 寻路算法 - 在计算最短路径时卡住了
- php - 如何修复 Session_start 警告?
- time-complexity - 哈希表与二叉搜索树、大 O 访问和搜索