Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord Bot Role Mentioning

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 ?

like image 670
SomeName Avatar asked Aug 27 '18 15:08

SomeName


Video Answer


4 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.

like image 144
110Percent Avatar answered Oct 10 '22 07:10

110Percent


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

like image 36
Destaq Avatar answered Oct 10 '22 07:10

Destaq


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

like image 5
Mia Avatar answered Oct 10 '22 06:10

Mia


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.

like image 2
Dec0Dedd Avatar answered Oct 10 '22 07:10

Dec0Dedd