Created
February 22, 2021 02:53
-
-
Save EhsanKia/30e81c206bd7ba5805f1ce3a068abdb3 to your computer and use it in GitHub Desktop.
Simple and minimal Discord bot made using discord.py for assigning a specific role when a user reacts to a given message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.