Repository navigation
Refactor/phase 6 polish - #6
Merged
Merged
Conversation
The presences intent triggers Discord to send PRESENCE_UPDATE events for every member's status/activity change — but the bot never reads member.activity, member.status, or registers on_presence_update. So the event stream was pure noise (parsed, dispatched, ignored). Dropping it shrinks gateway traffic, reduces the privileged-intent surface in the Dev Portal, and speeds up READY (fewer initial presences to receive). Add tests/test_bot_init.py to pin the intent shape so a future regression is caught.
The handler runs on every raw gateway packet (MESSAGE_CREATE, TYPING_START, VOICE_STATE_UPDATE, GUILD_MEMBER_UPDATE, ...) — hundreds per minute in a busy guild. It then json-parses every single one, discards the parsed dict if op != 0 or event_type isn't a GUILD_JOIN_REQUEST_*. Add a cheap substring check: if "GUILD_JOIN_REQUEST" isn't anywhere in the raw payload, bail out before json.loads. Also skip bytes payloads explicitly (we only consume str-mode dispatch frames). A user message containing the literal substring would pass the cheap gate, but the explicit event_type check inside the handler still filters it out — covered by a regression test.
_get_role_hierarchy() iterates and sorts guild.roles on every call. It's invoked from _fix_categories(), which runs on every RoleSession.commit() — i.e. on every role mutation through the session. For a busy guild that's tens of times per minute. Category-role topology (── prefixed roles, "🚫 none" markers) only changes when an admin renames/creates/moves a role, which happens on the order of minutes-to-days. A 30 s TTL bounds staleness to one half-minute window — well within tolerance for a hobby bot — and load_role_relations() (the existing "refresh state" hook) flushes the cache explicitly so the .load_role_relations admin command also refreshes the hierarchy. Adds clear_hierarchy_cache(guild_id=None) for selective or global invalidation. Cache is per-guild-id so multiple guilds don't share state.
The original test patched cogs.server_events._on_join_request_create directly, but discord.py's bot.load_extension overwrites sys.modules['cogs.server_events'] with a new module instance during test_cogs_load. The ServerEventsCog class imported at the top of test_socket_raw_gate refers to the ORIGINAL class, whose bound methods' __globals__ still point at the original module dict — so the patch on sys.modules misses the actual lookup. Test passed in isolation, failed when run after test_cogs_load. Switch to asserting on modules.general.send (the real observable side effect of the dispatch). modules.general is imported once and stable across both old and new server_events module instances.
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.
No description provided.