Skip to content

Commit e8815e5

Browse files
committed
feat(bot): add /sync command
1 parent 4bf983d commit e8815e5

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

api/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ app.post("/admin/:action/:guild/:target", authMiddleware, async (req, res) => {
338338
try {
339339
const [err, success] = await syncFromPolaris(guild);
340340
if (err) {
341+
if(err instanceof Error && err.message === "Server not found in Polaris") {
342+
return res.status(404).json({ message: "Server not found in Polaris" });
343+
}
341344
return res.status(500).json({ message: "Internal server error", err });
342345
} else {
343346
return res.status(200).json(success);
@@ -461,6 +464,9 @@ async function adminRolesAdd(guild: string, role: string, level: number) {
461464
async function syncFromPolaris(guild: string) {
462465
const res = await fetch(`https://gdcolon.com/polaris/api/leaderboard/${guild}`);
463466
const data = await res.json();
467+
if(data.apiError && data.code === "invalidServer") {
468+
return [new Error("Server not found in Polaris"), false];
469+
}
464470
const users = data.leaderboard;
465471
for(let i = 1; i < data.pageInfo.pageCount; i++) {
466472
const res = await fetch(`https://gdcolon.com/polaris/api/leaderboard/${guild}?page=${i + 1}`);

bot/commands.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import client from '.';
44
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, type CommandInteraction, ChannelType, type APIApplicationCommandOption, GuildMember, AttachmentBuilder, ComponentType } from 'discord.js';
55
import { heapStats } from 'bun:jsc';
6-
import { getGuildLeaderboard, makeGETRequest, getRoles, removeRole, addRole, enableUpdates, disableUpdates, getCooldown, setCooldown, getUpdatesChannel, setUpdatesChannel, setXP, setLevel } from './utils/requestAPI';
6+
import { getGuildLeaderboard, makeGETRequest, getRoles, removeRole, addRole, enableUpdates, disableUpdates, getCooldown, setCooldown, getUpdatesChannel, setUpdatesChannel, setXP, setLevel, syncFromPolaris } from './utils/requestAPI';
77
import convertToLevels from './utils/convertToLevels';
88
import quickEmbed from './utils/quickEmbed';
99
import { Font, RankCardBuilder } from 'canvacord';
@@ -721,6 +721,55 @@ const commands: Record<string, Command> = {
721721
return;
722722
}
723723
}
724+
},
725+
sync: {
726+
data: {
727+
options: [{
728+
name: 'bot',
729+
description: 'Select the bot to sync XP data from',
730+
type: 3,
731+
required: true,
732+
choices: [
733+
{
734+
name: 'Polaris',
735+
value: 'polaris',
736+
},
737+
]
738+
}],
739+
name: 'sync',
740+
description: 'Sync XP data from another bot!',
741+
integration_types: [0],
742+
contexts: [0, 2],
743+
},
744+
execute: async (interaction) => {
745+
if (!interaction.memberPermissions?.has('ManageGuild')) {
746+
const errorEmbed = quickEmbed({
747+
color: 'Red',
748+
title: 'Error!',
749+
description: 'Missing permissions: `Manage Server`'
750+
}, interaction);
751+
await interaction.reply({
752+
ephemeral: true,
753+
embeds: [errorEmbed]
754+
})
755+
.catch(console.error);
756+
return;
757+
}
758+
759+
const bot = interaction.options.get('bot')?.value;
760+
761+
let apiSuccess;
762+
switch (bot) {
763+
case 'polaris':
764+
apiSuccess = await syncFromPolaris(interaction.guildId as string);
765+
if (!apiSuccess) {
766+
await interaction.reply({ ephemeral: true, content: 'Error syncing data! This might mean that Polaris is not set up for this server, or the leaderboard for this server is not public.' });
767+
return;
768+
}
769+
await interaction.reply({ ephemeral: true, content: 'Data synced!' });
770+
return;
771+
}
772+
}
724773
}
725774
};
726775

bot/utils/requestAPI.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,17 @@ export async function setCooldown(guild: string, cooldown: number) {
205205
return response.status === 200;
206206
}
207207
//#endregion
208+
209+
//#region Sync
210+
export async function syncFromPolaris(guild: string) {
211+
const response = await fetch(`http://localhost:18103/admin/sync/${guild}/polaris`, {
212+
"headers": {
213+
'Content-Type': 'application/json',
214+
'Authorization': process.env.AUTH as string,
215+
},
216+
"body": JSON.stringify({}),
217+
"method": "POST"
218+
});
219+
return response.status === 200;
220+
}
221+
//#endregion

0 commit comments

Comments
 (0)