Files
Syntrel/cogs/moderation/archive.py

62 lines
2.3 KiB
Python
Raw Normal View History

2025-09-14 18:54:56 -04:00
"""
Copyright © Krypton 2019-Present - https://github.com/kkrypt0nn (https://krypton.ninja)
Description:
🐍 A simple template to start to code your own and personalized Discord bot in Python
Version: 6.4.0
"""
import os
from datetime import datetime
import discord
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
class Archive(commands.Cog, name="archive"):
def __init__(self, bot) -> None:
self.bot = bot
@commands.hybrid_command(
name="archive",
description="Archives in a text file the last messages with a chosen limit of messages.",
)
@commands.has_permissions(manage_messages=True)
@app_commands.describe(
limit="The limit of messages that should be archived.",
)
async def archive(self, context: Context, limit: int = 10) -> None:
"""
Archives in a text file the last messages with a chosen limit of messages. This command requires the MESSAGE_CONTENT intent to work properly.
:param context: The hybrid command context.
:param limit: The limit of messages that should be archived. Default is 10.
"""
log_file = f"{context.channel.id}.log"
with open(log_file, "w", encoding="UTF-8") as f:
f.write(
f'Archived messages from: #{context.channel} ({context.channel.id}) in the guild "{context.guild}" ({context.guild.id}) at {datetime.now().strftime("%d.%m.%Y %H:%M:%S")}\n'
)
async for message in context.channel.history(
limit=limit, before=context.message
):
attachments = []
for attachment in message.attachments:
attachments.append(attachment.url)
attachments_text = (
f"[Attached File{'s' if len(attachments) >= 2 else ''}: {', '.join(attachments)}]"
if len(attachments) >= 1
else ""
)
f.write(
f"{message.created_at.strftime('%d.%m.%Y %H:%M:%S')} {message.author} {message.id}: {message.clean_content} {attachments_text}\n"
)
f = discord.File(log_file)
await context.send(file=f)
os.remove(log_file)
async def setup(bot) -> None:
await bot.add_cog(Archive(bot))