如何让漫游器识别何时提到角色,然后进行回复?

时间:2019-08-04 09:54:29

标签: bots discord python-3.7

我希望机器人能够识别出何时标记了@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:“列表”对象没有属性“名称”

1 个答案:

答案 0 :(得分:1)

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.

此外,我使用any()检查所提到的任何角色是否包含需要检查的任何角色。
如果需要采取其他措施来代替,可以使用双循环。根据提到的角色类型来完成。