1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Administrator-py/extensions/purge.py

73 lines
2.5 KiB
Python
Raw Permalink Normal View History

2020-04-08 18:18:04 +02:00
from asyncio import sleep
from discord.ext import commands
from discord import Embed, RawReactionActionEvent
2021-02-03 23:06:45 +01:00
from discord_slash import SlashContext, cog_ext
2020-04-08 18:18:04 +02:00
2021-02-03 23:06:45 +01:00
from administrator import slash
from administrator.check import is_enabled, guild_only, has_permissions
from administrator.logger import logger
from administrator.utils import event_is_enabled
2020-04-08 18:18:04 +02:00
extension_name = "purge"
logger = logger.getChild(extension_name)
class Purge(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.purges = {}
2021-02-03 23:06:45 +01:00
slash.get_cog_commands(self)
2020-04-08 18:18:04 +02:00
def description(self):
return "Purge all messages between the command and the next add reaction"
2021-02-03 23:06:45 +01:00
@cog_ext.cog_slash(name="purge", description="Purge all message delimited by the command to your next reaction")
@is_enabled()
2021-02-03 23:06:45 +01:00
@guild_only()
@has_permissions(manage_messages=True)
async def purge(self, ctx: SlashContext):
message = await ctx.channel.send(content="\U0001f44d")
self.purges[ctx.author.id] = message
2020-07-23 21:30:42 +02:00
2021-02-03 23:06:45 +01:00
await sleep(2*60)
if ctx.author.id in self.purges and self.purges[ctx.author.id] == message:
await message.delete()
del self.purges[ctx.author.id]
2020-04-08 18:18:04 +02:00
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
2020-07-23 21:32:43 +02:00
if payload.guild_id:
if not event_is_enabled(self.qualified_name, payload.guild_id):
return
2020-07-23 21:32:43 +02:00
user = self.bot.get_user(payload.user_id)
message = await self.bot.get_guild(payload.guild_id).get_channel(payload.channel_id)\
.fetch_message(payload.message_id)
if user.id in self.purges:
if message.channel == self.purges[user.id].channel:
async with message.channel.typing():
2021-02-03 23:06:45 +01:00
await message.channel.purge(before=self.purges[user.id], after=message, limit=None)
2020-07-23 21:32:43 +02:00
await self.purges[user.id].delete()
await message.delete()
del self.purges[user.id]
2020-04-08 18:18:04 +02:00
def setup(bot):
logger.info(f"Loading...")
try:
bot.add_cog(Purge(bot))
except Exception as e:
logger.error(f"Error loading: {e}")
else:
logger.info(f"Load successful")
def teardown(bot):
logger.info(f"Unloading...")
try:
bot.remove_cog("Purge")
2020-04-08 18:18:04 +02:00
except Exception as e:
logger.error(f"Error unloading: {e}")
else:
logger.info(f"Unload successful")