Reaction Roles Discord Bot

Minahil Faisal
 
import os
import discord
from discord.ext import commands

# Environment variables
BOT_TOKEN = os.environ['DISCORD_BOT_TOKEN']
GUILD_ID = int(os.environ['GUILD_ID'])
MESSAGE_ID = int(os.environ['MESSAGE_ID'])

# Reaction-role mapping
REACTION_ROLE_MAP = {
    '🔴': 'team red',
    '🔵': 'team blue',
    # TODO: Add more reaction-role pairs as needed
}

intents = discord.Intents.default()
intents.members = True
intents.reactions = True

bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event

About this template

The Discord bot can manage multiple user roles based on reactions to a specific message. The reaction-role mapping is included directly in the code for easy editing by the builder.

Introduction to the Reaction Roles Discord Bot Template

Welcome to the Reaction Roles Discord Bot Template! This template allows you to create a Discord bot that can automatically manage user roles based on reactions to a specific message. This is particularly useful for community servers where you want to assign roles without manual intervention. The bot uses emoji reactions as triggers to assign or remove roles from users in your Discord server.

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Initial setup: Adding Environment Secrets

Before you can test and deploy your bot, you need to set up a few environment secrets within the Lazy Builder. These secrets include your Discord bot token, the ID of your guild (server), and the ID of the message you want to use for reaction roles.

  • DISCORD_BOT_TOKEN: This is the token for your Discord bot. You can obtain it from the Discord Developer Portal after creating your bot.
  • GUILD_ID: The numerical ID of your Discord server. You can find this by enabling Developer Mode in Discord, right-clicking on your server name, and selecting "Copy ID".
  • MESSAGE_ID: The numerical ID of the message that will be used for assigning roles. Obtain this by right-clicking on the message and selecting "Copy ID" (Developer Mode required).

Enter these values in the Environment Secrets tab within the Lazy Builder.

Test: Pressing the Test Button

Once you have set up the environment secrets, press the "Test" button to begin the deployment of your app. The Lazy CLI will handle the deployment process, and you won't need to worry about installing libraries or setting up your environment.

Using the App

After deployment, your Discord bot will be live and ready to manage roles based on reactions. Here's how it works:

  • When a user reacts to the specified message with an emoji that is mapped to a role in the REACTION_ROLE_MAP dictionary, the bot will automatically assign that role to the user.
  • If a user removes their reaction, the bot will remove the corresponding role from that user.

Make sure the bot has the necessary permissions to manage roles in your Discord server and that the roles you want to assign are correctly named in the REACTION_ROLE_MAP dictionary within the code.

Integrating the App

To integrate this bot into your Discord server, you need to invite the bot to your server using the OAuth2 URL generated in the Discord Developer Portal. Ensure you grant the bot the 'Manage Roles' permission and any other permissions it may require to function correctly.

After inviting the bot to your server, make sure to place the bot's role above the roles it will manage in the server's role hierarchy. This is necessary for the bot to have the ability to assign and remove roles from users.

With the bot set up and integrated into your server, you can now enjoy automated role management based on user reactions. This will make your server more interactive and engaging for your community.

If you need to make changes to the reaction-role mappings or add new ones, you can do so directly in the Lazy Builder interface by updating the REACTION_ROLE_MAP dictionary in the code.

Enjoy your new Reaction Roles Discord Bot!

Category
Technology
Last published
July 26, 2024

More templates like this

AlphaBot Financial Summary

AlphaBot Financial Summary: A Discord bot that utilizes the Alpha Vantage API to provide AI-generated summaries of income statements, balance sheets, and cash flow for renowned US companies.

Discord

Discord Moderation Bot

The Discord bot monitors all messages in the server. If a message contains profanity, the bot deletes it and sends a warning to the channel. The bot also notifies the host about the deletion via a direct message. Additionally, the bot outputs a helpful error message to a channel if there is a permissions error, guiding the server admin to enable the required permission in the Discord Developer portal.

Discord
Python
Flask

Discord Bot

This powerful app skeleton is a great starting place for a Discord bot

Discord
Home
/
Reaction Roles Discord Bot