by yahya

Discord Join and Leave Bot

Test this app for free
443
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!')
Get full code

Created: | Last Updated:

This Discord bot is designed to display join and leave messages. It requires environment variables (DISCORD_TOKEN) and channel IDs to function properly. Please provide the DISCORD_TOKEN in the Env Secrets tab. To setup this Discord join and leave bot and receive a token, you need to create a new application in the Discord developer portal, add a bot to that application, and then copy the bot's token.

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

  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 application and bot:

  1. Go to the Discord Developer Portal (https://discord.com/developers/applications).
  2. Click "New Application" and give it a name.
  3. Go 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 "Env Secrets" tab.
  6. Add a new secret with the key DISCORD_TOKEN and paste your bot token as the value.

Test

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

  1. Go back to the Discord Developer Portal and select your application.
  2. Go to the "OAuth2" tab and then "URL Generator".
  3. Under "Scopes", select "bot".
  4. Under "Bot Permissions", select the permissions your bot needs (at minimum: "Send Messages", "Read Messages/View Channels").
  5. Copy the generated URL at the bottom of the page.
  6. Open this URL in a new tab and select the server you want to add the bot to.
  7. 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.

Technologies

Discord Automation & Bots Discord Automation & Bots

Similar templates

We found some blogs you might like...