Fix issue #72: Link actor with a discord user
All checks were successful
Test / test (push) Successful in 2s
All checks were successful
Test / test (push) Successful in 2s
This commit is contained in:
@@ -16,6 +16,11 @@ function validGuildId(value) {
|
||||
return /^\d{5,25}$/.test(String(value ?? ''));
|
||||
}
|
||||
|
||||
// Discord ids are snowflakes the same shape as guild ids.
|
||||
function validUserId(value) {
|
||||
return /^\d{5,25}$/.test(String(value ?? ''));
|
||||
}
|
||||
|
||||
// Connecting a Discord server is the service link of issue: the bot asks
|
||||
// /api/link/start for a sign-up link, and whoever opens it signs in and picks the
|
||||
// campaign this server may read. What is left here is what the bot needs once
|
||||
@@ -179,4 +184,92 @@ router.get('/bot/guild/:guildId/actors', requireBot, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Who may take a character already held by somebody else is decided in Discord,
|
||||
// where the server administrators are: the bot authenticates with the shared
|
||||
// secret and says whether the person asking is one (`admin`). Everything else
|
||||
// the backend still guards itself: only the guild's own campaign is reachable.
|
||||
|
||||
// One command per player link (Syndria98/dragonroll#72): assigning takes the
|
||||
// character for `discordUserId`, and a player never holds two of them, so a
|
||||
// second assign moves the player instead of doubling them up.
|
||||
router.post('/bot/guild/:guildId/actors/assign', requireBot, async (req, res) => {
|
||||
try {
|
||||
const guildId = String(req.params.guildId);
|
||||
const discordUserId = String(req.body.discordUserId ?? '');
|
||||
if (!validGuildId(guildId) || !validUserId(discordUserId) || !req.body.actorId) {
|
||||
return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
}
|
||||
|
||||
const link = await DiscordGuildLink.findOne({ guildId }).lean();
|
||||
if (!link) return res.json({ status: 'error', msg: 'discord.errors.not-connected' });
|
||||
const campaign = await Campaign.findOne({ discordGuildId: guildId, createdBy: link.dragonrollUser }).select('_id name').lean();
|
||||
if (!campaign) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
|
||||
const actor = await Actor.findOne({ _id: req.body.actorId, campaign: campaign._id });
|
||||
if (!actor) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
|
||||
const holder = String(actor.discordUserId ?? '');
|
||||
if (holder && holder !== discordUserId && !req.body.admin) {
|
||||
return res.json({ status: 'error', msg: 'discord.errors.actor-held' });
|
||||
}
|
||||
|
||||
// The character this player hands over to take the new one.
|
||||
const previous = await Actor.findOne({ campaign: campaign._id, discordUserId, _id: { $ne: actor._id } }).lean();
|
||||
if (previous) {
|
||||
await Actor.updateMany(
|
||||
{ campaign: campaign._id, discordUserId, _id: { $ne: actor._id } },
|
||||
{ $set: { discordUserId: null } }
|
||||
);
|
||||
}
|
||||
|
||||
actor.discordUserId = discordUserId;
|
||||
await actor.save();
|
||||
|
||||
res.json({
|
||||
status: 'ok',
|
||||
actor: { id: String(actor._id), name: actor.name, discordUserId },
|
||||
previous: previous ? { id: String(previous._id), name: previous.name } : null
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.json({ status: 'error', msg: 'errors.internal' });
|
||||
}
|
||||
});
|
||||
|
||||
// Freeing a character: without `actorId` the player gives up their own, with
|
||||
// one a server administrator frees anybody's.
|
||||
router.post('/bot/guild/:guildId/actors/unassign', requireBot, async (req, res) => {
|
||||
try {
|
||||
const guildId = String(req.params.guildId);
|
||||
const discordUserId = String(req.body.discordUserId ?? '');
|
||||
if (!validGuildId(guildId) || !validUserId(discordUserId)) {
|
||||
return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
}
|
||||
|
||||
const link = await DiscordGuildLink.findOne({ guildId }).lean();
|
||||
if (!link) return res.json({ status: 'error', msg: 'discord.errors.not-connected' });
|
||||
const campaign = await Campaign.findOne({ discordGuildId: guildId, createdBy: link.dragonrollUser }).select('_id name').lean();
|
||||
if (!campaign) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
|
||||
const filter = { campaign: campaign._id };
|
||||
if (req.body.actorId) filter._id = req.body.actorId;
|
||||
else filter.discordUserId = discordUserId;
|
||||
const actor = await Actor.findOne(filter);
|
||||
if (!actor || !actor.discordUserId) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
|
||||
const holder = String(actor.discordUserId);
|
||||
if (holder !== discordUserId && !req.body.admin) {
|
||||
return res.json({ status: 'error', msg: 'discord.errors.actor-held' });
|
||||
}
|
||||
|
||||
actor.discordUserId = null;
|
||||
await actor.save();
|
||||
|
||||
res.json({ status: 'ok', actor: { id: String(actor._id), name: actor.name }, wasYours: holder === discordUserId });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.json({ status: 'error', msg: 'errors.internal' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user