Manually cache deleted messages

This commit is contained in:
Nexus 2023-04-18 17:42:01 +01:00
parent f99d8aab3a
commit 04a8f1d186
Signed by: nex
GPG key ID: 0FA334385D0B689F
2 changed files with 32 additions and 1 deletions

View file

@ -3,11 +3,41 @@ from datetime import datetime
import discord import discord
from discord.ext import commands from discord.ext import commands
from utils import Student, get_or_none, BannedStudentID, owner_or_admin, JimmyBans from utils import Student, get_or_none, BannedStudentID, owner_or_admin, JimmyBans
from typing import Sized
class LimitedList(list):
"""FIFO Limited list"""
def __init__(self, iterable: Sized = None, size: int = 5000):
if iterable:
assert len(iterable) <= size, "Initial iterable too big."
super().__init__(iterable or [])
self._max_size = size
def append(self, __object) -> None:
if len(self) + 1 >= self._max_size:
self.pop(0)
super().append(__object)
def __add__(self, other):
if len(other) > self._max_size:
raise ValueError("Other is too large")
elif len(other) == self._max_size:
self.clear()
super().__add__(other)
else:
for item in other:
self.append(item)
class Mod(commands.Cog): class Mod(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.cache = LimitedList()
@commands.Cog.listener()
async def on_message_delete(self, message: discord.Message):
self.cache.append(message)
@commands.user_command(name="Ban Account's B Number") @commands.user_command(name="Ban Account's B Number")
@discord.default_permissions(ban_members=True) @discord.default_permissions(ban_members=True)
@ -97,7 +127,7 @@ class Mod(commands.Cog):
@commands.command() @commands.command()
async def undelete(self, ctx: commands.Context, user: discord.User, *, query: str = None): async def undelete(self, ctx: commands.Context, user: discord.User, *, query: str = None):
"""Searches through the message cache to see if there's any deleted messages.""" """Searches through the message cache to see if there's any deleted messages."""
for message in self.bot.cached_messages: for message in self.cache:
message: discord.Message message: discord.Message
if message.author == user: if message.author == user:
query_ = query.lower() if query else None query_ = query.lower() if query else None

View file

@ -28,6 +28,7 @@ class Bot(commands.Bot):
debug_guilds=guilds, debug_guilds=guilds,
allowed_mentions=discord.AllowedMentions.none(), allowed_mentions=discord.AllowedMentions.none(),
intents=intents, intents=intents,
max_messages=100
) )
self.loop.run_until_complete(registry.create_all()) self.loop.run_until_complete(registry.create_all())
self.training_lock = Lock() self.training_lock = Lock()