Discord Bot Role Mentioning

Viewed 58597

I am making simple Discord bot for my server, because part of one bot doesn't work. But, that bot needs to tag people from one role (let's say that role is "Moderator"). I wanted it to tag everyone from Moderator role, which will be like @Moderator. Here is my code (I am using Python 3.6):

if message.content.startswith('!startbot'):
    msg = '@Moderator, (some message after this)'.format(message)

But, that "@Moderator" doesn't actually tag anyone from Moderator role. It's just blank text like every other message. But, when I as someone real from Discord server type @Moderator, it brings red color (which I set) and it tags Moderator.

Can someone help me please to solve this thing ?

5 Answers

Role mentions in Discord are triggered like this:

<@&ROLE_ID>

Where ROLE_ID is the ID of the role you are trying to mention. Get the ID of the Moderators role, add it to the string accordingly and the bot will mention the role as you would from the Discord client.

This method also works for webhooks.

Mentioning in embeds requires a special format. The easiest way to do so is by consulting the following table:

Type Structure Example Output
User <@USER_ID> <@80351110224678912> user_mention
User (Nickname) <@!USER_ID> <@!80351110224678912> user_mention
Channel <#CHANNEL_ID> <#103735883630395392> text_channel_mention

voice_channel_mention
Role <@&ROLE_ID> <@&165511591545143296> role_mention
Custom Emoji <:NAME:ID> <:mmLol:216154654256398347> custom_emoji
Custom Emoji (Animated) <:a:NAME:ID> <a:nyancat:392938283556143104> custom_emoji_animated
Unix Timestamp <t:TIMESTAMP> <t:1618953630> timestamp
Unix Timestamp (Styled) <t:TIMESTAMP:STYLE> <t:1618953630:d> timestamp_styled

Taken from the Discord API Docs

Assuming you are using the current stable version of discord.py

Per documentation, the role object has a method named mention. So all you need to do is

msg = '{} ...'.format(role.mention) 

To obtain the role object you probably need to iterate over the server's available roles and find the role object you are looking for

You have to get the role object first. To do this just do:

moderator = discord.utils.get(ctx.guild.roles, id=moderator_role_id_here)

The just send a message

await ctx.send(f'Hello {moderator.mention}')

It will tag all users with this role.

If you send a message "@SomeRole" discord sends it as plain text, the same way if you "@mention" a person, it does the same thing. This is also the case if you sent ":thinking:" it would just send the text.

This code will mention a specific user, based on their ID:

user = message.guild.members.find("id", "201909896357216256");
await message.guild.send(`${user} is the best!`);

If you know the name of your role, and are fine with hard-coding it

modRole = msg.channel.server.roles.mention('name', 'Moderator')
bot.sendMessage(msg, modRole.mention() + " is anyone here?")

To expand on @pkqxdd's if you send "\@SomeRole" yourself (ie, not the bot), you will get the role id. You can then do a similar thing to the code above, by id instead of by name.

Related