Firman N
Firman N

Reputation: 11

How to mention someone in "on_message"

So I want to to make the bot to mention the author on author who has said n-word. I've tried to use {message.mention} but apparently it does not exist so how does I mention someone using on_message event? Here is the code:

@commands.Cog.listener()
async def on_message(self, message):
  if "ni**a" in message.content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author}")
  if 'ni**er' in message.content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author}")
  if 'Ni**a' in message.content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author}")
  if 'Ni**er' in message.content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author}")

Upvotes: 1

Views: 576

Answers (3)

Der Pruefer
Der Pruefer

Reputation: 75

If you want to mention a user you always have to do this:

await message.channel.send(f"blablabliblu------> {message.authormention}")#you want to mention the message author
#mention a other spezific user
user = client/bot.get_user(user_id)#get the user
await message.channel.sedn(f"hellooooooo---> {user.mention})")
#so you alwas need <someuser.mention>

Upvotes: 0

Deru
Deru

Reputation: 1173

Member object

In order to mention someone you need to have the member object associated with that person.

The member object contains an attribute "mention" which can be used to retrieve the string used to mention that member.


So how to apply this?

As message.author is a member object. We can use message.author.mention to get the string to mention that member. Resulting in the following code:

@commands.Cog.listener()
async def on_message(self, message):
  if "ni**a" in message.content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author.mention}")
  if 'ni**er' in message.content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author.mention}")
  if 'Ni**a' in message.content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author.mention}")
  if 'Ni**er' in message.content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author.mention}")

Simplify

And as some people have mentioned before we can simplify your code to this:

@commands.Cog.listener()
async def on_message(self, message):
  content = message.content.lower()
  if "ni**a" in content or "ni**er" in content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author.mention}")

References:

Member object

Member.mention

Message object

Message.content

Upvotes: 1

Nurqm
Nurqm

Reputation: 4743

If you want to mention the author of a message, you can use message.author.mention. And also you don't have to do 4 if statements, 1 is enough. Here's what you can do:

@commands.Cog.listener()
async def on_message(self, message):
  content = message.content.lower()
  if "ni**a" in content or "ni**er" in content:
    await message.channel.send(f"<:sniper:711509974588719216> R6 BRUH {message.author.mention}")

Upvotes: 1

Related Questions