Skip to content

feat(#158): level foundation — member progress, level roles, configuration - #167

Open
wiktoriavh wants to merge 9 commits into
mainfrom
feat/158/level-foundation
Open

wiktoriavh wants to merge 9 commits into
mainfrom
feat/158/level-foundation

Conversation

@wiktoriavh

@wiktoriavh wiktoriavh commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Closes #158

Child item 1 of epic #157. Implementation spec: docs/spec/onboarding.item-1.md.

  • MemberProgress Prisma model (member_progress) + migration via pnpm migrate:dev
  • Onboarding config behind ONBOARDING_ENABLED 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() transitions with Level/LevelEvent as const objects (constants.ts)
  • applyLevelRoles(): exactly roles 0..N, idempotent, throws on failure
  • Exemption rules (bots never leveled; bots/moderators never dormant)
  • GuildMemberAdd/GuildMemberRemove listeners gated on the flag (join upserts progress + applies roles, leave stamps leftAt)
  • Unit tests for transitions, exemptions and role application (26 tests)
  • Deletes the unregistered onboarding command stub

Nothing onboarding-related runs while ONBOARDING_ENABLED is not 'true'.

Verified locally: pnpm test:ci (90/90, onboarding: 26/26), pnpm typecheck, pnpm lint, pnpm fmt:check, pnpm build.

Ops note

GatewayIntentBits.GuildMembers is a privileged intent — it must also be enabled in the Discord developer portal, otherwise the GuildMemberAdd/GuildMemberRemove listeners never fire in production.

wiktoriavh and others added 4 commits December 3, 2026 18:55
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>
@wiktoriavh

Copy link
Copy Markdown
Member Author

I tested it as well, and when the onboarding flag is enabled, the role is being applied.

@wiktoriavh
wiktoriavh marked this pull request as ready for review December 3, 2026 21:02
Comment thread src/features/onboarding/roles.ts Outdated
import { addRoleToUser } from '@/util/add-role-to-user.js';
import type { Level } from './constants.js';

const getLevelRoleIds = (): string[] => {

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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How would you fail here early? I don't see how.

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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

i was asking you to show me. :D

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.

You could fail by preforming the check way earlier, see my comment on env.ts!! ;3

@@ -0,0 +1,19 @@
export const Level = {

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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

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.

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 => {

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.

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?

@wiktoriavh wiktoriavh Dec 3, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/features/onboarding/exemptions.ts Outdated
): boolean => {
return (
member.user.bot ||
moderatorRoleIds.some((roleId) => member.roles.cache.has(roleId))

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.

I believe we have isModerator() for this already?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

you're right, let me rework it.

Comment thread src/env.ts Outdated
'ONBOARDING_LEVEL_3_ROLE_ID',
] as const
).filter((envVar) => !process.env[envVar]);
if (missingRoleVars.length > 0) {

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.

You can probably reuse your roleIDs function here, and make the error fatal.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

which role id function do you mean specifically?

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.

feat/onboarding/role.ts:6 ^^

@michal-skraburski

Copy link
Copy Markdown
Contributor

Claude /code-review high

  {
    "file": "src/features/onboarding/roles.ts",
    "line": 42,
    "summary": "Role removal is hand-rolled with inline try/catch error wrapping while role addition uses the shared addRoleToUser util, creating an asymmetry and no reusable removal helper.",
    "failure_scenario": "The add path calls addRoleToUser (which does the cache-has check and wraps failures as `Failed to add role ... to user ...`), but removal reimplements the same has-check + wrapped-error pattern inline. Items 3-7 that also move levels will likely copy this block; a removeRoleFromUser util mirroring add-role-to-user.ts would centralize the behavior and keep error messages consistent."
  },  {
    "file": "src/env.ts",
    "line": 77,
    "summary": "Startup warning hardcodes the four ONBOARDING_LEVEL_*_ROLE_ID names in a separate list instead of deriving from config.onboarding.roleIds, so the two can drift.",
    "failure_scenario": "If a level is added/renamed in config.onboarding.roleIds but the hardcoded warning list isn't updated in lockstep, the warning silently stops naming a genuinely-missing var (or names one that no longer exists), making the startup diagnostic misleading."
  }
]
  {
    "file": "src/features/onboarding/roles.ts",
    "line": 42,
    "summary": "Role removal is hand-rolled with inlihile role addition uses the shared addRoleToUser util,creating an asymmetry and no reusable removal helper.",
    "failure_scenario": "The add path calls addRoleToUser (which does the cache-has check and wraps failures as `Failed to add role ...
to user ...`), but removal reimplements the same has- inline. Items 3-7 that also move levels will likelycopy this block; a removeRoleFromUser util mirroring add-role-to-user.ts would centralize the behavior and keep error messages
consistent."
  },
  {
    "file": "src/env.ts",
    "line": 77,
    "summary": "Startup warning hardcodes the four ONBOARDING_LEVEL_*_ROLE_ID names in a separate list instead of deriving from config.onboarding.roleIds, so the two can drift.",
    "failure_scenario": "If a level is added/renamed in config.onboarding.roleIds but the hardcoded warning list isn't updated in lockstep, the warning silently stops naming a genuinely-missing var (or names one that no longer exists), making the startup diagnostic misleading."                                                                                                                               }
]```

wiktoriavh and others added 5 commits December 4, 2026 17:34
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>
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.

Level foundation: member progress, level roles and configuration

2 participants