Skip to content

Refactor/phase 6 polish - #6

Merged
Nitrocosm merged 4 commits into
masterfrom
refactor/phase-6-polish
May 19, 2026
Merged

Nitrocosm merged 4 commits into
masterfrom
refactor/phase-6-polish

Conversation

@Nitrocosm

Copy link
Copy Markdown
Owner

No description provided.

Nitrocosm added 4 commits May 19, 2026 04:39
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.
@Nitrocosm
Nitrocosm merged commit fc8be54 into master May 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant