Skip to content

Commit

Permalink
irc: use deque for "stack" of recent messages
Browse files Browse the repository at this point in the history
Doesn't save any code lines, but does make the process of updating what
has been said recently look a bit less voodoo-y. I very much prefer
declaring how long the list of "messages" should be where it's
initialized instead of pruning it later.
  • Loading branch information
dgw committed Jul 20, 2022
1 parent 90ecf45 commit 98660eb
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sopel/irc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from __future__ import annotations

import abc
from collections import deque
from datetime import datetime
import logging
import os
Expand Down Expand Up @@ -788,7 +789,7 @@ def say(
with self.sending:
recipient_id = self.make_identifier(recipient)
recipient_stack = self.stack.setdefault(recipient_id, {
'messages': [],
'messages': deque(maxlen=10),
'flood_left': flood_burst_lines,
})

Expand Down Expand Up @@ -836,7 +837,7 @@ def say(

# Loop detection
if antiloop_threshold > 0 and elapsed < antiloop_window:
messages = [m[1] for m in recipient_stack['messages'][-10:]]
messages = [m[1] for m in recipient_stack['messages']]

# If what we're about to send repeated at least N times
# in the anti-looping window, replace it
Expand All @@ -848,11 +849,10 @@ def say(

self.backend.send_privmsg(recipient, text)

# update recipient meta-data
# update recipient metadata
flood_left = recipient_stack['flood_left'] - 1
recipient_stack['flood_left'] = max(0, flood_left)
recipient_stack['messages'].append((time.time(), safe(text)))
recipient_stack['messages'] = recipient_stack['messages'][-10:]

# Now that we've sent the first part, we need to send the rest if
# requested. Doing so recursively seems simpler than iteratively.
Expand Down

0 comments on commit 98660eb

Please sign in to comment.