2021-08-07 14:58:24 +02:00
|
|
|
from discord.ext import commands
|
2021-08-11 20:02:47 +02:00
|
|
|
from logging import getLogger
|
2022-11-09 14:10:46 +01:00
|
|
|
import typing
|
|
|
|
|
from datetime import timedelta
|
|
|
|
|
from discord import User, errors, TextChannel, Forbidden
|
2021-08-07 14:58:24 +02:00
|
|
|
|
|
|
|
|
# setup logging
|
2021-08-11 20:02:47 +02:00
|
|
|
log = getLogger(__name__)
|
|
|
|
|
|
2023-03-19 21:01:53 +01:00
|
|
|
|
|
|
|
|
class Admin(commands.Cog):
|
|
|
|
|
""" Admin Commands, Use at own risk. """
|
2021-08-07 14:58:24 +02:00
|
|
|
def __init__(self, bot):
|
|
|
|
|
self.bot = bot
|
2023-03-19 21:01:53 +01:00
|
|
|
|
|
|
|
|
|
2022-11-09 14:10:46 +01:00
|
|
|
@commands.command(name='purge', hidden=True)
|
2023-03-19 21:01:53 +01:00
|
|
|
@commands.has_permissions(administrator=True)
|
2022-11-09 14:10:46 +01:00
|
|
|
async def purge(self, ctx, amount=1):
|
|
|
|
|
""" Purge <n> messages from current channel"""
|
|
|
|
|
await ctx.message.delete()
|
|
|
|
|
await ctx.channel.purge(limit = amount)
|
|
|
|
|
log.warn(f'{ctx.message.author} purged {amount} messages')
|
|
|
|
|
|
2023-03-19 21:01:53 +01:00
|
|
|
|
2022-11-09 14:10:46 +01:00
|
|
|
@commands.command(name='purge_from', hidden=True)
|
2023-03-19 21:01:53 +01:00
|
|
|
@commands.has_permissions(administrator=True)
|
2022-11-09 14:10:46 +01:00
|
|
|
async def purge_from(self, ctx, message_id: int):
|
|
|
|
|
"""Purge all messages after the given message.id"""
|
|
|
|
|
try:
|
|
|
|
|
message = await ctx.channel.fetch_message(message_id)
|
|
|
|
|
except errors.NotFound:
|
|
|
|
|
log.warn(f'{ctx.message.author} tried purging {message_id}, but id was not found.')
|
|
|
|
|
return
|
|
|
|
|
await ctx.message.delete()
|
|
|
|
|
await ctx.channel.purge(after=message)
|
|
|
|
|
log.info(f'{ctx.message.author} purged all messages after {message_id}')
|
|
|
|
|
return True
|
|
|
|
|
|
2023-03-19 21:01:53 +01:00
|
|
|
|
2022-11-09 14:10:46 +01:00
|
|
|
@commands.command(name='purge_user', hidden=True)
|
|
|
|
|
@commands.is_owner()
|
|
|
|
|
async def purge_user(self, ctx, user: User, num_minutes: typing.Optional[int] = 5):
|
|
|
|
|
"""Clear all messages of <user.id> in all channels within the last [n=5] minutes"""
|
|
|
|
|
after = ctx.message.created_at - timedelta(minutes=num_minutes)
|
|
|
|
|
def check(msg):
|
|
|
|
|
return msg.author.id == user.id
|
|
|
|
|
for channel in await ctx.guild.fetch_channels():
|
|
|
|
|
if type(channel) is TextChannel:
|
|
|
|
|
try:
|
|
|
|
|
await channel.purge(limit=10*num_minutes, check=check, after=after)
|
|
|
|
|
log.info(f'{ctx.message.author} purged all messages from {user.id} that were posted within the last {num_minutes}')
|
|
|
|
|
except Forbidden:
|
|
|
|
|
continue
|
2023-03-19 21:01:53 +01:00
|
|
|
|
2022-11-09 14:10:46 +01:00
|
|
|
|
2021-08-07 14:58:24 +02:00
|
|
|
@commands.Cog.listener()
|
|
|
|
|
async def on_ready(self):
|
2021-08-11 20:02:47 +02:00
|
|
|
log.info(f'module active')
|
2021-08-07 14:58:24 +02:00
|
|
|
|
2023-03-19 21:01:53 +01:00
|
|
|
|
2022-11-09 14:10:46 +01:00
|
|
|
async def setup(bot):
|
|
|
|
|
await bot.add_cog(Admin(bot))
|