|
2 | 2 | import typing as t
|
3 | 3 | from abc import abstractmethod
|
4 | 4 | from collections.abc import Awaitable, Callable
|
| 5 | +from datetime import UTC, datetime, timedelta |
5 | 6 | from gettext import ngettext
|
6 | 7 |
|
7 | 8 | import arrow
|
8 | 9 | import dateutil.parser
|
9 | 10 | import discord
|
| 11 | +from async_rediscache import RedisCache |
10 | 12 | from discord.ext.commands import Context
|
11 | 13 | from pydis_core.site_api import ResponseCodeError
|
12 | 14 | from pydis_core.utils import scheduling
|
| 15 | +from pydis_core.utils.channel import get_or_fetch_channel |
13 | 16 |
|
14 | 17 | from bot import constants
|
15 | 18 | from bot.bot import Bot
|
|
24 | 27 |
|
25 | 28 | log = get_logger(__name__)
|
26 | 29 |
|
| 30 | +AUTOMATED_TIDY_UP_HOURS = 8 |
| 31 | + |
27 | 32 |
|
28 | 33 | class InfractionScheduler:
|
29 | 34 | """Handles the application, pardoning, and expiration of infractions."""
|
30 | 35 |
|
| 36 | + messages_to_tidy: RedisCache = RedisCache() |
| 37 | + |
31 | 38 | def __init__(self, bot: Bot, supported_infractions: t.Container[str]):
|
32 | 39 | self.bot = bot
|
33 | 40 | self.scheduler = scheduling.Scheduler(self.__class__.__name__)
|
| 41 | + self.tidy_up_scheduler = scheduling.Scheduler( |
| 42 | + f"{self.__class__.__name__}TidyUp" |
| 43 | + ) |
34 | 44 | self.supported_infractions = supported_infractions
|
35 | 45 |
|
36 | 46 | async def cog_unload(self) -> None:
|
37 | 47 | """Cancel scheduled tasks."""
|
38 | 48 | self.scheduler.cancel_all()
|
| 49 | + self.tidy_up_scheduler.cancel_all() |
39 | 50 |
|
40 | 51 | @property
|
41 | 52 | def mod_log(self) -> ModLog:
|
@@ -76,7 +87,46 @@ async def cog_load(self) -> None:
|
76 | 87 |
|
77 | 88 | self.scheduler.schedule_at(next_reschedule_point, -1, self.cog_load())
|
78 | 89 |
|
79 |
| - log.trace("Done rescheduling") |
| 90 | + log.trace("Done rescheduling expirations, scheduling tidy up tasks.") |
| 91 | + |
| 92 | + for key, expire_at in await self.messages_to_tidy.items(): |
| 93 | + channel_id, message_id = map(int, key.split(":")) |
| 94 | + expire_at = dateutil.parser.isoparse(expire_at) |
| 95 | + |
| 96 | + log.trace( |
| 97 | + "Scheduling tidy up for message %s in channel %s at %s", |
| 98 | + message_id, channel_id, expire_at |
| 99 | + ) |
| 100 | + |
| 101 | + self.tidy_up_scheduler.schedule_at( |
| 102 | + expire_at, |
| 103 | + message_id, |
| 104 | + self._delete_infraction_message(channel_id, message_id) |
| 105 | + ) |
| 106 | + |
| 107 | + async def _delete_infraction_message( |
| 108 | + self, |
| 109 | + channel_id: int, |
| 110 | + message_id: int |
| 111 | + ) -> None: |
| 112 | + """ |
| 113 | + Delete a message in the given channel. |
| 114 | +
|
| 115 | + This is used to delete infraction messages after a certain period of time. |
| 116 | + """ |
| 117 | + channel = await get_or_fetch_channel(self.bot, channel_id) |
| 118 | + if channel is None: |
| 119 | + log.warning(f"Channel {channel_id} not found for infraction message deletion.") |
| 120 | + return |
| 121 | + |
| 122 | + try: |
| 123 | + message = await channel.fetch_message(message_id) |
| 124 | + await message.delete() |
| 125 | + log.trace(f"Deleted infraction message {message_id} in channel {channel_id}.") |
| 126 | + except discord.NotFound: |
| 127 | + log.warning(f"Message {message_id} not found in channel {channel_id}.") |
| 128 | + |
| 129 | + await self.messages_to_tidy.delete(f"{channel_id}:{message_id}") |
80 | 130 |
|
81 | 131 | async def reapply_infraction(
|
82 | 132 | self,
|
@@ -269,7 +319,25 @@ async def apply_infraction(
|
269 | 319 | # Send a confirmation message to the invoking context.
|
270 | 320 | log.trace(f"Sending infraction #{id_} confirmation message.")
|
271 | 321 | mentions = discord.AllowedMentions(users=[user], roles=False)
|
272 |
| - await ctx.send(f"{dm_result}{confirm_msg}{infr_message}.", allowed_mentions=mentions) |
| 322 | + sent_msg = await ctx.send(f"{dm_result}{confirm_msg}{infr_message}.", allowed_mentions=mentions) |
| 323 | + |
| 324 | + if infraction["actor"] == self.bot.user.id: |
| 325 | + expire_message_time = datetime.now(UTC) + timedelta(hours=AUTOMATED_TIDY_UP_HOURS) |
| 326 | + |
| 327 | + log.trace(f"Scheduling message tidy for infraction #{id_} in {AUTOMATED_TIDY_UP_HOURS} hours.") |
| 328 | + |
| 329 | + # Schedule the message to be deleted after a certain period of time. |
| 330 | + self.tidy_up_scheduler.schedule_at( |
| 331 | + expire_message_time, |
| 332 | + sent_msg.id, |
| 333 | + self._delete_infraction_message(ctx.channel.id, sent_msg.id) |
| 334 | + ) |
| 335 | + |
| 336 | + # Persist to Redis to handle for bot restarts. |
| 337 | + await self.messages_to_tidy.set( |
| 338 | + f"{ctx.channel.id}:{sent_msg.id}", |
| 339 | + expire_message_time.isoformat(), |
| 340 | + ) |
273 | 341 |
|
274 | 342 | if jump_url is None:
|
275 | 343 | jump_url = "(Infraction issued in a ModMail channel.)"
|
|
0 commit comments