From 54ff39f2095f50826b6fd5c58a3b68b3f4197004 Mon Sep 17 00:00:00 2001 From: Conatum Date: Tue, 12 Sep 2023 01:00:01 +0300 Subject: [PATCH] fix(timers): Improve option parsing. --- src/modules/pomodoro/options.py | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/modules/pomodoro/options.py b/src/modules/pomodoro/options.py index 7cb7f0b1..5428d97a 100644 --- a/src/modules/pomodoro/options.py +++ b/src/modules/pomodoro/options.py @@ -4,6 +4,7 @@ import discord from meta import LionBot from meta.errors import UserInputError +from utils.lib import replace_multiple from babel.translator import ctx_translator from settings import ModelData from settings.groups import SettingGroup, ModelConfig, SettingDotDict @@ -94,6 +95,19 @@ class TimerOptions(SettingGroup): def input_formatted(self): return str(self._data) if self._data is not None else '' + @classmethod + async def _parse_string(cls, parent_id, string, **kwargs): + try: + return await super()._parse_string(parent_id, string, **kwargs) + except UserInputError: + t = ctx_translator.get().t + raise UserInputError( + t(_p( + 'timerset:inactivity_length|desc', + "The inactivity threshold must be a positive whole number!" + )) + ) + @TimerConfig.register_model_setting class ManagerRole(ModelData, RoleSetting): setting_id = 'manager_role' @@ -173,6 +187,42 @@ class TimerOptions(SettingGroup): _model = TimerData.Timer _column = TimerData.Timer.channel_name.name + @classmethod + async def _parse_string(cls, parent_id, string, **kwargs): + # Enforce a length limit on a test-rendered string. + # TODO: Localised formatkey transformation + if string.lower() in ('', 'none', 'default'): + # Special cases for unsetting + return None + + testmap = { + '{remaining}': "10m", + '{name}': "Longish name", + '{stage}': "FOCUS", + '{members}': "25", + '{pattern}': "50/10", + } + testmapped = replace_multiple(string, testmap) + if len(testmapped) > 100: + t = ctx_translator.get().t + raise UserInputError( + t(_p( + 'timerset:channel_name_format|error:too_long', + "The provided name is too long! Channel names can be at most `100` characters." + )) + ) + else: + return string + + @classmethod + def _format_data(cls, parent_id, data, **kwargs): + """ + Overriding format to truncate displayed string. + """ + if data is not None and len(data) > 100: + data = data[:97] + '...' + return super()._format_data(parent_id, data, **kwargs) + @TimerConfig.register_model_setting class FocusLength(ModelData, DurationSetting): setting_id = 'focus_length'