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
July 20, 2024

More templates like this

AlphaBot Financial Summary

AlphaBot Financial Summary: A Discord bot that utilizes the Alpha Vantage API to provide AI-generated summaries of income statements, balance sheets, and cash flow for renowned US companies.


Discord Moderation Bot

The Discord bot monitors all messages in the server. If a message contains profanity, the bot deletes it and sends a warning to the channel. The bot also notifies the host about the deletion via a direct message. Additionally, the bot outputs a helpful error message to a channel if there is a permissions error, guiding the server admin to enable the required permission in the Discord Developer portal.


Discord Bot

This powerful app skeleton is a great starting place for a Discord bot

Discord Clear Chat Bot