diff --git a/sopel/bot.py b/sopel/bot.py index 782fd732e..607b33d52 100644 --- a/sopel/bot.py +++ b/sopel/bot.py @@ -598,13 +598,13 @@ def rate_limit_info(self, rule: plugin_rules.Rule, trigger: Trigger) -> plugin_r # TODO: these functions call now() and probably shouldn't if rule.is_user_rate_limited(trigger.nick): - template = rule.get_user_rate_message(trigger.nick) + template = rule.user_rate_template rate_limit_sec = rule._user_rate_limit elif rule.is_channel_rate_limited(trigger.sender): - template = rule.get_channel_rate_message(trigger.nick, trigger.sender) + template = rule.channel_rate_template rate_limit_sec = rule._channel_rate_limit elif rule.is_global_rate_limited(): - template = rule.get_global_rate_message(trigger.nick) + template = rule.global_rate_template rate_limit_sec = rule._global_rate_limit else: return plugin_rules.RateLimitCheckInfo() diff --git a/sopel/plugins/rules.py b/sopel/plugins/rules.py index 98b2edb32..0c759b035 100644 --- a/sopel/plugins/rules.py +++ b/sopel/plugins/rules.py @@ -754,38 +754,33 @@ def is_global_rate_limited(self) -> bool: :return: ``True`` when the rule reached the limit, ``False`` otherwise """ + @property @abc.abstractmethod - def get_user_rate_message(self, nick: Identifier) -> Optional[str]: - """Give the message to send with a NOTICE to ``nick``. + def user_rate_template(self) -> Optional[str]: + """Give the message template to send with a NOTICE to ``nick``. - :param nick: the nick that is rate limited :return: A formatted string, or ``None`` if no message is set. - This method is called by the bot when a trigger hits the user rate + This property is accessed by the bot when a trigger hits the user rate limit (i.e. for the specificed ``nick``). """ + @property @abc.abstractmethod - def get_channel_rate_message( - self, - nick: Identifier, - channel: Identifier, - ) -> Optional[str]: - """Give the message to send with a NOTICE to ``nick``. + def channel_rate_template(self) -> Optional[str]: + """Give the message template to send with a NOTICE to ``nick``. - :param nick: the nick that reached the channel's rate limit - :param channel: the channel that is rate limited :return: A formatted string, or ``None`` if no message is set. This method is called by the bot when a trigger hits the channel rate limit (i.e. for the specificed ``channel``). """ + @property @abc.abstractmethod - def get_global_rate_message(self, nick: Identifier) -> Optional[str]: + def global_rate_template(self) -> Optional[str]: """Give the message to send with a NOTICE to ``nick``. - :param nick: the nick that reached the global rate limit :return: A formatted string, or ``None`` if no message is set. This method is called by the bot when a trigger hits the global rate @@ -1166,28 +1161,19 @@ def is_global_rate_limited(self): rate_limit = datetime.timedelta(seconds=self._global_rate_limit) return self._metrics_global.is_limited(now - rate_limit) - def get_user_rate_message(self, nick): - template = self._user_rate_message or self._default_rate_message - - if not template: - return None - + @property + def user_rate_template(self): + template = self._user_rate_message or self._default_rate_message or None return template - def get_channel_rate_message(self, nick, channel): - template = self._channel_rate_message or self._default_rate_message - - if not template: - return None - + @property + def channel_rate_template(self): + template = self._channel_rate_message or self._default_rate_message or None return template - def get_global_rate_message(self, nick): - template = self._global_rate_message or self._default_rate_message - - if not template: - return None - + @property + def global_rate_template(self): + template = self._global_rate_message or self._default_rate_message or None return template def execute(self, bot, trigger): diff --git a/test/plugins/test_plugins_rules.py b/test/plugins/test_plugins_rules.py index 52a2686b0..dd121544a 100644 --- a/test/plugins/test_plugins_rules.py +++ b/test/plugins/test_plugins_rules.py @@ -1649,13 +1649,9 @@ def handler(bot, trigger): global_rate_message='Server message: {nick}', default_rate_message='Default message: {nick}', ) - assert rule.get_user_rate_message(mocktrigger.nick) == 'User message: {nick}' - assert rule.get_channel_rate_message( - mocktrigger.nick, mocktrigger.sender - ) == 'Channel message: {nick}/{channel}' - assert rule.get_global_rate_message( - mocktrigger.nick - ) == 'Server message: {nick}' + assert rule.user_rate_template == 'User message: {nick}' + assert rule.channel_rate_template == 'Channel message: {nick}/{channel}' + assert rule.global_rate_template == 'Server message: {nick}' def test_rule_rate_limit_messages_default(mockbot, triggerfactory): @@ -1675,10 +1671,9 @@ def handler(bot, trigger): channel_rate_limit=20, default_rate_message='Default message', ) - assert rule.get_user_rate_message(mocktrigger.nick) == 'Default message' - assert rule.get_channel_rate_message( - mocktrigger.nick, mocktrigger.sender) == 'Default message' - assert rule.get_global_rate_message(mocktrigger.nick) == 'Default message' + assert rule.user_rate_template == 'Default message' + assert rule.channel_rate_template == 'Default message' + assert rule.global_rate_template == 'Default message' def test_rule_rate_limit_messages_default_mixed(mockbot, triggerfactory): @@ -1699,10 +1694,9 @@ def handler(bot, trigger): user_rate_message='User message.', default_rate_message='The default.', ) - assert rule.get_user_rate_message(mocktrigger.nick) == 'User message.' - assert rule.get_channel_rate_message( - mocktrigger.nick, mocktrigger.sender) == 'The default.' - assert rule.get_global_rate_message(mocktrigger.nick) == 'The default.' + assert rule.user_rate_template == 'User message.' + assert rule.channel_rate_template == 'The default.' + assert rule.global_rate_template == 'The default.' rule = rules.Rule( [regex], @@ -1713,10 +1707,9 @@ def handler(bot, trigger): channel_rate_message='Channel message.', default_rate_message='The default.', ) - assert rule.get_user_rate_message(mocktrigger.nick) == 'The default.' - assert rule.get_channel_rate_message( - mocktrigger.nick, mocktrigger.sender) == 'Channel message.' - assert rule.get_global_rate_message(mocktrigger.nick) == 'The default.' + assert rule.user_rate_template == 'The default.' + assert rule.channel_rate_template == 'Channel message.' + assert rule.global_rate_template == 'The default.' rule = rules.Rule( [regex], @@ -1727,10 +1720,9 @@ def handler(bot, trigger): global_rate_message='Server message.', default_rate_message='The default.', ) - assert rule.get_user_rate_message(mocktrigger.nick) == 'The default.' - assert rule.get_channel_rate_message( - mocktrigger.nick, mocktrigger.sender) == 'The default.' - assert rule.get_global_rate_message(mocktrigger.nick) == 'Server message.' + assert rule.user_rate_template == 'The default.' + assert rule.channel_rate_template == 'The default.' + assert rule.global_rate_template == 'Server message.' # -----------------------------------------------------------------------------