by yahya

Discord Auto Role Bot with Assignment

Test this app for free
219
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
Get full code

Discord Auto Role Bot with Assignment

Created: | Last Updated:

The Discord auto role bot is a powerful tool designed to automate role management on Discord servers. It has the ability to assign any role to a user and automatically assign a specific role to new members upon joining. This bot is particularly useful for large servers where role management can become a tedious task. To function, it requires the 'DISCORD_BOT_TOKEN' to be set in the environment variables. The bot supports the following commands: - '!add_role <role> <user>': Assigns the specified role to the specified user. If no user is specified, the role is assigned to the author of the command. - 'on_member_join': Automatically assigns the 'New Member' role to new members upon joining.

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

  1. 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:

  1. Go to the Discord Developer Portal (https://discord.com/developers/applications).
  2. Click "New Application" and give your application a name.
  3. Navigate to the "Bot" tab and click "Add Bot".
  4. Under the "Token" section, click "Copy" to copy your bot token.
  5. In the Lazy Builder, go to the Environment Secrets tab.
  6. Add a new secret with the key DISCORD_BOT_TOKEN and paste your bot token as the value.

Test the Bot

  1. Click the "Test" button in the Lazy Builder interface to start the deployment process.
  2. 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:

  1. Assign the "New Member" role to any new member joining the server.
  2. Allow administrators to manually assign roles using the !add_role command.

To manually assign a role:

  1. Use the command: !add_role <role> <user>
  2. Replace <role> with the name of the role you want to assign.
  3. 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:

  1. Go back to the Discord Developer Portal and select your application.
  2. Navigate to the "OAuth2" tab.
  3. In the "Scopes" section, select "bot".
  4. In the "Bot Permissions" section, select the necessary permissions (at minimum: "Manage Roles" and "Send Messages").
  5. Copy the generated OAuth2 URL.
  6. Open this URL in a new browser tab and select the server where you want to add the bot.
  7. Follow the prompts to authorize the bot for your server.

Once the bot is added to your server, ensure that:

  1. The bot's role is placed higher in the role hierarchy than the roles it needs to manage.
  2. 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.

Technologies

Discord Discord
Python Python