Skip to content

fix(discord): use guild_id as source of truth for DM classification#34416

Closed
Sid-Qin wants to merge 1 commit intoopenclaw:mainfrom
Sid-Qin:fix/discord-requiremention-regression-34353
Closed

fix(discord): use guild_id as source of truth for DM classification#34416
Sid-Qin wants to merge 1 commit intoopenclaw:mainfrom
Sid-Qin:fix/discord-requiremention-regression-34353

Conversation

@Sid-Qin
Copy link
Contributor

@Sid-Qin Sid-Qin commented Mar 4, 2026

Summary

  • Problem: Discord requireMention: true stopped working — the agent responds and shows typing on every guild message even when not mentioned. The root cause is that isDirectMessage was derived solely from channelInfo?.type === ChannelType.DM, ignoring params.data.guild_id. When channelInfo returned ChannelType.DM for a guild channel (stale cache, API inconsistency, or Carbon library change), guild messages were classified as DMs, bypassing the requireMention gate entirely.
  • Why it matters: Every Discord guild deployment with requireMention: true is broken — the bot responds to every message, flooding channels.
  • What changed: In message-handler.preflight.ts, isDirectMessage and isGroupDm now require !isGuildMessage as a precondition. If guild_id is present, the message is never classified as DM or GroupDM — mirroring the existing pattern in agent-components.ts (line 214-217).
  • What did NOT change (scope boundary): resolveDiscordChannelInfo, resolveMentionGatingWithBypass, guild config resolution, and all other preflight checks are untouched.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

User-visible / Behavior Changes

Guild messages without an explicit bot mention are correctly dropped when requireMention: true is configured, restoring pre-regression behavior.

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

Environment

  • OS: macOS / Linux
  • Runtime: Node.js 22+

Steps

  1. Configure Discord guild with requireMention: true in guilds.<guildId>.
  2. Send a message in a guild channel without mentioning the bot.
  3. Observe the agent behavior.

Expected

  • Agent ignores the message (no typing, no response).

Actual

  • Before: Agent shows typing and responds to every message regardless of mention.
  • After: Agent correctly ignores messages without explicit mention.

Evidence

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

New test: "drops guild messages without mention even when channelInfo reports DM type (stale cache)" — simulates a guild message where channelInfo.type incorrectly returns ChannelType.DM, verifies the message is still dropped when requireMention: true.

Human Verification (required)

  • Verified scenarios: Guild message with guild_id + stale channelInfo.type=DM dropped. Guild message with correct channelInfo.type=GuildText still works. True DMs (no guild_id, channelInfo.type=DM) still accepted.
  • Edge cases checked: guild_id present but channelInfo null results in isDirectMessage = false (correct). GroupDM with guild_id absent still works via channelInfo.type.
  • What you did not verify: Live Discord API integration with actual stale channel cache scenario.

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Failure Recovery (if this breaks)

  • How to disable/revert: Revert this commit; isDirectMessage returns to channelInfo?.type === ChannelType.DM.
  • Files/config to restore: src/discord/monitor/message-handler.preflight.ts

Risks and Mitigations

  • Risk: A guild message where guild_id is absent from the event payload would fail to be recognized as guild.
    • Mitigation: guild_id is a core Discord gateway field always present for guild messages. The same pattern is already proven in agent-components.ts for interactions.

When channelInfo returned ChannelType.DM for a guild channel (stale
cache or API inconsistency), the message was classified as a DM,
bypassing the requireMention gate. guild_id is now authoritative:
if present, the message is never treated as DM or GroupDM.

Closes openclaw#34353

Made-with: Cursor
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 4, 2026

Greptile Summary

This PR fixes a regression where requireMention: true was not being enforced for guild messages — the bot was responding to every message in a guild channel because isDirectMessage was derived solely from channelInfo?.type, which could return ChannelType.DM for guild channels due to stale cache or library inconsistency. The fix adds !isGuildMessage as a precondition for both isDirectMessage and isGroupDm, mirroring the existing pattern in agent-components.ts.

  • The two-line change is minimal, well-scoped, and correctly targets the root cause.
  • isGuildMessage is derived from Boolean(params.data.guild_id), which is the authoritative Discord gateway field for guild membership — this is the right source of truth.
  • True DMs (no guild_id) continue to work correctly since !isGuildMessage evaluates to true there.
  • The new regression test correctly simulates the stale-cache scenario and verifies the message is dropped under requireMention: true.
  • No other behavior is changed; the fix is backward-compatible.

Confidence Score: 5/5

  • This PR is safe to merge — it is a minimal, well-targeted bug fix with no side effects on unrelated paths.
  • The change is two lines, well-justified by the Discord gateway spec (guild_id is always present for guild messages), mirrors an already-proven pattern in agent-components.ts, is covered by a new regression test, and does not touch any other preflight logic. No risk of breaking DMs, GroupDMs, or other message types.
  • No files require special attention.

Last reviewed commit: d7c4607

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: discord Channel integration: discord size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Discord requireMention not working - agent responds and shows typing even without mention

2 participants