by yahya
Discord Join and Leave Bot
import discord
from discord.ext import commands
import os
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Global variables to store channel IDs
welcome_channel_id = ''
leave_channel_id = ''
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.event
async def on_member_join(member):
if welcome_channel_id:
channel = bot.get_channel(welcome_channel_id)
if channel:
await channel.send(f'Welcome {member.mention} to the server!')
Created: | Last Updated:
Here's a step-by-step guide on how to use the Discord Join and Leave Bot:
Introduction
This template provides a Discord bot that welcomes new members and announces when members leave a server. It uses the discord.py library to interact with the Discord API.
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 application and bot:
- Go to the Discord Developer Portal (https://discord.com/developers/applications).
- Click "New Application" and give it a name.
- Go 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 "Env Secrets" tab.
- Add a new secret with the key
DISCORD_TOKEN
and paste your bot token as the value.
Test
- Click the "Test" button to start the deployment process.
Using the Bot
Once the bot is running, you can use the following commands in your Discord server:
!set_welcome_channel #channel-name
: Set the channel where welcome messages will be sent.!set_leave_channel #channel-name
: Set the channel where leave messages will be sent.
For example:
!set_welcome_channel #welcome
!set_leave_channel #goodbye
After setting these channels, the bot will automatically send messages when members join or leave the server.
Integrating the Bot
To add the bot to your Discord server:
- Go back to the Discord Developer Portal and select your application.
- Go to the "OAuth2" tab and then "URL Generator".
- Under "Scopes", select "bot".
- Under "Bot Permissions", select the permissions your bot needs (at minimum: "Send Messages", "Read Messages/View Channels").
- Copy the generated URL at the bottom of the page.
- Open this URL in a new tab and select the server you want to add the bot to.
- Follow the prompts to authorize the bot for your server.
Once the bot is added to your server and running via the Lazy platform, it will start responding to the commands and events as programmed.