Skip to content

Commit eaf7cfd

Browse files
committed
improve: support for user-friendly time input for snooze_default_duration
1 parent c680f67 commit eaf7cfd

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

core/config.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ class ConfigManager:
207207

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

210+
duration_seconds = {"snooze_default_duration"}
211+
210212
booleans = {
211213
"use_user_id_channel_name",
212214
"use_timestamp_channel_name",
@@ -369,6 +371,14 @@ def get(self, key: str, *, convert: bool = True) -> typing.Any:
369371
logger.warning("Invalid %s %s.", key, value)
370372
value = self.remove(key)
371373

374+
elif key in self.duration_seconds:
375+
if not isinstance(value, int):
376+
try:
377+
value = int(value)
378+
except (ValueError, TypeError):
379+
logger.warning("Invalid %s %s.", key, value)
380+
value = self.remove(key)
381+
372382
elif key in self.force_str:
373383
# Temporary: as we saved in int previously, leading to int32 overflow,
374384
# this is transitioning IDs to strings
@@ -453,6 +463,25 @@ async def set(self, key: str, item: typing.Any, convert=True) -> None:
453463
except ValueError:
454464
raise InvalidConfigError("Must be a yes/no value.")
455465

466+
elif key in self.duration_seconds:
467+
if isinstance(item, int):
468+
return self.__setitem__(key, item)
469+
try:
470+
converter = UserFriendlyTime()
471+
time = await converter.convert(None, str(item), now=discord.utils.utcnow())
472+
if time.arg:
473+
raise ValueError
474+
except BadArgument as exc:
475+
raise InvalidConfigError(*exc.args)
476+
except Exception as e:
477+
logger.debug(e)
478+
raise InvalidConfigError(
479+
"Unrecognized time, please use a duration like '5 days' or '2 hours'."
480+
)
481+
now = discord.utils.utcnow()
482+
duration_seconds = int((time.dt - now).total_seconds())
483+
return self.__setitem__(key, duration_seconds)
484+
456485
elif key in self.enums:
457486
if isinstance(item, self.enums[key]):
458487
# value is an enum type

0 commit comments

Comments
 (0)