fix: make archived messages be in order

Refactored the archive command to collect messages before writing to the log file, ensuring chronological order. Updated server creation date formatting in serverinfo embed footer for better readability.
This commit is contained in:
neoarz
2025-09-15 14:45:00 -04:00
parent 26c28fcc7a
commit 2e0c8dafd0
2 changed files with 24 additions and 26 deletions

View File

@@ -47,7 +47,7 @@ class ServerInfo(commands.Cog, name="serverinfo"):
name=f"Roles ({len(context.guild.roles)})", name=f"Roles ({len(context.guild.roles)})",
value=roles value=roles
) )
embed.set_footer(text=f"Created at: {context.guild.created_at}") embed.set_footer(text=f"Created at: {context.guild.created_at.strftime('%m/%d/%Y')}")
if getattr(context, "interaction", None): if getattr(context, "interaction", None):
await context.interaction.response.send_message(embed=embed, ephemeral=True) await context.interaction.response.send_message(embed=embed, ephemeral=True)
@@ -55,5 +55,4 @@ class ServerInfo(commands.Cog, name="serverinfo"):
await context.send(embed=embed) await context.send(embed=embed)
async def setup(bot) -> None: async def setup(bot) -> None:
await bot.add_cog(ServerInfo(bot)) await bot.add_cog(ServerInfo(bot))

View File

@@ -1,11 +1,3 @@
"""
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 import os
from datetime import datetime from datetime import datetime
import discord import discord
@@ -34,28 +26,35 @@ class Archive(commands.Cog, name="archive"):
:param limit: The limit of messages that should be archived. Default is 10. :param limit: The limit of messages that should be archived. Default is 10.
""" """
log_file = f"{context.channel.id}.log" log_file = f"{context.channel.id}.log"
messages = []
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 ""
)
message_line = f"{message.created_at.strftime('%d.%m.%Y %H:%M:%S')} {message.author} {message.id}: {message.clean_content} {attachments_text}\n"
messages.append(message_line)
with open(log_file, "w", encoding="UTF-8") as f: with open(log_file, "w", encoding="UTF-8") as f:
f.write( 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' 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 for message_line in reversed(messages):
): f.write(message_line)
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) f = discord.File(log_file)
await context.send(file=f) await context.send(file=f)
os.remove(log_file) os.remove(log_file)
async def setup(bot) -> None: async def setup(bot) -> None:
await bot.add_cog(Archive(bot)) await bot.add_cog(Archive(bot))