Add a starboard purge to delete messages in background

This commit is contained in:
Zeva Rose 2022-06-13 20:57:15 -06:00
parent 7bbb1343a4
commit 406ab89b88

View file

@ -1,9 +1,11 @@
"""JARVIS Starboard Cog."""
import asyncio
import logging
from jarvis_core.db import q
from jarvis_core.db.models import Star, Starboard
from naff import Client, Extension, InteractionContext, Permissions
from naff.client import errors
from naff.models.discord.channel import GuildText
from naff.models.discord.components import ActionRow, Select, SelectOption
from naff.models.discord.message import Message
@ -35,6 +37,17 @@ class StarboardCog(Extension):
self.bot = bot
self.logger = logging.getLogger(__name__)
async def _purge_starboard(self, ctx: InteractionContext, board: Starboard) -> None:
channel = await ctx.guild.fetch_channel(board.channel)
async for star in Star.find(q(starboard=channel.id, guild=ctx.guild.id)):
if message := await channel.fetch_message(star.message):
try:
await message.delete()
await asyncio.sleep(1) # Avoid rate limits
except (errors.Forbidden, errors.NotFound):
self.logger.debug(f"Failed to delete star {star.id}'s message.")
await star.delete()
starboard = SlashCommand(name="starboard", description="Extra pins! Manage starboards")
@starboard.subcommand(
@ -100,6 +113,7 @@ class StarboardCog(Extension):
found = await Starboard.find_one(q(channel=channel.id, guild=ctx.guild.id))
if found:
await found.delete()
asyncio.create_task(self._purge_starboard(ctx, found))
await ctx.send(f"Starboard deleted from {channel.mention}.")
else:
await ctx.send(f"Starboard not found in {channel.mention}.", ephemeral=True)