Skip to content

Commit

Permalink
Add a simple duplication prevention mechanism
Browse files Browse the repository at this point in the history
On conduwuit at least, the sync() calls may sometimes trigger duplicate events, resulting in duplcated commands. This simple addition just records which event IDs are seen, adds them to a dequeue, and refuses to process duplicated ones.
  • Loading branch information
nexy7574 committed Oct 22, 2024
1 parent 16a639d commit fe1a547
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/niobot/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import collections
import functools
import getpass
import importlib
Expand Down Expand Up @@ -265,6 +266,8 @@ def __init__(
self._sync_full_state = sync_full_state
self.default_parse_mentions = default_parse_mentions

self._event_id_cache = collections.deque(maxlen=1000)

@property
def supported_server_versions(self) -> typing.List[typing.Tuple[int, int, int]]:
"""
Expand Down Expand Up @@ -407,9 +410,12 @@ async def update_read_receipts(self, room: U[str, nio.MatrixRoom], event: nio.Ev

async def process_message(self, room: nio.MatrixRoom, event: nio.RoomMessage) -> None:
"""Processes a message and runs the command it is trying to invoke if any."""
if event.event_id in self._event_id_cache:
self.log.warning("Not processing duplicate message event %r.", event.event_id)
return
if self.start_time is None:
raise RuntimeError("Bot has not started yet!")

self._event_id_cache.append(event.event_id)
self.message_cache.append((room, event))
self.dispatch("message", room, event)
if not isinstance(event, nio.RoomMessageText):
Expand Down

0 comments on commit fe1a547

Please sign in to comment.