From 3a7063b8ae16525d5bfd68cc54b5881a1978636d Mon Sep 17 00:00:00 2001 From: James Gerity Date: Sun, 26 Mar 2023 15:41:53 -0400 Subject: [PATCH] Move rate_limit_info() from Rule to Sopel --- sopel/bot.py | 47 ++++++++++++++++++++++++++++++++++++++++-- sopel/plugins/rules.py | 34 ------------------------------ 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/sopel/bot.py b/sopel/bot.py index 6e9889399..782fd732e 100644 --- a/sopel/bot.py +++ b/sopel/bot.py @@ -39,7 +39,7 @@ from sopel.trigger import Trigger if TYPE_CHECKING: - from sopel.trigger import PreTrigger + from sopel.trigger import Trigger, PreTrigger __all__ = ['Sopel', 'SopelWrapper'] @@ -592,6 +592,49 @@ def register_urls(self, urls: Iterable) -> None: except plugins.exceptions.PluginError as err: LOGGER.error("Cannot register URL callback: %s", err) + def rate_limit_info(self, rule: plugin_rules.Rule, trigger: Trigger) -> plugin_rules.RateLimitCheckInfo: + if trigger.admin or rule.is_unblockable(): + return plugin_rules.RateLimitCheckInfo() + + # 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) + 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) + rate_limit_sec = rule._channel_rate_limit + elif rule.is_global_rate_limited(): + template = rule.get_global_rate_message(trigger.nick) + rate_limit_sec = rule._global_rate_limit + else: + return plugin_rules.RateLimitCheckInfo() + + # TODO: add: + # [ ] time_left + # [ ] time_left_sec + # [ ] rate_limit + # [x] rate_limit_type + # [x] command (label) + # [x] plugin + # [ ] ??? + # TODO: tests for the above + channel = trigger.sender if not trigger.sender.is_nick() else None + + if template: + message = template.format( + nick=trigger.nick, + channel=channel, + sender=trigger.sender, + plugin=rule.get_plugin_name(), + label=rule.get_rule_label(), + rate_limit_sec=rate_limit_sec, + ) + else: + message = None + + return plugin_rules.RateLimitCheckInfo(message=message, limited=True) + + # message dispatch def call_rule( @@ -604,7 +647,7 @@ def call_rule( context = trigger.sender is_channel = context and not context.is_nick() - limited, limit_msg = rule.rate_limit_info(trigger) + limited, limit_msg = self.rate_limit_info(rule, trigger) if limit_msg: sopel.notice(limit_msg, destination=nick) if limited: diff --git a/sopel/plugins/rules.py b/sopel/plugins/rules.py index db43a2f6c..98b2edb32 100644 --- a/sopel/plugins/rules.py +++ b/sopel/plugins/rules.py @@ -1166,40 +1166,6 @@ 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 rate_limit_info(self, trigger: Trigger) -> RateLimitCheckInfo: - if trigger.admin or self.is_unblockable(): - return RateLimitCheckInfo() - - # TODO: these functions call now() and probably shouldn't - if self.is_user_rate_limited(trigger.nick): - template = self.get_user_rate_message(trigger.nick) - elif self.is_channel_rate_limited(trigger.sender): - template = self.get_channel_rate_message(trigger.nick, trigger.sender) - elif self.is_global_rate_limited(): - template = self.get_global_rate_message(trigger.nick) - else: - return RateLimitCheckInfo() - - # TODO: add: - # * time_left - # * time_left_sec - # * rate_limit - # * rate_limit_type - # * command - # * plugin - # * ??? - if template: - message = template.format( - nick=trigger.nick, - # TODO: add is_channel() to trigger? - channel=trigger.sender if not trigger.sender.is_nick() else None, - sender=trigger.sender, - ) - else: - message = None - - return RateLimitCheckInfo(message=message, limited=True) - def get_user_rate_message(self, nick): template = self._user_rate_message or self._default_rate_message