130 lines
5.1 KiB
JavaScript
130 lines
5.1 KiB
JavaScript
class CampaignNotLinkedError extends Error {
|
|
constructor(guildId) {
|
|
super(`No Dragonroll campaign is linked to Discord server ${guildId}.`);
|
|
this.name = 'CampaignNotLinkedError';
|
|
this.guildId = guildId;
|
|
}
|
|
}
|
|
|
|
class DragonrollNotConnectedError extends Error {
|
|
constructor(guildId) {
|
|
super(`No Dragonroll account is connected to Discord server ${guildId}.`);
|
|
this.name = 'DragonrollNotConnectedError';
|
|
this.guildId = guildId;
|
|
}
|
|
}
|
|
|
|
function createDragonrollApi(config) {
|
|
async function request(path, options = {}) {
|
|
if (!config.discordBotSecret) {
|
|
throw new Error('Set DRAGONROLL_DISCORD_BOT_SECRET in the bot and backend environments before using Dragonroll commands.');
|
|
}
|
|
|
|
const response = await fetch(`${config.apiBaseUrl}${path}`, {
|
|
...options,
|
|
headers: {
|
|
...(options.body ? { 'Content-Type': 'application/json' } : {}),
|
|
'X-Dragonroll-Discord-Bot-Secret': config.discordBotSecret,
|
|
...options.headers
|
|
}
|
|
});
|
|
|
|
let payload;
|
|
try {
|
|
payload = await response.json();
|
|
} catch {
|
|
const error = new Error(`Dragonroll API returned an unreadable response (${response.status}).`);
|
|
error.status = response.status;
|
|
throw error;
|
|
}
|
|
|
|
if (!response.ok || payload.status === 'error') {
|
|
const error = new Error(payload.msg || `Dragonroll API request failed (${response.status}).`);
|
|
error.status = response.status;
|
|
error.code = payload.msg;
|
|
throw error;
|
|
}
|
|
return payload;
|
|
}
|
|
|
|
// The backend hands out the sign-up link the person opens to pick a campaign,
|
|
// so the bot never builds one itself: every service shows the same link.
|
|
async function startAuthorization(guildId, guildName, discordUserId, discordUserName) {
|
|
const payload = await request('/link/start', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
service: 'discord',
|
|
target: {
|
|
id: String(guildId),
|
|
name: guildName,
|
|
user: discordUserName,
|
|
meta: { userId: String(discordUserId) }
|
|
}
|
|
})
|
|
});
|
|
if (!payload.linkUrl) throw new Error('Dragonroll did not provide a sign-up link. Update the Dragonroll backend.');
|
|
return payload;
|
|
}
|
|
|
|
async function claimLinkNotifications() {
|
|
const payload = await request('/discord/bot/link-requests/notifications', { method: 'POST' });
|
|
return Array.isArray(payload.notifications) ? payload.notifications : [];
|
|
}
|
|
|
|
async function getStatus(guildId) { return request(`/discord/bot/guild/${encodeURIComponent(guildId)}/status`);
|
|
}
|
|
|
|
async function listCampaigns(guildId) {
|
|
const payload = await request(`/discord/bot/guild/${encodeURIComponent(guildId)}/campaigns`);
|
|
if (!Array.isArray(payload.campaigns)) {
|
|
throw new Error(payload.msg || 'Dragonroll returned an invalid campaign list.');
|
|
}
|
|
return payload.campaigns;
|
|
}
|
|
|
|
async function linkCampaign(guildId, campaignId) {
|
|
const payload = await request(`/discord/bot/guild/${encodeURIComponent(guildId)}/campaign`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ campaignId })
|
|
});
|
|
if (!payload.campaign) throw new Error('Dragonroll returned an invalid campaign link.');
|
|
return payload.campaign;
|
|
}
|
|
|
|
async function unlinkCampaign(guildId) {
|
|
const current = await getCampaign(guildId);
|
|
await request(`/discord/bot/guild/${encodeURIComponent(guildId)}/campaign`, { method: 'DELETE' });
|
|
return current;
|
|
}
|
|
|
|
async function getCampaign(guildId) {
|
|
const key = String(guildId);
|
|
const status = await getStatus(key);
|
|
if (!status.connected) throw new DragonrollNotConnectedError(key);
|
|
if (!status.campaign) throw new CampaignNotLinkedError(key);
|
|
|
|
const campaign = {
|
|
id: String(status.campaign._id ?? status.campaign.id),
|
|
name: status.campaign.name || 'Untitled campaign'
|
|
};
|
|
if (!campaign.id || campaign.id === 'undefined') throw new Error('Dragonroll returned an invalid campaign.');
|
|
return campaign;
|
|
}
|
|
|
|
async function listActors(guildId) {
|
|
const payload = await request(`/discord/bot/guild/${encodeURIComponent(guildId)}/actors`);
|
|
if (!Array.isArray(payload.actors) || !payload.campaign) {
|
|
throw new Error(payload.msg || 'Dragonroll returned an invalid actor list.');
|
|
}
|
|
const campaign = {
|
|
id: String(payload.campaign.id ?? payload.campaign._id),
|
|
name: payload.campaign.name || 'Untitled campaign'
|
|
};
|
|
return { campaign, actors: payload.actors };
|
|
}
|
|
|
|
return { startAuthorization, claimLinkNotifications, getStatus, getCampaign, listCampaigns, linkCampaign, unlinkCampaign, listActors };
|
|
}
|
|
|
|
module.exports = { createDragonrollApi, CampaignNotLinkedError, DragonrollNotConnectedError };
|