by yahya
Discord Auto Role Bot with Assignment
import os
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.event
async def on_member_join(member):
role = discord.utils.get(member.guild.roles, name="New Member")
if role:
await member.add_roles(role)
@bot.command()
@has_permissions(administrator=True)
async def add_role(ctx, role: discord.Role, user: discord.Member = None):
user = user or ctx.author
Discord Auto Role Bot with Assignment
Created: | Last Updated:
Here's a step-by-step guide on how to use the Discord Auto Role Bot with Assignment template:
Introduction
This template provides a Discord bot that automatically assigns roles to new members and allows administrators to manually assign roles to users. The bot is designed to streamline role management in Discord servers, especially for larger communities.
Getting Started
- Click "Start with this Template" to begin using this template in the Lazy Builder interface.
Initial Setup
Before running the bot, you need to set up a Discord bot and obtain its token. Follow these steps:
- Go to the Discord Developer Portal (https://discord.com/developers/applications).
- Click "New Application" and give your application a name.
- Navigate to the "Bot" tab and click "Add Bot".
- Under the "Token" section, click "Copy" to copy your bot token.
- In the Lazy Builder, go to the Environment Secrets tab.
- Add a new secret with the key
DISCORD_BOT_TOKEN
and paste your bot token as the value.
Test the Bot
- Click the "Test" button in the Lazy Builder interface to start the deployment process.
- The Lazy CLI will appear, showing the bot's login status.
Using the Bot
Once the bot is running, it will automatically perform the following functions:
- Assign the "New Member" role to any new member joining the server.
- Allow administrators to manually assign roles using the
!add_role
command.
To manually assign a role:
- Use the command:
!add_role <role> <user>
- Replace
<role>
with the name of the role you want to assign. - Replace
<user>
with the mention or ID of the user (optional, defaults to the command user).
Example:
!add_role Moderator @JohnDoe
Integrating the Bot
To add the bot to your Discord server:
- Go back to the Discord Developer Portal and select your application.
- Navigate to the "OAuth2" tab.
- In the "Scopes" section, select "bot".
- In the "Bot Permissions" section, select the necessary permissions (at minimum: "Manage Roles" and "Send Messages").
- Copy the generated OAuth2 URL.
- Open this URL in a new browser tab and select the server where you want to add the bot.
- Follow the prompts to authorize the bot for your server.
Once the bot is added to your server, ensure that:
- The bot's role is placed higher in the role hierarchy than the roles it needs to manage.
- There is a role named "New Member" for the automatic assignment feature to work.
With these steps completed, your Discord Auto Role Bot should be fully functional and ready to use in your server.