Skip to content

feat: resolve the guild of gateway structures from the cache - #103

Merged
RedStar071 merged 2 commits into
feat/manager-addfrom
feat/structure-guild
Sep 25, 2026
Merged

RedStar071 merged 2 commits into
feat/manager-addfrom
feat/structure-guild

Conversation

@RedStar071

Copy link
Copy Markdown
Member

Stacked on #102.

🔗 Linked issue

Related to #54.

🧭 Context

#102 resolves authors and users from the cache. discord.js also resolves a structure's guild: channel.guild, member.guild, role.guild, message.guild and so on are all the cached Guild. Here, guild structures only had guildId, and fetchGuild() existed only on Message.

📚 Description

Every guild structure now has a guild getter that returns the cached guild. That covers channels and threads, members, roles, messages, emojis, stickers and invites. Messages also get channel, the cached channel. Managers resolve both in hydrate, so they come for free with get, fetch, _add and the gateway events. The getter is null when the guild isn't cached or the structure was built by hand. fetchGuild() is now on every guild structure for when you need the guild regardless.

GuildInvite#guild matches discord.js: the cached Guild when there is one, else the partial InviteGuild from the API.

The relations from #102 used one private field per class. That can't work for channels, since their getters live in mixins that can't see a class's private fields. So Structure now keeps every relation under one exported kRelations symbol:

  • managers pass the relations as the constructor's second argument;
  • each class narrows the type (declare protected [kRelations]: MessageRelations);
  • kClone copies them, so a clone keeps its guild;
  • dropRelations() forgets a relation when a patch carries fresher data for it, e.g. message.edit() returning a new author.

One cost to know: resolving guild is one more cache read per structure. hydrate runs all of a structure's reads in parallel, but listing 1000 members still reads the same guild 1000 times. On Redis that's worth batching later, e.g. per-call memoization in list methods.

Key changes

Area Change
Structure kRelations slot, constructor relations argument, kClone keeps relations, dropRelations
guild Channels/threads (GuildChannelMixin), GuildMember, Role, Message, GuildEmoji, Sticker, GuildInvite
fetchGuild() Added to GuildMember, Role, GuildEmoji, Sticker and guild channels
Message#channel The cached channel
Managers hydrate resolves the guild through the new cachedGuild() helper

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation change
  • Workflow / CI change

Pre-flight Checklist

  • I have read the Contributing Guidelines (if applicable)
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly
  • I added a changeset with pnpm changeset (or pnpm changeset add --empty if no release is needed). See .changeset/README.md.

🤖 AI disclosure: Wolfstar Agent Kit modified this description. My AI open-source policy.

@coldtea-pr-lens

coldtea-pr-lens Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

◈ PR Lens

PR Lens paused analysis for this repository — the fair-use limit was reached. It resumes automatically at 2026-09-25T18:00:00.000Z.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one minor suggestion inline, and a coverage note below.

Reviewed changes

Reviewed the full diff: every guild-scoped structure in plugin-gateway (channels/threads, GuildMember, Role, Message, GuildEmoji, Sticker, GuildInvite) gains a cached guild getter plus (mostly) a fetchGuild() REST fallback, and Message gains a cached channel getter.

  • kRelations symbol — Structure replaces the old one-private-field-per-class pattern with a single exported kRelations slot, needed because mixin getters (GuildChannelMixin) can't see a class's private fields; kClone now shallow-copies it and dropRelations() invalidates named relations on fresher patch data.
  • CachedManager.cachedGuild() — new cache-only helper (this.client.guilds.get(guildId) ?? null), used by every touched manager's hydrate() alongside existing relations via Promise.all.
  • ChannelManager/ThreadManager — derive the guild ID via a channelGuildId() helper that accounts for GUILD_CREATE's embedded channels not carrying guild_id natively (verified against plugin-cache's hydrateGuildCreate, which synthesizes it).
  • MessageManager — resolves channel via client.channels.get(), which already falls back to the thread cache, so messages in threads resolve correctly.
  • Tests — tests/cached-manager.test.ts gains coverage for channel/role/message/member guild resolution, an uncached/hand-built → null case, and clone-retains-relations.

I traced the trickier claims by hand (GUILD_CREATE's synthesized guild_id, thread fallback in ChannelManager.get, member guild_id backfill in fetchRaw, mixin type-narrowing of kRelations) and didn't find a correctness issue in any of them — the pattern is applied consistently across all seven structures.

ℹ️ Nitpicks

  • New tests cover guild resolution for channels, roles, messages, and members, but not GuildEmoji, Sticker, or GuildInvite — they follow the exact same cachedGuild() pattern, so risk is low, but it's a gap worth closing before the pattern is copied elsewhere.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using Claude Sonnet | 𝕏

@pkg-pr-new

pkg-pr-new Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@wolfstar/plugin-api

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-api@103

@wolfstar/plugin-cache

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-cache@103

@wolfstar/plugin-gateway

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-gateway@103

@wolfstar/plugin-i18next

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-i18next@103

@wolfstar/plugin-logger

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-logger@103

@wolfstar/plugin-subcommands-advanced

pnpm add https://pkg.pr.new/wolfstar-project/plugins/@wolfstar/plugin-subcommands-advanced@103

commit: 6684158

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Reviewed commit 8c9b9ec ("fix(plugin-gateway): type-check relation names and cover asset guilds"), which addresses both points from the prior Pullfrog review of b33b12f.

  • Tightened dropRelations's parameter type — Structure.dropRelations(...names: string[]) is now dropRelations(...names: (keyof this[typeof kRelations] & string)[]), so a typo'd or renamed relation key now fails to compile instead of silently no-op'ing at runtime.
  • Widened [kRelations] from protected to public — required by the above (the base dropRelations method needs to reference this[typeof kRelations] in a type position across all narrowed subclasses); applied consistently to Structure and all 7 narrowing subclasses (BaseInvite, Channel, GuildEmoji, GuildMember, Message, Role, Sticker), and documented with an @internal JSDoc explaining why.
  • Added missing test coverage — a new describe("guild relations of guild assets", ...) block in tests/cached-manager.test.ts exercises GuildEmoji/Sticker/GuildInvite's guild resolution via client.guilds.emojis/stickers/invites(guildId).fetch(...), closing the gap the prior review flagged.

I traced the [kRelations] widening for misuse risk (grepped every read/write site) and found none outside the intended getter/constructor/kClone/dropRelations pattern — the wider accessibility is a real but currently-unexploited surface expansion, reasonable given the alternative (readonly isn't viable, since both dropRelations and [kClone] mutate the field outside the constructor). I also rebuilt @wolfstar/plugin-cache and ran tsc --noEmit (clean) plus the full cached-manager.test.ts suite (12/12 passing, including the 2 new tests) to confirm the generic-constraint change compiles and the new tests genuinely exercise the emoji/sticker/invite paths rather than asserting tautologies.

The prior review's one open thread (Structure.ts:97-108, untyped dropRelations names) is addressed and resolved.

Pullfrog  | View workflow run | Using Claude Sonnet | 𝕏

@pullfrog

pullfrog Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Your Claude subscription has hit its usage limit. It resets at 7:20pm (UTC). Re-trigger Pullfrog after the reset, or add an ANTHROPIC_API_KEY repo secret — Pullfrog routes around an exhausted subscription automatically when one is present.

Add repo secret → · Model settings → · Setup docs → · Ask in Discord →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using Claude Sonnet | 𝕏

@pullfrog

pullfrog Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Your Claude subscription has hit its usage limit. It resets at 7:20pm (UTC). Re-trigger Pullfrog after the reset, or add an ANTHROPIC_API_KEY repo secret — Pullfrog routes around an exhausted subscription automatically when one is present.

Add repo secret → · Model settings → · Setup docs → · Ask in Discord →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using Claude Sonnet | 𝕏

@RedStar071
RedStar071 merged commit 5f897c6 into main Sep 25, 2026
19 of 25 checks passed
@RedStar071
RedStar071 deleted the feat/structure-guild branch September 25, 2026 18:02
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