Discord Clear Chat Bot

 import os
import discord
from discord.ext import commands

intents = discord.Intents.all()
intents.members = True

bot = commands.Bot(command_prefix='!', intents=intents)

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

async def clear_chat(ctx):
    if ctx.message.author.guild_permissions.administrator:
        await ctx.send('Are you sure you want to clear all messages in this channel? Reply with "yes" to confirm or "no" to cancel.')
        def check(m):
            return m.author == ctx.message.author and m.content in ['yes', 'no']
            confirmation = await bot.wait_for('message', timeout=60.0, check=check)
        except asyncio.TimeoutError:
            await ctx.send('Confirmation not received. No messages were cleared.')

About this template

This bot will be designed to perform a single task on Discord which is to clear all messages in a channel. The bot listens for a "!clear_chat" command from a user with an admin role and then deletes the channel's chats. The bot requires one environment variable to function properly: DISCORD_BOT_TOKEN (the token of your Discord bot). The bot asks for confirmation from the user before clearing the chat. The user can cancel this action by replying with a "no". Only users with admin access can delete the messages. If the user is not an admin, the bot sends a message on the channel stating that the user does not have administrative rights to clear the chat history.

Introduction to the Discord Clear Chat Bot Template

Welcome to the step-by-step guide on how to set up and use the Discord Clear Chat Bot. This bot is designed to help administrators of Discord servers quickly clear all messages in a channel. It listens for a "!clear_chat" command and requires confirmation before proceeding to delete messages. This guide will walk you through the necessary steps to get this bot up and running on your server.

Setting Up Your Environment Secrets

Before you can start using the Discord Clear Chat Bot, you need to set up an environment secret that the bot will use to authenticate with Discord. This secret is your Discord bot token, which you can obtain from the Discord Developer Portal after creating your bot application.

  • Go to the Environment Secrets tab within the Lazy Builder interface.
  • Create a new secret with the key DISCORD_BOT_TOKEN.
  • Paste your Discord bot token as the value for this secret.

With your environment secret in place, the bot will be able to log in to Discord and perform its functions.

Using the Test Button

Once you have set up your environment secret, you can use the Test button to deploy your bot. This will launch the Lazy CLI, and if any additional user input is required, you will be prompted at this stage. After deployment, your bot should be online and ready to use on your Discord server.

Inviting the Bot to Your Discord Server

To use the bot, you must invite it to your Discord server. Follow these steps:

  • Go to the Discord Developer Portal where you created your bot.
  • Navigate to the OAuth2 page.
  • Under "Scopes," select "bot" and then choose the permissions your bot will need under "Bot Permissions." For the Clear Chat Bot, you will need to grant it permission to manage messages.
  • Use the generated URL to invite the bot to your server.

Using the Discord Clear Chat Bot

With the bot now part of your server, you can use the "!clear_chat" command in any text channel to initiate the message clearing process. Remember, only users with administrative rights can use this command. Here's how it works:

  • Type !clear_chat in the channel where you want to clear messages.
  • The bot will ask for confirmation. Reply with "yes" to proceed or "no" to cancel.
  • If you confirm with "yes," the bot will purge all messages in the channel.
  • If you do not respond within 60 seconds, or if you reply with "no," the bot will cancel the action.

That's all there is to it! You now have a functional Discord Clear Chat Bot to help maintain your server channels. If you encounter any issues or have questions, refer to the documentation provided with the bot or seek assistance from the Lazy support team.

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 Clear Chat Bot