thirteenth-friend/commands.js

47 lines
1.4 KiB
JavaScript

import { discordRequest } from './utils.js';
export const TEST_COMMAND = {
name: 'test',
description: 'Basic guild command',
type: 1,
};
export async function hasGuildCommands(appId, guildId, commands) {
if (guildId === '' || appId === '') {
return;
}
commands.forEach((c) => hasGuildCommand(appId, guildId, c));
}
async function hasGuildCommand(appId, guildId, command) {
const endpoint = `applications/${appId}/guilds/${guildId}/commands`;
try {
const res = await discordRequest(endpoint, { method: 'GET' });
const data = await res.json();
if (data) {
const installedNames = data.map((c) => c['name']);
// This is just matching on the name, so it's not good for updates
if (!installedNames.includes(command['name'])) {
console.log(`Installing "${command['name']}"`);
installGuildCommand(appId, guildId, command);
} else {
console.log(`"${command['name']}" command already installed`);
}
}
} catch (err) {
console.error(err);
}
}
export async function installGuildCommand(appId, guildId, command) {
const endpoint = `applications/${appId}/guilds/${guildId}/commands`;
try {
await discordRequest(endpoint, { method: 'POST', body: command });
} catch (err) {
console.error(err);
}
}