by Lazy Sloth
Discord Bot That Deletes Messages
import os
import discord
import asyncio
from datetime import datetime, timedelta, timezone
from discord.ext import commands, tasks
from collections import defaultdict
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
channel_id = int(os.getenv('DISCORD_CHANNEL_ID')) # The specific channel ID to clear
clear_interval_seconds = None # No default clear interval
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
if clear_interval_seconds is not None:
clear_chat_interval.change_interval(seconds=clear_interval_seconds)
clear_chat_interval.start()
@bot.command()
async def set_clear_interval(ctx, seconds: int):
Frequently Asked Questions
How can this Discord Message Management Bot benefit my community server?
The Discord Message Management Bot can significantly improve the management and cleanliness of your community server. It allows administrators to easily remove spam, clear out old messages, and maintain a tidy chat environment. This can enhance user experience by keeping conversations relevant and reducing clutter. For instance, you can set up automatic clearing of messages after a certain time period, which is particularly useful for announcement or temporary information channels.
What are some practical applications of the delete_keyword_messages command in a business context?
The delete_keyword_messages command in the Discord Message Management Bot can be invaluable for businesses using Discord for customer support or internal communication. It can be used to: - Remove messages containing sensitive information that was accidentally shared - Clean up channels after a product launch or event, removing outdated information - Quickly delete spam messages that contain specific keywords - Remove messages with inappropriate content based on predefined keywords
How can the delete_duplicates feature improve user engagement in a community server?
The delete_duplicates feature of the Discord Message Management Bot can significantly enhance user engagement by: - Reducing clutter and repetition in chat channels - Making it easier for users to follow conversations without redundant messages - Preventing spam or accidental multiple postings of the same message - Improving the overall readability and quality of discussions This cleaner, more organized chat environment can lead to more meaningful interactions and higher user satisfaction.
How can I modify the Discord Message Management Bot to work with multiple channels instead of just one?
To modify the bot to work with multiple channels, you would need to change the channel_id
variable to a list of channel IDs and modify the relevant functions to iterate over this list. Here's an example of how you could modify the clear_chat_interval
function:
```python channel_ids = [int(id) for id in os.getenv('DISCORD_CHANNEL_IDS').split(',')]
@tasks.loop() async def clear_chat_interval(): if clear_interval_seconds is not None: for channel_id in channel_ids: channel = bot.get_channel(channel_id) if channel: time_threshold = datetime.now(timezone.utc) - timedelta(seconds=clear_interval_seconds) messages = [message async for message in channel.history(limit=None) if message.created_at.replace(tzinfo=timezone.utc) < time_threshold] await channel.delete_messages(messages) ```
You would need to make similar modifications to other functions in the Discord Message Management Bot that reference channel_id
.
How can I add a command to the Discord Message Management Bot that deletes messages within a specific date range?
You can add a new command to delete messages within a specific date range by creating a new function like this:
python
@bot.command()
async def delete_date_range(ctx, start_date: str, end_date: str):
if ctx.message.author.guild_permissions.administrator:
channel = bot.get_channel(channel_id)
if channel:
start = datetime.strptime(start_date, "%Y-%m-%d").replace(tzinfo=timezone.utc)
end = datetime.strptime(end_date, "%Y-%m-%d").replace(tzinfo=timezone.utc) + timedelta(days=1)
deleted = await channel.purge(after=start, before=end)
await ctx.send(f'{len(deleted)} messages between {start_date} and {end_date} have been deleted.')
else:
await ctx.send('You do not have administrative rights to delete messages.')
This new command in the Discord Message Management Bot would allow administrators to delete messages within a specific date range by using the command !delete_date_range YYYY-MM-DD YYYY-MM-DD
.
Created: | Last Updated:
Introduction to the Discord Bot That Deletes Messages Template
Welcome to the guide on how to use the Discord Bot That Deletes Messages template on Lazy. This template is designed to help you manage messages within a Discord channel by providing commands to set an automatic message clearing interval, remove the interval, delete messages from specific users or bots, remove duplicate messages, and delete messages containing a specific keyword. This bot is a powerful tool for Discord server administrators looking to keep their channels clean and organized.
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 use the bot, you'll need to set up two environment secrets within the Lazy Builder:
- DISCORD_BOT_TOKEN: This is the token of your Discord bot. You can obtain this token from the Discord Developer Portal where you created your bot.
- DISCORD_CHANNEL_ID: This is the ID of the Discord channel where messages will be managed. You can find this by enabling Developer Mode in your Discord settings, right-clicking on the channel, and selecting "Copy ID".
Enter these values in the Environment Secrets tab within the Lazy Builder.
Test: Pressing the Test Button
Once you have set up the environment secrets, press the "Test" button to deploy the app. The Lazy CLI will launch, and the bot will start running on the Lazy platform.
Entering Input: Filling in User Input
If the bot requires any user input, you will be prompted to provide it through the Lazy CLI after pressing the test button. Follow the prompts to enter the necessary information.
Using the App
After deployment, you can use the bot commands directly within your Discord server. For example, you can type !set_clear_interval 3600
to set the message clearing interval to one hour. The bot will respond to commands issued by users with administrative rights in the Discord server.
Integrating the App
If you wish to integrate this bot with other tools or services, you may need to provide the bot's server link or API endpoints. Since this template is designed to work within Discord, no additional integration steps are required unless you want to extend the bot's functionality with external services.
Remember, all the commands and functionalities provided by this bot are to be used within the Discord platform. There is no need to integrate it with external tools unless your specific use case requires it.
By following these steps, you should now have a fully functional Discord bot that can manage messages in your channel. Enjoy your cleaner and more organized Discord server!
Template Benefits
-
Automated Channel Maintenance: This bot can automatically clear messages at set intervals, helping to keep channels organized and clutter-free without manual intervention. This is particularly useful for high-traffic channels or those that require regular cleanup.
-
Enhanced Privacy and Security: The ability to delete messages containing specific keywords or from particular users can help businesses quickly remove sensitive information, inappropriate content, or potential security threats from their Discord channels.
-
Improved User Experience: By removing duplicate messages and managing channel content, the bot helps create a cleaner, more streamlined communication environment. This can lead to better user engagement and more effective information sharing.
-
Compliance Management: For businesses operating in regulated industries, the bot's message deletion capabilities can assist in maintaining compliance with data retention policies or privacy regulations by ensuring that certain types of information are not retained longer than necessary.
-
Customizable Moderation Tools: The bot provides administrators with flexible tools to manage channel content, allowing them to tailor moderation efforts to their specific needs. This can save time and resources compared to manual moderation, while still maintaining control over the channel's content.