Skip to content

fix: role mentions trigger unintended bots in multi-bot guilds #245

@chengli

Description

@chengli

Description

When multiple openab bots share a Discord guild, a role mention intended for one bot triggers all bots whose allowed_channels include that channel.

Root cause: openab supports triggering the bot via role mention (in addition to direct @user mention). The detection logic at discord.rs:53 checks if msg.mention_roles appear in msg.content via string matching:

msg.mention_roles.iter().any(|r| msg.content.contains(&format!("<@&{}>", r)))

This matches any role mentioned in the message, because the code never checks whether the mentioned roles actually belong to the receiving bot. If bot A has role X and bot B has role Y, a message mentioning role Y will incorrectly trigger bot A.

Proposed fix: compare msg.mention_roles against the bot's own guild roles instead of string-matching the message content.

Performance consideration: calling guild.member() per message to look up the bot's roles adds an API call on every message. A role cache (populated once from guild_create events and stored in a RwLock<HashSet>) eliminates this overhead entirely. guild_create is preferred over ready because ready.guilds only contains UnavailableGuild stubs — guild member data may not be available until guild_create fires, particularly for larger guilds.

Use Case

In a multi-bot guild (e.g., multiple openab agents each with their own Discord role for targeted routing), role-mentioning one agent should not trigger the others. This is critical for multi-agent architectures where different agents have different capabilities and tasks must be routed precisely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdiscordp2Medium — planned work

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions