Link actor with a discord user #72

Closed
opened 2026-10-15 15:46:58 +00:00 by Syndria98 · 16 comments
Owner

Make a command to link an actor with a discord user, so that each discord user that is a player on the campaign has assigned one character

Make a command to link an actor with a discord user, so that each discord user that is a player on the campaign has assigned one character
Syndria98 added the ai-ready label 2026-10-15 15:46:58 +00:00
Agent added the working label 2026-10-15 16:44:09 +00:00
Collaborator

Now let me look at the service-links service, the bot's deploy-commands and config, and check locale keys usage:

Now let me look at the service-links service, the bot's deploy-commands and config, and check locale keys usage:
Collaborator

I have the full picture now. Let me check the actor route briefly, then implement. The design:

  • Model: Actor.discordUserId — the Discord player holding a character, unique per campaign (one character per player).
  • Backend: POST /api/discord/bot/guild/:guildId/actors/assign and .../actors/unassign (bot-secret gated, like the rest of the router).
  • Bot: /dragonroll assign <character> (any player claims a free character for themselves; admins move held ones) and /dragonroll unassign [character], plus visibility in /characters and /dragonroll status.
I have the full picture now. Let me check the actor route briefly, then implement. The design: - **Model**: `Actor.discordUserId` — the Discord player holding a character, unique per campaign (one character per player). - **Backend**: `POST /api/discord/bot/guild/:guildId/actors/assign` and `.../actors/unassign` (bot-secret gated, like the rest of the router). - **Bot**: `/dragonroll assign <character>` (any player claims a free character for themselves; admins move held ones) and `/dragonroll unassign [character]`, plus visibility in `/characters` and `/dragonroll status`.
Collaborator

Now the backend routes in discord.js:

Now the backend routes in `discord.js`:
Collaborator

Now the backend bot endpoints for assigning/unassigning actors:

Now the backend bot endpoints for assigning/unassigning actors:
Collaborator

Now the bot's API client:

Now the bot's API client:
Collaborator

Now the slash command definitions:

Now the slash command definitions:
Collaborator

The guildOnly( wrapper on line 32 needs its closing paren at the end of the chain:

The `guildOnly(` wrapper on line 32 needs its closing paren at the end of the chain:
Collaborator

I accidentally removed a blank line; let me restore it and insert the assign/unassign dispatch in the same edit:

I accidentally removed a blank line; let me restore it and insert the assign/unassign dispatch in the same edit:
Collaborator

Now add the handleActorAssignment function and the status field. First the status addition:

Now add the `handleActorAssignment` function and the status field. First the status addition:
Collaborator

The diff is clean. Now update the documentation — discord-bot/README.md and the Actor line in AGENTS.md:

The diff is clean. Now update the documentation — `discord-bot/README.md` and the `Actor` line in `AGENTS.md`:
Collaborator

Now the tests. Following the repo convention of loading routers into node:vm with stubbed models:

Now the tests. Following the repo convention of loading routers into `node:vm` with stubbed models:
Collaborator

Now let me run the test suite:

Now let me run the test suite:
Collaborator

All 262 tests pass. Let me double-check a few integration points — the actor update route (to ensure discordUserId isn't mangled by Foundry sync or the sheet update):

All 262 tests pass. Let me double-check a few integration points — the actor update route (to ensure `discordUserId` isn't mangled by Foundry sync or the sheet update):
Collaborator

The field is untouched by sheet updates and Foundry sync. Final review of the full diff:

The field is untouched by sheet updates and Foundry sync. Final review of the full diff:
Collaborator

The full test suite passes (262/262). Everything is in place. Summary of the change for issue #72:

What was implemented

Feature: each Discord player of a linked campaign can be assigned one character, through the bot.

Backend

  • backend/src/models/Actor.js — new discordUserId field (the player holding the character, null when free) with a partial unique index on (campaign, discordUserId), so a player holds at most one character per campaign. Sheet updates and Foundry sync never touch the field.
  • backend/src/routes/discord.js — two new bot-secret-gated routes, following the existing /bot/guild/:guildId/... conventions (envelope, {status,msg}, guild-link → campaign ownership checks):
    • POST /bot/guild/:guildId/actors/assign — takes a character of the guild's campaign for a Discord user; a second assign moves the player (their previous character is freed). A character already held by somebody else answers discord.errors.actor-held unless the bot says the asker is a server administrator (admin).
    • POST /bot/guild/:guildId/actors/unassign — frees the caller's own character, or anybody's with actorId + admin.
    • GET /bot/guild/:guildId/actors already returns the whole actor documents, so it now carries each character's discordUserId for the bot's listings.

Discord bot

  • /dragonroll assign <character> (name autocomplete) — any player claims a free character for themselves; a server administrator can also take one held by somebody else. Claims a second one frees the first, with a confirmation naming both.
  • /dragonroll unassign [character] — without the option you give up your own character; naming somebody else's is administrator-only.
  • /characters now shows the player linked to each character (🎮 @player, never a ping), and /dragonroll status shows "Your character".
  • Docs updated in discord-bot/README.md; AGENTS.md's Actor model line now lists discordUserId.

Tests

  • New tests/discord-actor-links.test.js (14 tests) loads routes/discord.js into node:vm with stubbed models, like map-tokens.test.js: one-player-one-character, the held-character guard, admin moves/frees, guild-link guards, and the bot's command/autocomplete/route wiring. Ran node --test tests/: 262 pass, 0 fail — I did not run the bot or backend live (needs Discord/Mongo credentials), only syntax checks and the suite.
The full test suite passes (262/262). Everything is in place. Summary of the change for issue #72: ## What was implemented **Feature: each Discord player of a linked campaign can be assigned one character, through the bot.** ### Backend - `backend/src/models/Actor.js` — new `discordUserId` field (the player holding the character, `null` when free) with a partial unique index on `(campaign, discordUserId)`, so a player holds at most one character per campaign. Sheet updates and Foundry sync never touch the field. - `backend/src/routes/discord.js` — two new bot-secret-gated routes, following the existing `/bot/guild/:guildId/...` conventions (envelope, `{status,msg}`, guild-link → campaign ownership checks): - `POST /bot/guild/:guildId/actors/assign` — takes a character of the guild's campaign for a Discord user; a second assign *moves* the player (their previous character is freed). A character already held by somebody else answers `discord.errors.actor-held` unless the bot says the asker is a server administrator (`admin`). - `POST /bot/guild/:guildId/actors/unassign` — frees the caller's own character, or anybody's with `actorId` + `admin`. - `GET /bot/guild/:guildId/actors` already returns the whole actor documents, so it now carries each character's `discordUserId` for the bot's listings. ### Discord bot - `/dragonroll assign <character>` (name autocomplete) — any player claims a free character for themselves; a server administrator can also take one held by somebody else. Claims a second one frees the first, with a confirmation naming both. - `/dragonroll unassign [character]` — without the option you give up your own character; naming somebody else's is administrator-only. - `/characters` now shows the player linked to each character (`🎮 @player`, never a ping), and `/dragonroll status` shows "Your character". - Docs updated in `discord-bot/README.md`; `AGENTS.md`'s `Actor` model line now lists `discordUserId`. ### Tests - New `tests/discord-actor-links.test.js` (14 tests) loads `routes/discord.js` into `node:vm` with stubbed models, like `map-tokens.test.js`: one-player-one-character, the held-character guard, admin moves/frees, guild-link guards, and the bot's command/autocomplete/route wiring. Ran `node --test tests/`: **262 pass, 0 fail** — I did not run the bot or backend live (needs Discord/Mongo credentials), only syntax checks and the suite.
Collaborator

Implementation is ready for review: #78

Implementation is ready for review: https://git.aranroig.com/Syndria98/dragonroll/pulls/78
Agent added help wanted and removed working labels 2026-10-15 17:06:26 +00:00
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Syndria98/dragonroll#72