0

So I was programming a discord bot in python. But there is a problem. I know how to mention the user who sent the message. But how do I get the ID of a user that is mentioned by the author in the message? I couldn't get the answer.

halfer
  • 19,824
  • 17
  • 99
  • 186
Raebel Christo
  • 35
  • 2
  • 2
  • 5

2 Answers2

4

You can get the user(s) that were mentioned in a message using message.mentions (async rewrite)

From there, you can either get the ID from the Member object, or get a formatted mention from those objects

message.mentions[0].id
message.mentions[0].mention
Sam Rockett
  • 3,145
  • 2
  • 19
  • 33
  • So will the following example be correct? `async def on_message(): if message.content.upper() == "+CALL": await bot.send_message(message.channel, "<%s> and <%s> are called to come" % (message.mentions[0].id,message.mentions[1].id))` – Raebel Christo Jan 20 '18 at 11:00
  • No. Mentions are in the format `<@...>` not just `<...>`. Besides, you can just use `member.mention` to create a preformatted mention. `"%s" % message.mentions[0].mention`. Lastly, it might be worth looking into [`str.format`](https://stackoverflow.com/questions/5082452/python-string-formatting-vs-format) – Sam Rockett Jan 20 '18 at 15:16
0

discord.Message objects have an attribute of mentions that returns a list of discord.Member objects of all users mentioned in the message content, discord.Member objects have discord.User objects as parents so you can access discord.User attributes like author.id and author.name etc. simply iterate over the mentions with

for member in message.mentions:
    # do stuff with member

You should really be using discord.ext.commands(async rewrite) because making a command parser that purely works in on_message event method is a bad non pythonic idea.

What you probably want

seems like all you just want is to be able to access the author object of the message and the users mentioned in the message as parameters, for that please consult my answer here. To access the author you can simply do it through the discord.Message object with message.author

mental
  • 836
  • 6
  • 18