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
* Add config to change how long logs are stored

* Optimize deleting logs

* Update core/config_help.json

Co-authored-by: khakers <22665282+khakers@users.noreply.github.com>
Signed-off-by: Cordila <49218334+Cordila@users.noreply.github.com>

* Testing changes

* Update core/config_help.json (Fix copy paste)

Co-authored-by: Jerrie <70805800+Jerrie-Aries@users.noreply.github.com>
Signed-off-by: Cordila <49218334+Cordila@users.noreply.github.com>

---------

Signed-off-by: Cordila <49218334+Cordila@users.noreply.github.com>
Co-authored-by: Cordila <49218334+Cordila@users.noreply.github.com>
Co-authored-by: khakers <22665282+khakers@users.noreply.github.com>
Co-authored-by: Taku <45324516+Taaku18@users.noreply.github.com>
Co-authored-by: Jerrie <70805800+Jerrie-Aries@users.noreply.github.com>
  • Loading branch information
5 people authored and raidensakura committed Apr 10, 2024
1 parent ea06613 commit 614d37c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 12 deletions.
18 changes: 14 additions & 4 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
import platform
import typing
from datetime import datetime, timezone
from datetime import datetime, timezone, timedelta
from subprocess import PIPE
from types import SimpleNamespace

Expand Down Expand Up @@ -616,6 +616,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 All @@ -640,7 +641,6 @@ async def get_or_fetch_user(self, id: int) -> discord.User:
return self.get_user(id) or await self.fetch_user(id)

async def retrieve_emoji(self) -> typing.Tuple[str, str]:

sent_emoji = self.config["sent_emoji"]
blocked_emoji = self.config["blocked_emoji"]

Expand Down Expand Up @@ -714,7 +714,6 @@ def check_manual_blocked_roles(self, author: discord.Member) -> bool:
if isinstance(author, discord.Member):
for r in author.roles:
if str(r.id) in self.blocked_roles:

blocked_reason = self.blocked_roles.get(str(r.id)) or ""

try:
Expand Down Expand Up @@ -773,7 +772,6 @@ async def is_blocked(
channel: discord.TextChannel = None,
send_message: bool = False,
) -> bool:

member = self.guild.get_member(author.id)
if member is None:
# try to find in other guilds
Expand Down Expand Up @@ -1707,6 +1705,18 @@ async def before_autoupdate(self):
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
2 changes: 0 additions & 2 deletions cogs/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ async def unload_plugin(self, plugin: Plugin) -> None:
del sys.modules[module]

async def parse_user_input(self, ctx, plugin_name, check_version=False):

if not self.bot.config["enable_plugins"]:
embed = discord.Embed(
description="Plugins are disabled, enable them by setting `ENABLE_PLUGINS=true`",
Expand Down Expand Up @@ -399,7 +398,6 @@ async def plugins_add(self, ctx, *, plugin_name: str):
await self.bot.config.update()

if self.bot.config.get("enable_plugins"):

invalidate_caches()

try:
Expand Down
1 change: 0 additions & 1 deletion cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,6 @@ async def status(self, ctx, *, status_type: str.lower):
return await ctx.send(embed=embed)

async def set_presence(self, *, status=None, activity_type=None, activity_message=None):

if status is None:
status = self.bot.config.get("status")

Expand Down
4 changes: 2 additions & 2 deletions core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


class ConfigManager:

public_keys = {
# activity
"twitch_url": "https://www.twitch.tv/discordmodmail/",
Expand All @@ -37,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 @@ -187,7 +187,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
1 change: 0 additions & 1 deletion core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ async def convert(self, ctx, argument):
try:
return await super().convert(ctx, argument)
except commands.ChannelNotFound:

if guild:
categories = {c.name.casefold(): c for c in guild.categories}
else:
Expand Down
2 changes: 0 additions & 2 deletions core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,6 @@ async def delete_message(
async def find_linked_message_from_dm(
self, message, either_direction=False, get_thread_channel=False
) -> typing.List[discord.Message]:

joint_id = None
if either_direction:
joint_id = get_joint_id(message)
Expand Down Expand Up @@ -914,7 +913,6 @@ async def send(
persistent_note: bool = False,
thread_creation: bool = False,
) -> None:

if not note and from_mod:
self.bot.loop.create_task(self._restart_close_timer()) # Start or restart thread auto close

Expand Down

0 comments on commit 614d37c

Please sign in to comment.