6
votes

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

5 Answers

4
votes

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

22
votes

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.

6
votes

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

Type Structure Example
User <@USER_ID> <@80351110224678912>
User (Nickname) <@!USER_ID> <@!80351110224678912>
Channel <#CHANNEL_ID> <#103735883630395392>
Role <@&ROLE_ID> <@&165511591545143296>
Custom Emoji <:NAME:ID> <:mmLol:216154654256398347>
Custom Emoji (Animated) <:a:NAME:ID> <a:b1nzy:392938283556143104>

Taken from the Discord docs.

2
votes

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.

1
votes

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.