Foundry and discord bot
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
This commit is contained in:
117
discord-bot/src/dragonroll-api.js
Normal file
117
discord-bot/src/dragonroll-api.js
Normal file
@@ -0,0 +1,117 @@
|
||||
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;
|
||||
}
|
||||
|
||||
async function startAuthorization(guildId, guildName, discordUserId, discordUserName) {
|
||||
return request('/discord/link/start', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ guildId, guildName, discordUserId, discordUserName })
|
||||
});
|
||||
}
|
||||
|
||||
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 };
|
||||
Reference in New Issue
Block a user