fix: role-mention only matches bot's own roles (with guild_create cache)#246
Open
chengli wants to merge 1 commit intoopenabdev:mainfrom
Open
fix: role-mention only matches bot's own roles (with guild_create cache)#246chengli wants to merge 1 commit intoopenabdev:mainfrom
chengli wants to merge 1 commit intoopenabdev:mainfrom
Conversation
The existing role-mention check matched any role mentioned in the message, not just roles assigned to the receiving bot. In multi-bot guilds this caused cross-triggering: mentioning bot B's role would also wake up bot A. Replace string matching against msg.content with a set intersection against the bot's own cached role IDs. The cache is populated from guild_create events (not ready, which only has UnavailableGuild stubs for larger guilds) and stored in a RwLock<HashSet<u64>>. Early-exit when msg.mention_roles is empty to skip the cache read entirely. Closes openabdev#245
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
guild_createevents (notready) in aRwLock<HashSet>— zero per-message API callsmsg.mention_rolesis empty (skip cache read entirely)Closes #245
Details
Bug:
msg.mention_roles.iter().any(|r| msg.content.contains(...))triggers on any role mentioned in the message, not just the bot's own roles. In a guild with bots A (role X) and B (role Y), mentioning role Y incorrectly wakes up bot A.Fix: replace string matching with a set intersection against the bot's cached roles:
Technical notes:
Why
guild_createinstead ofready?ready.guildscontainsUnavailableGuildstubs. Guild member data may not be available yet, particularly for larger guilds, causing.member()lookups to 404.guild_createfires per guild afterreadywith complete data.Global
HashSetvs per-guildHashMap: We use a flatHashSet<u64>for the cache. Discord Role IDs are globally unique andmsg.mention_rolesonly contains roles valid for the current guild, so a flat cache is safe and keeps lookups simple.Known limitation: If a role is added to the bot while it is running, it won't be recognized until restart. A
guild_member_updatehandler could keep the cache hot if dynamic role assignment becomes a requested feature.Files changed
src/discord.rsbot_role_ids: Arc<RwLock<HashSet<u64>>>to Handler; replace string-match with cache lookup; addguild_createhandler to populate cachesrc/main.rsbot_role_idscacheTest plan
cargo checkpasses ✅ (verified locally)guild_create