Last active
December 14, 2023 20:34
-
-
Save macabreb0b/fbcb4e94db0dc9dd572529ea3f5d0672 to your computer and use it in GitHub Desktop.
discord autodelete bot script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sourced from https://pastebin.com/12UpbQHT | |
import discord | |
from discord.ext import tasks, commands | |
from datetime import datetime, timedelta | |
bot_token = "some-bot-token" | |
# replace with your channel ID | |
channel_id = 1111 | |
bot_prefix = "!" | |
bot = commands.Bot(command_prefix=bot_prefix, intents=discord.Intents.default()) | |
@bot.event | |
async def on_ready(): | |
print(f'We have logged in as {bot.user}') | |
delete_old_messages.start() | |
@tasks.loop(hours=1.0) | |
async def delete_old_messages(): | |
print('delete_old_messages start') | |
limit_time = datetime.now() - timedelta(days=1) | |
channel = bot.get_channel(channel_id) | |
if channel is not None: | |
try: | |
print(f"deleting messages posted before {limit_time}") | |
async for message in channel.history(limit=None): | |
if message.created_at.timestamp() < limit_time.timestamp(): | |
print(f"deleting message posted at {message.created_at}") | |
await message.delete() | |
except Exception as e: | |
print(f"Failed to delete message or messages in {channel.name} due to {e}") | |
else: | |
print(f"No channel found for channel_id: {channel_id}") | |
print('delete_old_messages end') | |
delete_old_messages.before_loop(bot.wait_until_ready) | |
bot.run(bot_token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment