Reputation:
I want my discord bot to reply when someone mentions it. For example, if @someone types "Hello @bot" I want my bot to reply "Hello @someone!".
I tried several methods: 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) + "!")
and even this,
@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) + "!")
But none of these worked.
So how do I make my discord bot reply to a mention?
Upvotes: 1
Views: 746
Reputation: 15728
Discord mentions aren't processed like that, their internal format looks like this:
<@{id_here}> - Normal mention
<@!{id_here}> - Nick mention
<@&{id_here}> - Role mention
<#{id_here}> - Channel mention
You can make a simple regex:
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}!")
Upvotes: 2