#17254: fix(discord): intercept text-based slash commands instead of forwarding to agent (#16433)
channel: discord
size: XS
experienced-contributor
Cluster:
Signal and Discord Fixes
## Problem
When a user types `/reset`, `/new`, `/status`, etc. as plain text in Discord (instead of using the slash command picker), the message is forwarded to the agent session as regular text. The agent then interprets it creatively — potentially resetting workspaces, deleting files, or other destructive actions.
Fixes #16433
## Solution
Add a preflight check in `preflightDiscordMessage()` that detects text-based slash commands using the existing `hasControlCommand()` function and drops them before they reach the agent. Proper Discord slash command interactions (via the interaction system) are unaffected.
## Changes
- `src/discord/monitor/message-handler.preflight.ts` — 8 lines: check `hasControlCommand(baseText, params.cfg)` after text resolution, return `null` with verbose logging if matched
- Adjusted the preflight behavior to preserve legitimate status-reply flow (including `responsePrefix`) so `/status` handling still sends the expected prefixed response
## Testing
Existing Discord tests pass. The check uses the same `hasControlCommand` function that other channels rely on for slash command detection.
## Local Validation
- `pnpm build` ✅
- `pnpm check` (oxlint) ✅
- Relevant test suites pass ✅
## Contribution Checklist
- [x] Focused scope — single fix per PR
- [x] Clear "what" + "why" in description
- [x] AI-assisted (Codex/Claude) — reviewed and tested by human
- [x] Local validation run (`pnpm build && pnpm check`)
*AI-assisted (Claude). Reviewed by human.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Intercepts text-based slash commands (e.g. `/reset`, `/new`) in Discord guild channels so they are dropped before reaching the agent, preventing unintended destructive actions. DMs are excluded from interception so `/status` and `/help` continue to work. Also fixes a consistency gap in `createInboundDebouncer` where the non-debounced flush path was missing error handling.
- **Slash command interception**: Added `hasControlCommand()` check gated by `!isDirectMessage` in `preflightDiscordMessage()`, placed after text resolution but before channel activity recording
- **Debounce error handling**: Wrapped the non-debounced `onFlush` path in try/catch with `onError` callback, matching the existing pattern in `flushBuffer`
- Both changes are low-risk: the interception uses an existing detection function with strict command matching, and all production debouncer callers already provide `onError` handlers
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge — both changes are minimal, defensive, and use existing well-tested functions.
- Score of 5 reflects: (1) the preflight interception uses the existing `hasControlCommand()` function that other channels already rely on, (2) the `!isDirectMessage` guard correctly preserves DM command handling, (3) the debounce error handling fix mirrors an existing pattern already present in the same file, and (4) all production callers already provide the `onError` callback. The changes are well-scoped and introduce no new behavior beyond the intended fix.
- No files require special attention.
<sub>Last reviewed commit: 97e0a57</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#23158: discord: harden preflight/reply path against slow lookup latency
by danielstarman · 2026-02-22
81.5%
#20009: fix(discord): immediately defer interactions to prevent timeouts
by Gitjay11 · 2026-02-18
80.9%
#17513: fix(discord): respect groupPolicy in channel config fallback (#4555)
by aronchick · 2026-02-15
79.6%
#15900: fix(discord): filter bot's own messages early to prevent self-DoS
by Shuai-DaiDai · 2026-02-14
79.5%
#22940: fix(discord): detect voice messages for preflight transcription bef...
by aldoeliacim · 2026-02-21
78.0%
#17648: fix: Discord guild channel detection using rawMessage.guild_id
by MisterGuy420 · 2026-02-16
77.4%
#19615: fix(discord): include default account when sub-accounts are configured
by prue-starfield · 2026-02-18
77.4%
#21466: Copilot/fix forwarded message visibility
by stnguyen90 · 2026-02-20
77.2%
#21463: fix(discord): prevent WebSocket death spiral + fix numeric channel ID…
by akropp · 2026-02-20
76.8%
#20186: fix(discord): thread mediaLocalRoots through reply delivery path
by pvoo · 2026-02-18
76.8%