1

I'd like my bot to respond when a certain user is mentioned. I've only found snippets of the answer on Google and I can't quite put them together. I've noticed that if I call a string in place of user ID it works, but if I call the string with the user ID in the same message it returns nothing.

Any insight is appreciated, work is below (subbing the desired users id into <@user_id> of course):

@client.event
async def on_message(message):
    if message.author == client.user:
      return
        #Making sure it doesn't respond to itself

    if "@here" in message.content: 
      return
    if "@everyone" in message.content:
      return
        #Making sure it doesn't respond to tags including everyone

    if '<@user_id>' in message.content:
        clock = get_time()
        clock = json.dumps(clock, indent=4)
        await message.channel.send("The time in Athens is currently: ")
        await message.channel.send(clock[-11: -6])

client.run(os.environ['TOKEN'])
yotam rec
  • 587
  • 4
  • 15
Adam Marx
  • 11
  • 4
  • Does this answer your question? [How to get the ID of a mentioned User in Python discord bot?](https://stackoverflow.com/questions/48354901/how-to-get-the-id-of-a-mentioned-user-in-python-discord-bot) – futur Aug 21 '21 at 23:51

1 Answers1

0

Message object have a mentions object, so you can get the mentioned user from there like this:

    message.mentions[0]

That returns the user object, you can do whatever you want with that.

If you want to check if there is a mention in the message just do:

    if message.mentions:
        #your code
yotam rec
  • 587
  • 4
  • 15
Nico Halpe
  • 398
  • 2
  • 9
  • Thank you for the help! That got me one step closer (unless I'm missing something), but running it gave me this error: Ignoring exception in on_message Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(*args, **kwargs) File "main.py", line 31, in on_message if user_id in message.mentions[0]: TypeError: argument of type 'Member' is not iterable I have no idea how to proceed to make Member iterable. Code is in the comment below: – Adam Marx Aug 22 '21 at 14:16
  • ` python if user_id in message.mentions[0]: clock = get_time() clock = json.dumps(clock, indent=4) await message.channel.send("The time in Athens is currently: ") await message.channel.send(clock[-11: -6])` – Adam Marx Aug 22 '21 at 14:23