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 = ''

async def on_ready():
    print(f'Logged in as {bot.user.name}')

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!')

About this template

This 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.

Introduction to the Discord Join and Leave Bot Template

Welcome to the step-by-step guide on how to set up and use the Discord Join and Leave Bot using the Lazy platform. This bot is designed to send automated welcome messages to new members when they join a Discord server and farewell messages when they leave. This guide will walk you through the process of setting up the bot, from starting with the template to integrating it into your Discord server.

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button on the Lazy platform. 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 use the bot, you need to set up an environment secret within the Lazy Builder. This secret is the 'DISCORD_TOKEN', which is necessary for the bot to interact with your Discord server.

  1. Go to the Discord Developer Portal and create a new application.
  2. Inside the application, navigate to the "Bot" section and add a new bot.
  3. Copy the bot's token provided in this section.
  4. Back in the Lazy Builder, go to the Environment Secrets tab.
  5. Create a new secret with the key 'DISCORD_TOKEN' and paste the token you copied as the value.

With the 'DISCORD_TOKEN' set, your bot is now ready to be deployed.

Test: Pressing the Test Button

Once you have set up the environment secret, press the "Test" button on the Lazy platform. This will begin the deployment of the bot and launch the Lazy CLI. The bot will start running, and you will see a confirmation in the CLI that it is logged in and ready.

Entering Input: Filling in User Input

There is no additional user input required through the CLI for this template. All configurations can be done through Discord commands once the bot is running.

Using the App

After deploying the bot, you can use it directly within your Discord server. Here's how to set up the welcome and leave channels:

  1. Invite the bot to your Discord server using the OAuth2 URL generated in the Discord Developer Portal.
  2. In Discord, use the command !set_welcome_channel #channel-name to set the channel where welcome messages will be sent.
  3. Similarly, use the command !set_leave_channel #channel-name to set the channel for leave messages.

Replace #channel-name with the actual channel you want to use for each type of message. Once set, the bot will automatically send welcome and leave messages in the specified channels.

Integrating the App

No further external integration is required for this template. The bot will function within your Discord server, and you can manage its settings using the provided commands.

Remember to ensure that the bot has the necessary permissions in your Discord server to read and send messages in the channels you have designated for welcome and leave messages.

That's it! You now have a fully functional Discord Join and Leave Bot running in your server, thanks to the Lazy platform.

Last published
April 7, 2024

More templates like this

WhatsApp ChatBot (Twilio, InstantChat)

Build a WhatApp chatbot using Twilio, Discord and LazyAI.



GreetingBot: A discord bot that welcomes new members to your server.


Stock AI Advice

The bot requires certain permissions to function properly. These include the ability to read message history, send messages, and react to messages. The bot will generate stats such as This bot will provide ticker stats, commodity stats, Stock News and other AI Stock Trading Advice Please provide the Discord bot token in the Env Secrets tab under the name 'DISCORD_BOT_TOKEN' and your API Key for the Alpha Advantage

Discord Join and Leave Bot