Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple and minimal Discord bot made using discord.py for assigning a specific role when a user reacts to a given message.
import discord
BOT_TOKEN = '(bot token here)'
MESSAGE_ID = 1234
EMOJI_NAME = 'emojiName'
ROLE_ID = 4321
class ReactionRole(discord.Client):
async def on_raw_reaction_add(self, payload):
await self.add_remove_role(payload)
async def on_raw_reaction_remove(self, payload):
await self.add_remove_role(payload)
async def add_remove_role(self, payload):
if payload.message_id != MESSAGE_ID:
return
if payload.emoji.name != EMOJI_NAME:
return
guild = self.get_guild(payload.guild_id)
if guild is None:
return
member = guild.get_member(payload.user_id)
role = guild.get_role(ROLE_ID)
if not member or not role:
return
if payload.event_type == 'REACTION_ADD':
print(f'Adding {role} to {member}')
await member.add_roles(role)
elif payload.event_type == 'REACTION_REMOVE':
print(f'Removing {role} from {member}')
await member.remove_roles(role)
intents = discord.Intents.default()
intents.members = True
client = ReactionRole(intents=intents)
client.run(BOT_TOKEN)
@EhsanKia
Copy link
Author

EhsanKia commented Feb 22, 2021

This is mostly for my own reference. This is a very simple bot I needed and most of the stuff out there was overly complicated or bloated. This is very minimal, and doesn't have anything extra. It's looking for one emote on one message, and it adds/removes one role from whichever user reactions or unreacts on that messages.

You can easily expand this to handle multiple roles/emotes/messages if you're familiar with Python, but this code should give you a quick idea of what the base is on the discord API side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment