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)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.command()
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']
try:
confirmation = await bot.wait_for('message', timeout=60.0, check=check)
except asyncio.TimeoutError:
await ctx.send('Confirmation not received. No messages were cleared.')
else:
Frequently Asked Questions
How can this Discord Clear Chat Bot benefit my community server?
The Discord Clear Chat Bot can be a valuable tool for maintaining a clean and organized server environment. It allows administrators to quickly remove all messages from a channel, which can be useful for: - Clearing out old discussions before starting a new topic - Removing spam or inappropriate content - Resetting channels for special events or announcements - Maintaining privacy by removing sensitive information after discussions
Are there any risks associated with using the Clear Chat Bot?
While the Discord Clear Chat Bot is designed with safety in mind, there are a few considerations: - Accidental deletion of important messages - Potential misuse if admin rights are not properly managed - Loss of chat history that might be needed for reference
To mitigate these risks, the bot includes a confirmation step and restricts the clear chat function to administrators only.
How can I customize the Clear Chat Bot for my server's specific needs?
The Discord Clear Chat Bot can be customized in several ways: - Modify the command prefix (currently set to '!') - Add additional commands or features - Implement logging of cleared messages - Customize the confirmation messages or timeout duration
To make these changes, you'll need to modify the main.py
file and potentially add new functions or event handlers.
How can I add a feature to log cleared messages before deletion?
You can add a logging feature to the Clear Chat Bot by modifying the clear_chat
function. Here's an example of how you might implement this:
```python import logging
# Set up logging logging.basicConfig(filename='cleared_messages.log', level=logging.INFO)
@bot.command() async def clear_chat(ctx): if ctx.message.author.guild_permissions.administrator: # ... (existing confirmation code) ... if confirmation.content == 'yes': messages = await ctx.channel.history(limit=None).flatten() for message in messages: logging.info(f"Cleared message: {message.author}: {message.content}") await ctx.channel.purge() # ... (rest of the function) ```
This modification will log all cleared messages to a file named 'cleared_messages.log' before deleting them.
How can I modify the Clear Chat Bot to clear messages from a specific user only?
You can add a new command to the Clear Chat Bot that clears messages from a specific user. Here's an example implementation:
python
@bot.command()
async def clear_user_chat(ctx, user: discord.Member, limit: int = 100):
if ctx.message.author.guild_permissions.administrator:
def is_user(m):
return m.author == user
deleted = await ctx.channel.purge(limit=limit, check=is_user)
await ctx.send(f'Deleted {len(deleted)} messages from {user.name}.')
else:
await ctx.send('You do not have administrative rights to clear chat history.')
This new command, !clear_user_chat
, takes a user mention and an optional limit parameter. It will delete up to the specified number of messages from the mentioned user in the current channel.
Created: | Last Updated:
Here's a step-by-step guide on how to use the Discord Clear Chat Bot template:
Introduction
This template provides a Discord bot that can clear all messages in a channel. The bot listens for a "!clear_chat" command from users with administrative privileges and then deletes the channel's messages after confirmation.
Getting Started
- Click "Start with this Template" to begin using this template in the Lazy Builder interface.
Initial Setup
- Set up the required environment secret:
- In the Lazy Builder, navigate to the Environment Secrets tab.
- Add a new secret with the key
DISCORD_BOT_TOKEN
. - To obtain your Discord bot token:
- Go to the Discord Developer Portal (https://discord.com/developers/applications).
- Create a new application or select an existing one.
- Navigate to the "Bot" section and click "Add Bot" if you haven't already.
- Under the bot's token, click "Copy" to copy your bot token.
- Paste the bot token as the value for the
DISCORD_BOT_TOKEN
secret in the Lazy Builder.
Test
- Click the "Test" button in the Lazy Builder to deploy and run the bot.
Using the Bot
- Once the bot is running, invite it to your Discord server:
- Go back to the Discord Developer Portal and select your application.
- Navigate to the "OAuth2" section.
- In the "Scopes" section, select "bot".
- In the "Bot Permissions" section, select "Administrator" (or at least "Manage Messages" and "Read Message History").
- Copy the generated OAuth2 URL and open it in a new browser tab.
-
Select the server you want to add the bot to and complete the authorization process.
-
Using the bot in your Discord server:
- In any channel where the bot is present, type
!clear_chat
. - If you have administrative privileges, the bot will ask for confirmation.
- Reply with "yes" to confirm and clear all messages in the channel, or "no" to cancel.
- If you don't have administrative privileges, the bot will inform you that you don't have the necessary rights.
Integrating the Bot
This bot doesn't require any additional integration steps. Once it's added to your Discord server and running, it's ready to use with the !clear_chat
command.
Remember that this bot has significant power to delete messages, so use it carefully and ensure that only trusted administrators have access to the command.
Here are 5 key business benefits for this Discord Clear Chat Bot template:
Template Benefits
-
Enhanced Privacy and Security: Allows administrators to quickly remove sensitive or confidential information from chat channels, reducing the risk of data breaches or information leaks.
-
Improved Channel Management: Enables easy maintenance of clean, organized channels by removing outdated or irrelevant messages, enhancing overall communication efficiency.
-
Compliance Support: Assists businesses in meeting regulatory requirements by providing a tool to remove data that should not be retained, supporting data minimization practices.
-
Time-Saving Automation: Automates the process of clearing chat history, saving administrators significant time compared to manual deletion of messages.
-
Customizable Moderation: Offers a foundation for building more complex moderation tools, allowing businesses to tailor the bot to their specific needs and policies for content management on Discord.