Repository navigation
feat(#158): level foundation — member progress, level roles, configuration - #167
wiktoriavh wants to merge 9 commits into
Conversation
Finalized implementation spec for the level foundation: data model, env config, module layout, transition table and test plan. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
…ation Child item 1 of epic #157. Adds the shared base for onboarding: - MemberProgress Prisma model (member_progress table) + migration - Onboarding config behind the ONBOARDING_ENABLED flag in src/env.ts: only the env vars this item reads (ONBOARDING_LEVEL_0..3_ROLE_ID) - GuildMembers intent in src/index.ts - Pure nextLevel() transition function with Level/LevelEvent as const objects in constants.ts (one level at a time, only 3->2 or forced reset down, never skips) - applyLevelRoles(): member holds exactly level roles 0..N, idempotent, throws on failure, never touches channel overwrites - Exemption rules: bots never get levels, bots/moderators never dormant - GuildMemberAdd/GuildMemberRemove listeners, gated on the flag: join upserts progress and applies the level roles, leave stamps leftAt - Unit tests for the transition table and exemption rules - Deletes the unregistered onboarding command stub Nothing onboarding-related runs while ONBOARDING_ENABLED is not 'true'. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
- isLevel type guard; GuildMemberAdd applies the existing row's level (fallback NewJoiner), applies roles before persisting the upsert, and clears leftAt on rejoin - env.ts logs a startup warning when ONBOARDING_ENABLED=true but level role vars are unset - add roles.test.ts covering applyLevelRoles add/remove/idempotency/error paths - braced bodies for multi-line arrow functions - spec doc updated to match Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
|
I tested it as well, and when the onboarding flag is enabled, the role is being applied. |
| import { addRoleToUser } from '@/util/add-role-to-user.js'; | ||
| import type { Level } from './constants.js'; | ||
|
|
||
| const getLevelRoleIds = (): string[] => { |
There was a problem hiding this comment.
This should fail as early as possible, instead it is implemented to possibly fail during a critical moment where you could lose data and a member could get no access.
There was a problem hiding this comment.
How would you fail here early? I don't see how.
There was a problem hiding this comment.
How would you fail here early? I don't see how.
That's the point, you don't fail early here, and because you don't, you won't know the bot isn't applying roles until it's a problem. If you fail early and fatally, you'll know immediately the roles are messed up. This is the function I meant earlier, by the way.
There was a problem hiding this comment.
i was asking you to show me. :D
There was a problem hiding this comment.
You could fail by preforming the check way earlier, see my comment on env.ts!! ;3
| @@ -0,0 +1,19 @@ | |||
| export const Level = { | |||
There was a problem hiding this comment.
This structure is so similar to the onboarding role ids, while I like that it simplifies the semantics of what each role id is supposed to represent, maybe we should consider simply renaming the role ids to match what we mean? This "enum" is being used to solve indexing for an ARRAY.
There was a problem hiding this comment.
This "enum" is being used to solve indexing for an ARRAY.
I don't quite get what you mean.
we want to specifically be able to point to a level. an array isn't the solution here. they just happen to both start at 0, but mean different things.
There was a problem hiding this comment.
They mean different things but are linked. Both don't just start 0, they are the same thing as data. When you refer to role id 0, you refer to NewJoiner. Why don't you just call NewJoinerRoleId and tie them together? Iterate over them as the array they are here:
// role.ts:8~
const configured = [
config.onboarding.roleIds.level0, //config.onboarding.roleIds.NewJoiner
config.onboarding.roleIds.level1, // config.onboarding.rolesIds.AgreeToRules
config.onboarding.roleIds.level2, // etc.
config.onboarding.roleIds.level3,
];| } as const; | ||
| export type Level = (typeof Level)[keyof typeof Level]; | ||
|
|
||
| export const isLevel = (value: number): value is Level => { |
There was a problem hiding this comment.
are you checking... range? Are you expecting holes in the Level structure? if not you could simplify this to the max and min value if check?
There was a problem hiding this comment.
It is for better typing. It's a type guard. to make sure what we use is the level. instead of using assertion (we force the type), we check if it is the type and assign it correctly.
might be cleaned up a bit.
| ): boolean => { | ||
| return ( | ||
| member.user.bot || | ||
| moderatorRoleIds.some((roleId) => member.roles.cache.has(roleId)) |
There was a problem hiding this comment.
I believe we have isModerator() for this already?
There was a problem hiding this comment.
you're right, let me rework it.
| 'ONBOARDING_LEVEL_3_ROLE_ID', | ||
| ] as const | ||
| ).filter((envVar) => !process.env[envVar]); | ||
| if (missingRoleVars.length > 0) { |
There was a problem hiding this comment.
You can probably reuse your roleIDs function here, and make the error fatal.
There was a problem hiding this comment.
which role id function do you mean specifically?
There was a problem hiding this comment.
feat/onboarding/role.ts:6 ^^
|
Claude /code-review high |
config.onboarding.roleIds is now a Record<Level, roleId> built from a single env-var map, and when ONBOARDING_ENABLED=true the vars are read with requireEnv, so a missing role ID stops startup instead of failing when a member joins and roles cannot be applied. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Mirrors addRoleToUser: skips roles the member does not hold and wraps removal failures with role and member context. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Replaces the positional role-ID array with a Record<Level, string> and removes higher-level roles via removeRoleFromUser. Completeness is guaranteed at startup by requireEnv, so the runtime check is gone. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
isExemptFromDormancy now delegates to the shared isModerator() helper instead of taking moderator role IDs as a parameter. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Closes #158
Child item 1 of epic #157. Implementation spec:
docs/spec/onboarding.item-1.md.MemberProgressPrisma model (member_progress) + migration viapnpm migrate:devONBOARDING_ENABLEDinsrc/env.ts(only the env vars this item reads:ONBOARDING_LEVEL_0..3_ROLE_ID)GuildMembersintent insrc/index.tsnextLevel()transitions withLevel/LevelEventas const objects (constants.ts)applyLevelRoles(): exactly roles 0..N, idempotent, throws on failureGuildMemberAdd/GuildMemberRemovelisteners gated on the flag (join upserts progress + applies roles, leave stampsleftAt)Nothing onboarding-related runs while
ONBOARDING_ENABLEDis not'true'.Verified locally:
pnpm test:ci(90/90, onboarding: 26/26),pnpm typecheck,pnpm lint,pnpm fmt:check,pnpm build.Ops note
GatewayIntentBits.GuildMembersis a privileged intent — it must also be enabled in the Discord developer portal, otherwise theGuildMemberAdd/GuildMemberRemovelisteners never fire in production.