Repository navigation
feat(plugin-gateway): sorted setPosition and transformResolved - #160
Conversation
◈ PR LensNote This drawing shows
Architecture 7 components touched across 4 lanes. Play the interactive walkthrough Inside the changed components — 2 viewsComponent view — Position sorting and reordering Sorts guild channels and roles by Discord hierarchy rules and reorders sibling positions. Component view — Mentions and entity resolution Resolves message mentions and raw data payloads into structures using cache lookups. Data flow
Follow each request, response and payload The other flows — 1 sequence
View
Tip Run 🪧 More tips
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. |
There was a problem hiding this comment.
ℹ️ 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 toPATCH.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. ThrowsGuildChannelUnknownfor a channel that isn't one of the guild's.RoleManager#setPosition: same treatment for roles; the 4th parameter is nowSetPositionOptions | string(a bare reason string still works). ThrowsGuildRoleUnknownfor an unrecognized role.transformResolved: resolves users/members/roles/channels by ID from the cache, or from raw API data, into structures.MessageManager#resolveMentionsnow buildsMessageMentionsrelations through it instead of ad hocwhenCachedMapcalls.- Tests: new coverage for
discordSort,getSortableGroupTypes,moveElementInArray,computePositions,transformResolved, and the newsetPosition/fetchSortededge 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 returnsdiscordSort(structures).toReversed(), so its ordering already matchesdiscordSortsemantics — no other inconsistency found (e.g.GuildMemberRoleManager#fetch's untouchedcomparePositionTo-based sort ties-breaks identically, just via a different code path).
Claude Sonnet | 𝕏
@wolfstar/plugin-api
@wolfstar/plugin-broker
@wolfstar/plugin-cache
@wolfstar/plugin-gateway
@wolfstar/plugin-i18next
@wolfstar/plugin-logger
@wolfstar/plugin-sharder
@wolfstar/plugin-subcommands-advanced
commit: |
There was a problem hiding this comment.
✅ 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 inRoleManager.setPositioninstead of re-runningdiscordSorton 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.
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

🔗 Linked issue
Related to #159
🧭 Context
Next step of the discord.js
Util.jsport. #158 landeddiscordSort,moveElementInArray, andgetSortableGroupTypesin parallel, but nothing used them yet: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.MessageManagerresolved the mention relations with ad hoc cache reads. discord.js hastransformResolvedfor this.📚 Description
setPositionon channels and roles now moves the entity among its sorted siblings, like discord.js, through a newcomputePositionshelper.transformResolvedresolves users, members, roles, and channels by ID (from the cache) or from raw data, andMessageMentionsrelations are built with it.moveElementInArrayalso gets a guard: with a missing element,splice(-1, 1)used to move the last item.Things I'm not sure about:
setPositionpaths 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.positionon channels and roles is still the raw value. A sortedpositionwith a separaterawPositionwould need every sibling in hand at build time, so I left it for feat(plugin-gateway): remaining discord.js Util helpers #159.transformResolvedaccepts raw data, butMessageMentionsstill 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.The
positionargument ofGuildChannel#setPosition,Role#setPosition, andRoleManager.setPositionis 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
Pre-flight Checklist
pnpm changeset(orpnpm changeset add --emptyif no release is needed). See .changeset/README.md.