#16801: fix: Register Discord listeners before gateway connects
channel: discord
stale
size: S
trusted-contributor
Cluster:
Signal and Discord Fixes
## Summary
This fix addresses a race condition where MESSAGE_CREATE events could be missed if the gateway connected before Discord event listeners were registered. This issue is especially problematic on ARM64 platforms like Raspberry Pi where timing behaves differently.
The fix ensures all Discord event listeners are created and passed to the Client constructor BEFORE the gateway plugin connects to Discord WebSocket.
## Changes
- Create message handler, logger, and guildHistories before Client initialization
- Create all event listeners before creating the Client
- Pass all listeners to the Client constructor
## Testing
- TypeScript compilation check passed
- Code follows existing patterns
Fixes openclaw/openclaw#16781
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes a race condition where Discord MESSAGE_CREATE events could be missed on ARM64 platforms (like Raspberry Pi) by ensuring all event listeners are registered before the Client constructor is called, which triggers the gateway connection. The core refactoring moves listener creation earlier in the initialization sequence and passes them to the Client constructor upfront.
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with minimal risk, though one design choice regarding botUserId warrants attention
- The refactoring successfully addresses the race condition by reordering initialization. The code follows existing patterns and TypeScript compilation passes. However, the deliberate choice to not update botUserId after fetching (lines 592-595) creates a potential edge case where the bot could respond to its own messages in certain configurations, though this is mitigated by the allowBots check
- No files require special attention - the refactoring is straightforward
<sub>Last reviewed commit: 3882589</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#15900: fix(discord): filter bot's own messages early to prevent self-DoS
by Shuai-DaiDai · 2026-02-14
79.2%
#17648: fix: Discord guild channel detection using rawMessage.guild_id
by MisterGuy420 · 2026-02-16
79.0%
#21463: fix(discord): prevent WebSocket death spiral + fix numeric channel ID…
by akropp · 2026-02-20
77.6%
#22611: fix(discord): allow messages from other instance bots in multi-acco...
by dae-sun · 2026-02-21
77.0%
#16736: fix: stagger multi-account channel startup to avoid Discord rate li...
by rm289 · 2026-02-15
76.8%
#10731: fix(discord): add outer retry loop for gateway reconnect exhaustion
by Milofax · 2026-02-06
75.8%
#7585: Tests: fix lint in discord auto-thread race test
by justinhuangcode · 2026-02-03
75.8%
#19615: fix(discord): include default account when sub-accounts are configured
by prue-starfield · 2026-02-18
75.3%
#23158: discord: harden preflight/reply path against slow lookup latency
by danielstarman · 2026-02-22
75.2%
#16720: fix: fall back to guild_id when guild object is missing in resolveD...
by xqliu · 2026-02-15
74.9%