How it works
- Event handler integration — user management runs from the main event handlers in
src/events/common.ts. - Automatic check — every slash command (and prefix command) checks whether the caller exists in the database.
- Automatic creation — if not, a new
Userrow is created. - Username updates — if the row exists but the Discord username changed, it is updated.
Database model
User.id UUID is what foreign keys use — not the Discord snowflake.
Key functions
ensureUserExists(discordId, username)
- Looks up by Discord id.
- Creates a row when missing.
- Updates
usernamewhen it changed. - Returns the database user object.
getUserByDiscordId(discordId)
Retrieves a user by Discord id without creating one.
Event integration
Slash commands callensureUserExists before dispatch:
message.author.
Error handling
Database errors are logged. If user creation fails, the existing error handler surfaces it to the caller. Production (main.ts) closes the Prisma connection on SIGINT/SIGTERM; development (dev.ts) does not.
Using it in commands
Any command can assume the caller has aUser row after the event hook runs:
/profile uses the same helper instead of duplicating create/update logic.
Related
- Commands —
/profileviews or refreshes the stored row - Account linking — links hang off
User.id