Skip to content

Commit

Permalink
Add archive flag to purge
Browse files Browse the repository at this point in the history
  • Loading branch information
LightSage committed Oct 20, 2024
1 parent 9a664ee commit 95e4785
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lightning/cogs/mod/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import contextlib
from collections import Counter
from datetime import datetime, timedelta, timezone
from io import StringIO
from typing import TYPE_CHECKING, Annotated, List, Optional, Union

import discord
Expand Down Expand Up @@ -51,6 +52,18 @@ class ModContext(GuildContext):
config: Optional[GuildModConfig]


def create_message_archive(messages: List[discord.Message]):
fp = StringIO()
for message in messages:
if message.content:
fp.write(f"{message.created_at}: {message.author} ({message.author.id}): {message.content}\n")
else:
fp.write(f"{message.created_at}: {message.author} ({message.author.id}): Message contained embed or "
"attachment\n")
fp.seek(0)
return discord.File(fp, filename="archived.txt")


confirmations = {"ban": "{target} was banned. \N{THUMBS UP SIGN}",
"timeban": "{target} was banned. \N{THUMBS UP SIGN} It will expire in {expiry}.",
"kick": "{target} was kicked. \N{OK HAND SIGN}",
Expand Down Expand Up @@ -316,7 +329,11 @@ async def purge(self, ctx: GuildContext, search: commands.Range[int, 1, 300], *,
spam = sorted(spam.items(), key=lambda m: m[1], reverse=True)
messages.extend(f'{name}: {count}' for name, count in spam)
msg = '\n'.join(messages)
await ctx.send(msg, delete_after=40)

if flags.archive:
await ctx.send(msg, file=create_message_archive(purged))
else:
await ctx.send(msg, delete_after=40)

def can_timeout(self, ctx: ModContext, duration: datetime):
me = ctx.message.guild.me
Expand Down
3 changes: 3 additions & 0 deletions lightning/cogs/mod/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ class PurgeFlags(commands.FlagConverter, prefix="--", delimiter=""):
description="Search for messages after this message ID")
user: Optional[discord.Member] = commands.flag(default=None, description="Remove messages from the specified user")
bots: Optional[bool] = commands.flag(default=None, description="Removes messages from bots")
archive: Optional[bool] = commands.flag(default=False,
description="Whether to create an file with all the messages removed or "
"not")

0 comments on commit 95e4785

Please sign in to comment.