我希望我的机器人能够回复对特定用户的提及(例如,一个人提到我的个人帐户,而机器人回复说我目前不在)
有没有办法使用与此类似的格式来做到这一点?
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('@user_id'):
await message.channel.send('Im not here leave a message!')
答案 0 :(得分:0)
您需要使用不和谐机器人接收提及的特定格式。格式为 <@!user_id>。
@client.event
async def on_message(message):
if ("<@!put user id here>" in message.content):
await message.channel.send("Im not here leave a message!")
如何应用它并且对我有用的示例
@client.event
async def on_message(message):
if ("<@!348256959671173120>" in message.content):
await message.channel.send("Im not here leave a message!")
答案 1 :(得分:0)
Discord Member objects 有一个 .mentioned_in(message)
方法。
WIZARD_ID = 123456 # <- replace with user ID you want
async def on_message(message):
wizard_of_oz = message.guild.get_member(WIZARD_ID)
if wizard_of_oz.mentioned_in(message):
await message.channel.send("Who dares summon the great Wizard of OZ?!")
如果您还想以 role 是否提及用户为条件,您还需要检查消息中的 role_mentions。所以更完整的例子如下:
def was_mentioned_in(message: discord.Message, member: discord.Member) -> bool:
"""
Whether or not the member (or a role the member has) was mentioned in a message.
"""
if member.mentioned_in(message):
return True
for role in message.role_mentions:
if role in member.roles:
return True
return False
@client.event
async def on_message(message):
wizard_of_oz = message.guild.get_member(WIZARD_ID)
if was_mentioned_in(message, wizard_of_oz):
await message.channel.send("Who dares summon the great Wizard of OZ?!")