我如何让我的不和机器人回复一个提及?

2024-04-25 17:16:09 发布

您现在位置:Python中文网/ 问答频道 /正文

当有人提到我的不和谐机器人时,我想让它回复。例如,如果@someone键入“Hello@bot”,我希望我的bot回复“Hello@someone!”

我尝试了几种方法: 1.

@client.event
async def on_message(message):
  if message.author == client.user:
    return
  if message.content == ("@bot"):
    await message.channel.send("Hello {}".format(message.author.mention) + "!")

即使这样,

@client.event
    async def on_message(message):
      if message.author == client.user:
        return
      if message.content.startswith("@bot"):
        await message.channel.send("Hello {}".format(message.author.mention) + "!")

但这些都不起作用

那么,我如何让我的不和谐机器人回复一个提及


Tags: clienteventmessagehelloasyncreturnifon
1条回答
网友
1楼 · 发布于 2024-04-25 17:16:09

不和谐的提及不是这样处理的,它们的内部格式如下所示:

<@{id_here}>  - Normal mention
<@!{id_here}> - Nick mention
<@&{id_here}> - Role mention
<#{id_here}>  - Channel mention

您可以创建一个简单的正则表达式:

import re

@client.event
async def on_message(message):
    if message.author == client.user:
        return
  
    pattern = re.compile(f"hello <@!?{client.user.id}>") # The exclamation mark is optional

    if pattern.match(message.content.lower()) is not None: # Checking whether the message matches our pattern
        await message.channel.send(f"Hello {message.author.mention}!")

相关问题 更多 >