mirror of
https://github.com/cassoule/flopobot_v2.git
synced 2026-03-18 21:40:27 +01:00
throw API errors
This commit is contained in:
@@ -15,17 +15,19 @@ async function HasGuildCommand(appId, guildId, command) {
|
||||
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('Error installing commands: ', err);
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +39,7 @@ export async function InstallGuildCommand(appId, guildId, command) {
|
||||
try {
|
||||
await DiscordRequest(endpoint, { method: 'POST', body: command });
|
||||
} catch (err) {
|
||||
console.error('Error installing commands: ', err);
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
12
utils.js
12
utils.js
@@ -15,19 +15,27 @@ export function VerifyDiscordRequest(clientKey) {
|
||||
};
|
||||
}
|
||||
|
||||
export function DiscordRequest(endpoint, options) {
|
||||
export async function DiscordRequest(endpoint, options) {
|
||||
// append endpoint to root API URL
|
||||
const url = 'https://discord.com/api/v9/' + endpoint;
|
||||
// Stringify payloads
|
||||
if (options.body) options.body = JSON.stringify(options.body);
|
||||
// Use node-fetch to make requests
|
||||
return fetch(url, {
|
||||
const res = await fetch(url, {
|
||||
headers: {
|
||||
Authorization: `Bot ${process.env.DISCORD_TOKEN}`,
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
},
|
||||
...options
|
||||
});
|
||||
// throw API errors
|
||||
if (!res.ok) {
|
||||
const data = await res.json();
|
||||
console.log(res.status);
|
||||
throw new Error(JSON.stringify(data));
|
||||
}
|
||||
// return original response
|
||||
return res;
|
||||
}
|
||||
|
||||
// Simple method that returns a random emoji from list
|
||||
|
||||
Reference in New Issue
Block a user