Reputation: 9
If I do something like if message.content.startswith('hello'):
it works fine.
However, I can't find a way to make it work for pings. I tried bot ID and name. My code with bot ID:
@client.event
async def on_message(message):
await client.process_commands(message)
if message.content.startswith('<@741352621826375781>'):
embed = discord.Embed(
colour = discord.Colour.red())
embed.add_field(name='Hello', value='''My prefix is `.`, type `.help` for help.''', inline=False)
await message.channel.send(embed=embed)
Can somebody help?
Upvotes: 0
Views: 659
Reputation: 66
There is an easier way to check if the bot was mentioned in a message or not.
@client.event
async def on_message(message):
if client.user in message.mentions:
await message.channel.send("Hello!")
Upvotes: 0
Reputation: 498
This should fix your issue, The reason your code didn't work is because of an attribute error.
@client.event
async def on_message(message):
if str(message.content) == '<@741352621826375781>':
await message.channel.send('Hello ?')
await client.process_commands(message)
Upvotes: 1
Reputation: 15689
Here is a bit improved version of Sofia
@client.event
async def on_message(message):
# Adding `<!@{id_here}>` so it's gonna work whether it's mentioned by nick or simply by the name
if message.content in [f'<!@{client.user.id}>', f'<@{client.user.id}>']:
await message.channel.send('Hello there')
await client.process_commands(message)
Upvotes: 1