Skip to content

Commit

Permalink
Move rate_limit_info() from Rule to Sopel
Browse files Browse the repository at this point in the history
  • Loading branch information
SnoopJ committed Mar 26, 2023
1 parent bd4fe13 commit 3a7063b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
47 changes: 45 additions & 2 deletions sopel/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand Down
34 changes: 0 additions & 34 deletions sopel/plugins/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3a7063b

Please sign in to comment.