Skip to content

Commit

Permalink
Add config to manage how long logs are stored (modmail-dev#3257)
Browse files Browse the repository at this point in the history
  • Loading branch information
raidensakura committed Dec 26, 2023
1 parent b23f5ee commit b1e4e6c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ async def on_ready(self):

self.post_metadata.start()
self.autoupdate.start()
self.log_expiry.start()
self._started = True

async def convert_emoji(self, name: str) -> str:
Expand Down Expand Up @@ -1772,6 +1773,18 @@ async def before_autoupdate(self):
logger.warning("Autoupdates disabled.")
self.autoupdate.cancel()
return

@tasks.loop(hours=1, reconnect=False)
async def log_expiry(self):
log_expire_after = self.config.get("log_expiration")
if log_expire_after == isodate.Duration():
return self.log_expiry.stop()

now = discord.utils.utcnow()
expiration_datetime = now - log_expire_after
expired_logs = await self.db.logs.delete_many({"closed_at": {"$lte": str(expiration_datetime)}})

logger.info(f"Deleted {expired_logs.deleted_count} expired logs.")

def format_channel_name(self, author, exclude_channel=None, force_null=False):
"""Sanitises a username for use with text channel names
Expand Down
3 changes: 2 additions & 1 deletion core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ConfigManager:
"account_age": isodate.Duration(),
"guild_age": isodate.Duration(),
"thread_cooldown": isodate.Duration(),
"log_expiration": isodate.Duration(),
"reply_without_command": False,
"anon_reply_without_command": False,
"plain_reply_without_command": False,
Expand Down Expand Up @@ -183,7 +184,7 @@ class ConfigManager:

colors = {"mod_color", "recipient_color", "main_color", "error_color"}

time_deltas = {"account_age", "guild_age", "thread_auto_close", "thread_cooldown"}
time_deltas = {"account_age", "guild_age", "thread_auto_close", "thread_cooldown", "log_expiration"}

booleans = {
"use_user_id_channel_name",
Expand Down
11 changes: 11 additions & 0 deletions core/config_help.json
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,17 @@
"To disable thread cooldown, do `{prefix}config del thread_cooldown`."
]
},
"log_expiration": {
"default": "Never",
"description": "The duration closed threads will be stored within the database before deletion. Logs that have been closed for longer than this duration will be deleted automatically.",
"examples": [
"`{prefix}config set log_expiration P12DT3H` (stands for 12 days and 3 hours in [ISO-8601 Duration Format](https://en.wikipedia.org/wiki/ISO_8601#Durations))",
"`{prefix}config set log_expiration 3 days and 5 hours` (accepted readable time)"
],
"notes": [
"To disable log expiration, do `{prefix}config del log_expiration`."
]
},
"thread_cancelled": {
"default": "\"Cancelled\"",
"description": "This is the message to display when a thread times out and creation is cancelled.",
Expand Down

0 comments on commit b1e4e6c

Please sign in to comment.