Skip to content

feat(plugin-gateway): sorted setPosition and transformResolved - #160

Merged
kodiakhq[bot] merged 4 commits into
mainfrom
feat/gateway-position-sort-transform-resolved
Sep 28, 2026
Merged

kodiakhq[bot] merged 4 commits into
mainfrom
feat/gateway-position-sort-transform-resolved

Conversation

@RedStar071

@RedStar071 RedStar071 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

🔗 Linked issue

Related to #159

🧭 Context

Next step of the discord.js Util.js port. #158 landed discordSort, moveElementInArray, and getSortableGroupTypes in parallel, but nothing used them yet:

  • Moving a channel or role used the raw API position. With relative, it just added the offset to that number, even though Discord orders channels per category and per group (text before voice). discord.js sorts the siblings and resends every position.
  • MessageManager resolved the mention relations with ad hoc cache reads. discord.js has transformResolved for this.

📚 Description

setPosition on channels and roles now moves the entity among its sorted siblings, like discord.js, through a new computePositions helper. transformResolved resolves users, members, roles, and channels by ID (from the cache) or from raw data, and MessageMentions relations are built with it. moveElementInArray also gets a guard: with a missing element, splice(-1, 1) used to move the last item.

Things I'm not sure about:

  • Both setPosition paths call the API once more (a GET) before the PATCH, because the cache cannot list the channels or roles of one guild. It works, but costs a request. I listed a cache-backed version in feat(plugin-gateway): remaining discord.js Util helpers #159.
  • position on channels and roles is still the raw value. A sorted position with a separate rawPosition would need every sibling in hand at build time, so I left it for feat(plugin-gateway): remaining discord.js Util helpers #159.
  • transformResolved accepts raw data, but MessageMentions still only passes IDs, so its "cached copies only" semantics stay the same. Passing the raw mentions would match discord.js more closely (it _adds them). Open to switching.

⚠️ Breaking Changes

The position argument of GuildChannel#setPosition, Role#setPosition, and RoleManager.setPosition is now an index among the sorted siblings (lowest first), not a raw API position. An out of range index leaves everything where it is instead of letting Discord clamp it. Role#setPosition(position, "reason") still works; the second argument can also be { relative, reason }.

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.

@RedStar071 RedStar071 added the packages:plugin-gateway Changes in plugin-gateway label Sep 28, 2026
@coldtea-pr-lens

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

Copy link
Copy Markdown

◈ PR Lens

Note

This drawing shows c93ff6f, and the branch has new commits since. Tick Redraw to draw the latest one

  • Redraw

🟢 +2 new · 🟠 ~5 changed · 🔴 -0 removed · 2 flows · 11 files · commit c93ff6f


Architecture

Architecture diagram for wolfstar-project/plugins at c93ff6f

7 components touched across 4 lanes.

Play the interactive walkthrough


Inside the changed components — 2 views

Component view — Position sorting and reordering

Sorts guild channels and roles by Discord hierarchy rules and reorders sibling positions.

Architecture view of Component view — Position sorting and reordering in wolfstar-project/plugins

Component view — Mentions and entity resolution

Resolves message mentions and raw data payloads into structures using cache lookups.

Architecture view of Component view — Mentions and entity resolution in wolfstar-project/plugins

Data flow

Data flow diagram for wolfstar-project/plugins at c93ff6f

Reordering guild channel positions · Resolving message mentions into structures

Follow each request, response and payload


The other flows — 1 sequence

Resolving message mentions into structures

Sequence diagram of Resolving message mentions into structures in wolfstar-project/plugins

View

  • Architecture lens
  • Data flow lens
  • Expand every detail

Tip

Run npx @coldtea/pr-lens-cli analyze --base origin/main on a branch, then npx @coldtea/pr-lens-cli render .pr-lens/graph.json. Same lenses, your own model key, before the pull request exists

🪧 More tips
  • Run npx skills add coldteadotai/pr-lens, then tell your coding agent: "Diagram the change you just made with PR Lens and attach it to the pull request."
  • Untick Architecture lens or Data flow lens under View to hide a diagram, or tick Expand every detail to open every section. The comment redraws in a few seconds
  • Click the link under each diagram to open it on a canvas you can zoom, pan and step through
  • The diagrams are links. Click one to open it on the canvas, then press W or click play to walk through the change
  • Open a diagram on the canvas, then press W or click play to walk through the change one step at a time
  • The CLI's render reads .github/pr-lens.yml and applies your renames, exclusions and lane pins at draw time
  • Set github.comment.collapsed: true in .github/pr-lens.yml to fold the comment behind one View architecture and data flow row. Drawing still runs as before
  • Set github.draw: on-demand in .github/pr-lens.yml and PR Lens stops drawing pull requests on its own. Comment @pr-lens draw on a pull request when you want that one drawn
  • Add .github/workflows/pr-lens.yml with coldteadotai/pr-lens/packages/action@v0 and your model provider's key as its api-key to run PR Lens from your own CI. Any /chat/completions endpoint works
  • Push a commit and the drawing stays, with a note that it is out of date. Tick Redraw in the note to draw the new head
  • Switch GitHub to dark mode and the diagrams follow. The moving dots are this pull request's data in motion

Thanks for using PR Lens! It's built by Coldtea, free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

@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 efficiency nitpick inline.

Reviewed changes

Reviewed the port of discord.js's Util.js position sorting and transformResolved into plugin-gateway.

  • discordSort / getSortableGroupTypes / moveElementInArray / computePositions (util/Util.ts): sorts roles and guild channels the way Discord displays them, and computes the reindexed sibling positions to PATCH.
  • GuildChannelManager#setPosition / #fetchSorted: a channel now moves among the sorted channels of its category and group (text-like/voice/categories), not a raw API position. Throws GuildChannelUnknown for a channel that isn't one of the guild's.
  • RoleManager#setPosition: same treatment for roles; the 4th parameter is now SetPositionOptions | string (a bare reason string still works). Throws GuildRoleUnknown for an unrecognized role.
  • transformResolved: resolves users/members/roles/channels by ID from the cache, or from raw API data, into structures. MessageManager#resolveMentions now builds MessageMentions relations through it instead of ad hoc whenCachedMap calls.
  • Tests: new coverage for discordSort, getSortableGroupTypes, moveElementInArray, computePositions, transformResolved, and the new setPosition/fetchSorted edge cases (relative moves, out-of-range index, unknown channel/role).

I hand-traced computePositions/moveElementInArray against the new tests, cross-checked discordSort's tie-break direction and the sortable-group logic against discord.js's actual Util.js semantics (parity confirmed, including moveElementInArray's added index > -1 guard being a genuine improvement over discord.js's version), and ran the full affected test suite after building @wolfstar/plugin-cache — 71/71 pass. tsc --noEmit and oxlint on the touched files are both clean.

ℹ️ Nitpicks

  • RoleManager.fetchAll (used elsewhere for "highest role" lookups) already returns discordSort(structures).toReversed(), so its ordering already matches discordSort semantics — no other inconsistency found (e.g. GuildMemberRoleManager#fetch's untouched comparePositionTo-based sort ties-breaks identically, just via a different code path).

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

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

@wolfstar/plugin-api

pnpm add https://pkg.pr.new/@wolfstar/plugin-api@160

@wolfstar/plugin-broker

pnpm add https://pkg.pr.new/@wolfstar/plugin-broker@160

@wolfstar/plugin-cache

pnpm add https://pkg.pr.new/@wolfstar/plugin-cache@160

@wolfstar/plugin-gateway

pnpm add https://pkg.pr.new/@wolfstar/plugin-gateway@160

@wolfstar/plugin-i18next

pnpm add https://pkg.pr.new/@wolfstar/plugin-i18next@160

@wolfstar/plugin-logger

pnpm add https://pkg.pr.new/@wolfstar/plugin-logger@160

@wolfstar/plugin-sharder

pnpm add https://pkg.pr.new/@wolfstar/plugin-sharder@160

@wolfstar/plugin-subcommands-advanced

pnpm add https://pkg.pr.new/@wolfstar/plugin-subcommands-advanced@160

commit: 79b2dcd

@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 the single commit pushed since the last review: a follow-up to the earlier feedback on RoleManager.setPosition.

  • Reused fetchAll's already-sorted order in RoleManager.setPosition instead of re-running discordSort on it.

Verified the fix is behaviorally equivalent to the original: fetchAll returns discordSort(structures).toReversed() (highest role first), so reversing that result again yields discordSort(structures) (lowest first) directly — same output as the old discordSort(await this.fetchAll(guildId)), just an O(n) reverse instead of an extra O(n log n) sort. This addresses the only open thread from the prior review, which has been replied to and resolved.

Pullfrog  | View workflow run | Using Claude Sonnet | 𝕏

…-sort-transform-resolved

# Conflicts:
#	packages/plugin-gateway/src/managers/RoleManager.ts
#	packages/plugin-gateway/src/structures/guilds/Role.ts
#	packages/plugin-gateway/src/util/Util.ts
@RedStar071 RedStar071 changed the title feat(plugin-gateway): add discordSort, sorted setPosition, and transformResolved feat(plugin-gateway): sorted setPosition and transformResolved Sep 28, 2026
@kodiakhq
kodiakhq Bot merged commit b7efc3d into main Sep 28, 2026
16 of 17 checks passed
@kodiakhq
kodiakhq Bot deleted the feat/gateway-position-sort-transform-resolved branch September 28, 2026 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

packages:plugin-gateway Changes in plugin-gateway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant