mirror of
https://github.com/sern-handler/cli
synced 2026-06-06 01:16:53 +00:00
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import type { PublishableModule } from './create-publish.d.ts';
|
|
|
|
const baseURL = new URL('https://discord.com/api/v10/applications/');
|
|
|
|
const excludedKeys = new Set(['command', 'absPath']);
|
|
|
|
const publishablesIntoJson = (ps: PublishableModule[]) =>
|
|
JSON.stringify(
|
|
ps.map((module) => module.data),
|
|
(key, value) => (excludedKeys.has(key) ? undefined : value),
|
|
4
|
|
);
|
|
|
|
export const create = async (token: string) => {
|
|
const headers = {
|
|
Authorization: 'Bot ' + token,
|
|
'Content-Type': 'application/json',
|
|
};
|
|
let me;
|
|
let appid: string;
|
|
try {
|
|
me = await fetch(new URL('@me', baseURL), { headers }).then(res => res.json());
|
|
appid = me.id;
|
|
} catch(e) {
|
|
console.log("Something went wrong while trying to fetch your application:");
|
|
throw e;
|
|
}
|
|
const globalURL = new URL(`${appid}/commands`, baseURL);
|
|
|
|
return {
|
|
updateGlobal: (commands: PublishableModule[]) =>
|
|
fetch(globalURL, {
|
|
method: 'PUT',
|
|
body: publishablesIntoJson(commands),
|
|
headers,
|
|
}),
|
|
getGuildCommands: (id: string) => {
|
|
const guildCommandURL = new URL(`${appid}/guilds/${id}/commands`, baseURL);
|
|
return fetch(guildCommandURL, { headers });
|
|
},
|
|
|
|
putGuildCommands: (guildId: string, guildCommand: unknown) => {
|
|
const guildCommandURL = new URL(`${appid}/guilds/${guildId}/commands`, baseURL);
|
|
return fetch(guildCommandURL, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(guildCommand),
|
|
headers,
|
|
});
|
|
},
|
|
};
|
|
};
|