From 763f2551a95b597a4cdb46fc0c23dab05a1e3800 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 17 Dec 2022 20:44:23 +0300 Subject: [PATCH] Move snipe to moderation --- minearchy_bot/cogs/misc.py | 67 +----------------------------- minearchy_bot/cogs/moderation.py | 70 +++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 67 deletions(-) diff --git a/minearchy_bot/cogs/misc.py b/minearchy_bot/cogs/misc.py index ee7fd4d..f7011fd 100644 --- a/minearchy_bot/cogs/misc.py +++ b/minearchy_bot/cogs/misc.py @@ -1,6 +1,5 @@ from __future__ import annotations -from collections import defaultdict as DefaultDict, deque as Deque from datetime import timedelta as TimeDelta from inspect import cleandoc as strip from io import BytesIO @@ -8,8 +7,7 @@ from platform import python_version from time import monotonic as get_monotonic, time as get_time from typing import TYPE_CHECKING -from discord import CategoryChannel, Color, Embed, File, TextChannel -from discord.ext import commands +from discord import CategoryChannel, File from discord.ext.commands import Cog, command from discord.utils import escape_markdown @@ -30,7 +28,6 @@ class Miscellaneous( def __init__(self, bot: MinearchyBot) -> None: self.bot = bot self.bot.help_command.cog = self - self.sniped = DefaultDict(Deque) @override def cog_unload(self) -> None: @@ -149,68 +146,6 @@ class Miscellaneous( await message.author.edit(nick=message.author.display_name[5:]) await message.channel.send(f"Welcome back {message.author.mention}! I've unset your AFK status.") - @command( - brief="Sends the latest deleted messages.", - help=( - "Sends the last 5 deleted messages in a specified channel.\nIf the channel" - " isn't specified, it uses the current channel." - ), - ) - @commands.has_permissions(manage_messages=True) # needs to be able to delete messages to run the command - async def snipe(self, ctx: Context, channel: TextChannel | None = None) -> None: - if channel is None: - channel = ctx.channel - - logs = self.sniped[channel.id] - - if not logs: - await ctx.reply( - "There are no messages to be sniped in" - f" {'this channel.' if channel.id == ctx.channel.id else channel.mention}" - ) - return - - embed = Embed( - title=( - "Showing last 5 deleted messages for" - f" {'the current channel' if ctx.channel.id == channel.id else channel}" - ), - description="The lower the number is, the more recent it got deleted.", - color=Color.random(), - ) - - zwsp = "\uFEFF" - - for i, log in reversed(list(enumerate(logs))): - message, ts = log - - embed.add_field( - name=str(i) + ("" if i else " (latest)"), - value=strip( - f""" - Author: {message.author.mention} (ID: {message.author.id}, Plain: {escape_markdown(str(message.author))}) - Deleted at: (Relative: ) - Content: - ``` - {message.content.replace('`', f'{zwsp}`{zwsp}')} - ``` - """ - ), - inline=False, - ) - - await ctx.reply(embed=embed) - - @Cog.listener() - async def on_message_delete(self, message: Message) -> None: - if not message.guild: - return - - self.sniped[message.channel.id].appendleft((message, int(get_time()))) - - while len(self.sniped[message.channel.id]) > 5: - self.sniped[message.channel.id].pop() - async def setup(bot: MinearchyBot) -> None: await bot.add_cog(Miscellaneous(bot)) diff --git a/minearchy_bot/cogs/moderation.py b/minearchy_bot/cogs/moderation.py index c5f1b80..e9a7c04 100644 --- a/minearchy_bot/cogs/moderation.py +++ b/minearchy_bot/cogs/moderation.py @@ -1,13 +1,18 @@ from __future__ import annotations +from collections import defaultdict as DefaultDict, deque as Deque from datetime import timedelta as TimeDelta +from inspect import cleandoc as strip +from time import time as get_time from typing import TYPE_CHECKING +from discord import Color, Embed, TextChannel from discord.ext import commands from discord.ext.commands import Cog, command +from discord.utils import escape_markdown if TYPE_CHECKING: - from discord import Member + from discord import Member, Message from discord.ext.commands import Context from .. import MinearchyBot @@ -22,6 +27,7 @@ class Moderation(Cog): "m": "minutes", "s": "seconds", } + self.sniped = DefaultDict(Deque) @command( aliases=("mute",), @@ -50,6 +56,68 @@ class Moderation(Cog): await ctx.reply(f"Timed out {member.mention} for {time} {clean_time_name}.") + @command( + brief="Sends the latest deleted messages.", + help=( + "Sends the last 5 deleted messages in a specified channel.\nIf the channel" + " isn't specified, it uses the current channel." + ), + ) + @commands.has_permissions(manage_messages=True) # needs to be able to delete messages to run the command + async def snipe(self, ctx: Context, channel: TextChannel | None = None) -> None: + if channel is None: + channel = ctx.channel + + logs = self.sniped[channel.id] + + if not logs: + await ctx.reply( + "There are no messages to be sniped in" + f" {'this channel.' if channel.id == ctx.channel.id else channel.mention}" + ) + return + + embed = Embed( + title=( + "Showing last 5 deleted messages for" + f" {'the current channel' if ctx.channel.id == channel.id else channel}" + ), + description="The lower the number is, the more recent it got deleted.", + color=Color.random(), + ) + + zwsp = "\uFEFF" + + for i, log in reversed(list(enumerate(logs))): + message, ts = log + + embed.add_field( + name=str(i) + ("" if i else " (latest)"), + value=strip( + f""" + Author: {message.author.mention} (ID: {message.author.id}, Plain: {escape_markdown(str(message.author))}) + Deleted at: (Relative: ) + Content: + ``` + {message.content.replace('`', f'{zwsp}`{zwsp}')} + ``` + """ + ), + inline=False, + ) + + await ctx.reply(embed=embed) + + @Cog.listener() + async def on_message_delete(self, message: Message) -> None: + if not message.guild: + return + + self.sniped[message.channel.id].appendleft((message, int(get_time()))) + + while len(self.sniped[message.channel.id]) > 5: + self.sniped[message.channel.id].pop() + async def setup(bot: MinearchyBot) -> None: await bot.add_cog(Moderation(bot))