Port a commands and utils modules from getting-started
This commit is contained in:
parent
50febb14bd
commit
494576d19c
3 changed files with 105 additions and 1 deletions
47
commands.js
Normal file
47
commands.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue