Skip to content

Commit 614d37c

Browse files
Atharv AgarwalCordilakhakersTaaku18Jerrie-Aries
authored andcommitted
Add config to manage how long logs are stored (modmail-dev#3257)
* Add config to change how long logs are stored * Optimize deleting logs * Update core/config_help.json Co-authored-by: khakers <[email protected]> Signed-off-by: Cordila <[email protected]> * Testing changes * Update core/config_help.json (Fix copy paste) Co-authored-by: Jerrie <[email protected]> Signed-off-by: Cordila <[email protected]> --------- Signed-off-by: Cordila <[email protected]> Co-authored-by: Cordila <[email protected]> Co-authored-by: khakers <[email protected]> Co-authored-by: Taku <[email protected]> Co-authored-by: Jerrie <[email protected]>
1 parent ea06613 commit 614d37c

File tree

7 files changed

+27
-12
lines changed

7 files changed

+27
-12
lines changed

bot.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import sys
1313
import platform
1414
import typing
15-
from datetime import datetime, timezone
15+
from datetime import datetime, timezone, timedelta
1616
from subprocess import PIPE
1717
from types import SimpleNamespace
1818

@@ -616,6 +616,7 @@ async def on_ready(self):
616616

617617
self.post_metadata.start()
618618
self.autoupdate.start()
619+
self.log_expiry.start()
619620
self._started = True
620621

621622
async def convert_emoji(self, name: str) -> str:
@@ -640,7 +641,6 @@ async def get_or_fetch_user(self, id: int) -> discord.User:
640641
return self.get_user(id) or await self.fetch_user(id)
641642

642643
async def retrieve_emoji(self) -> typing.Tuple[str, str]:
643-
644644
sent_emoji = self.config["sent_emoji"]
645645
blocked_emoji = self.config["blocked_emoji"]
646646

@@ -714,7 +714,6 @@ def check_manual_blocked_roles(self, author: discord.Member) -> bool:
714714
if isinstance(author, discord.Member):
715715
for r in author.roles:
716716
if str(r.id) in self.blocked_roles:
717-
718717
blocked_reason = self.blocked_roles.get(str(r.id)) or ""
719718

720719
try:
@@ -773,7 +772,6 @@ async def is_blocked(
773772
channel: discord.TextChannel = None,
774773
send_message: bool = False,
775774
) -> bool:
776-
777775
member = self.guild.get_member(author.id)
778776
if member is None:
779777
# try to find in other guilds
@@ -1707,6 +1705,18 @@ async def before_autoupdate(self):
17071705
self.autoupdate.cancel()
17081706
return
17091707

1708+
@tasks.loop(hours=1, reconnect=False)
1709+
async def log_expiry(self):
1710+
log_expire_after = self.config.get("log_expiration")
1711+
if log_expire_after == isodate.Duration():
1712+
return self.log_expiry.stop()
1713+
1714+
now = discord.utils.utcnow()
1715+
expiration_datetime = now - log_expire_after
1716+
expired_logs = await self.db.logs.delete_many({"closed_at": {"$lte": str(expiration_datetime)}})
1717+
1718+
logger.info(f"Deleted {expired_logs.deleted_count} expired logs.")
1719+
17101720
def format_channel_name(self, author, exclude_channel=None, force_null=False):
17111721
"""Sanitises a username for use with text channel names
17121722

cogs/plugins.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ async def unload_plugin(self, plugin: Plugin) -> None:
276276
del sys.modules[module]
277277

278278
async def parse_user_input(self, ctx, plugin_name, check_version=False):
279-
280279
if not self.bot.config["enable_plugins"]:
281280
embed = discord.Embed(
282281
description="Plugins are disabled, enable them by setting `ENABLE_PLUGINS=true`",
@@ -399,7 +398,6 @@ async def plugins_add(self, ctx, *, plugin_name: str):
399398
await self.bot.config.update()
400399

401400
if self.bot.config.get("enable_plugins"):
402-
403401
invalidate_caches()
404402

405403
try:

cogs/utility.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ async def status(self, ctx, *, status_type: str.lower):
584584
return await ctx.send(embed=embed)
585585

586586
async def set_presence(self, *, status=None, activity_type=None, activity_message=None):
587-
588587
if status is None:
589588
status = self.bot.config.get("status")
590589

core/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222

2323
class ConfigManager:
24-
2524
public_keys = {
2625
# activity
2726
"twitch_url": "https://www.twitch.tv/discordmodmail/",
@@ -37,6 +36,7 @@ class ConfigManager:
3736
"account_age": isodate.Duration(),
3837
"guild_age": isodate.Duration(),
3938
"thread_cooldown": isodate.Duration(),
39+
"log_expiration": isodate.Duration(),
4040
"reply_without_command": False,
4141
"anon_reply_without_command": False,
4242
"plain_reply_without_command": False,
@@ -187,7 +187,7 @@ class ConfigManager:
187187

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

190-
time_deltas = {"account_age", "guild_age", "thread_auto_close", "thread_cooldown"}
190+
time_deltas = {"account_age", "guild_age", "thread_auto_close", "thread_cooldown", "log_expiration"}
191191

192192
booleans = {
193193
"use_user_id_channel_name",

core/config_help.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,17 @@
373373
"To disable thread cooldown, do `{prefix}config del thread_cooldown`."
374374
]
375375
},
376+
"log_expiration": {
377+
"default": "Never",
378+
"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.",
379+
"examples": [
380+
"`{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))",
381+
"`{prefix}config set log_expiration 3 days and 5 hours` (accepted readable time)"
382+
],
383+
"notes": [
384+
"To disable log expiration, do `{prefix}config del log_expiration`."
385+
]
386+
},
376387
"thread_cancelled": {
377388
"default": "\"Cancelled\"",
378389
"description": "This is the message to display when a thread times out and creation is cancelled.",

core/models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ async def convert(self, ctx, argument):
394394
try:
395395
return await super().convert(ctx, argument)
396396
except commands.ChannelNotFound:
397-
398397
if guild:
399398
categories = {c.name.casefold(): c for c in guild.categories}
400399
else:

core/thread.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,6 @@ async def delete_message(
721721
async def find_linked_message_from_dm(
722722
self, message, either_direction=False, get_thread_channel=False
723723
) -> typing.List[discord.Message]:
724-
725724
joint_id = None
726725
if either_direction:
727726
joint_id = get_joint_id(message)
@@ -914,7 +913,6 @@ async def send(
914913
persistent_note: bool = False,
915914
thread_creation: bool = False,
916915
) -> None:
917-
918916
if not note and from_mod:
919917
self.bot.loop.create_task(self._restart_close_timer()) # Start or restart thread auto close
920918

0 commit comments

Comments
 (0)