Summary
The backlog replay logic in bot.py (on_ready) has two related problems that can cause missed messages and duplicate replies.
Problem 1 – History scan limit drops older messages
At line 114, only the last 50 messages are scanned to find the bot's last reply cursor (last_bot_msg). If the bot was offline long enough that its last reply is beyond the 50-message window, last_bot_msg is None and the fallback (lines 126–129) processes only the last 5 user messages, silently dropping any older unprocessed ones.
Problem 2 – Duplicate replies when on_message is active during replay
The on_message event handler is active while the backlog is being replayed in on_ready. If a new message arrives (or if history iteration yields a message that also triggers on_message), the same message can be forwarded to Ollama and replied to twice.
Suggested fix
- Persist a durable checkpoint — store
last_processed_message_id to disk (e.g., a small JSON/SQLite file) after each successful process_message call instead of relying on scanning chat history for the last bot message.
- Use the checkpoint for history fetch — call
channel.history(after=last_processed_message_id, oldest_first=True) (or from the beginning if no checkpoint exists) with no artificial small limit.
- Add message-id deduplication in
process_message (maintain a set of recently processed IDs) or temporarily gate on_message while the replay loop runs to prevent double-processing.
References
Summary
The backlog replay logic in
bot.py(on_ready) has two related problems that can cause missed messages and duplicate replies.Problem 1 – History scan limit drops older messages
At line 114, only the last 50 messages are scanned to find the bot's last reply cursor (
last_bot_msg). If the bot was offline long enough that its last reply is beyond the 50-message window,last_bot_msgisNoneand the fallback (lines 126–129) processes only the last 5 user messages, silently dropping any older unprocessed ones.Problem 2 – Duplicate replies when
on_messageis active during replayThe
on_messageevent handler is active while the backlog is being replayed inon_ready. If a new message arrives (or if history iteration yields a message that also triggerson_message), the same message can be forwarded to Ollama and replied to twice.Suggested fix
last_processed_message_idto disk (e.g., a small JSON/SQLite file) after each successfulprocess_messagecall instead of relying on scanning chat history for the last bot message.channel.history(after=last_processed_message_id, oldest_first=True)(or from the beginning if no checkpoint exists) with no artificial small limit.process_message(maintain a set of recently processed IDs) or temporarily gateon_messagewhile the replay loop runs to prevent double-processing.References