Bots 如何让机器人在提到某个角色时识别,然后回复?

Bots 如何让机器人在提到某个角色时识别,然后回复?,bots,discord,python-3.7,Bots,Discord,Python 3.7,我希望bot能够识别@communitymanager、@Admin、@版主角色何时被标记,在同一消息中标记单个或多个角色,然后向频道发送一条消息,提及用户名 我可以让机器人识别何时使用以下代码标记: if client.user.mentioned_in(message) and message.mention_everyone is False: await message.delete() 我一辈子都不知道如何查看其他角色是否被标记 我试过了 if message.rol

我希望bot能够识别@communitymanager、@Admin、@版主角色何时被标记,在同一消息中标记单个或多个角色,然后向频道发送一条消息,提及用户名

我可以让机器人识别何时使用以下代码标记:

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”对象没有属性“name”

返回属性列表

然后,您可以使用从公会获取角色,以便与您从消息中获得的角色列表进行比较

应该会产生类似的结果:

# 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.

此外,我曾经检查提到的任何角色是否包含需要检查的任何角色。
如果您需要根据提到的角色类型执行不同的操作,则可以使用双循环。

因此,这与我的代码冲突,我的代码让我知道是否有人向bot发送了DM。当某人DM的it用于向控制台发布消息是什么以及消息来自谁时,现在由于我将上面的代码放入,它抛出以下错误:AttributeError:'NoneType'对象没有属性'get_role'。这是说rolestocheckback=[message.guild.get_角色(521374635712053249)是issue@BrandonWeeks它说,
guild
为null,您正试图使用
get_role
from null。检查您的
消息是否首先在帮会中发送。角色提及消息是,是,但当bot接收到DM时会抛出此错误。两者都在异步def on_消息中,我的if是否iInstant(message.channel,discord.DMChannel)需要在其他地方吗?在调用整个代码之前,你应该检查
message.guild
是否为空。我该怎么做?我有点自学了,有点python方面的经验