From 2d6e65a1e6073f605aab192b8cea33037a04af2c Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sun, 28 Jan 2024 22:26:24 -0600 Subject: [PATCH] feat: make application id optional, thanks @trueharuu (#130) make application id optional, thanks @trueharuu --- src/commands/command-clear.ts | 4 +--- src/commands/publish.ts | 2 +- src/create-publish.mts | 7 +++---- src/rest.ts | 14 ++++++++++++-- src/types/config.d.ts | 2 +- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/commands/command-clear.ts b/src/commands/command-clear.ts index 5302dca..35555a5 100644 --- a/src/commands/command-clear.ts +++ b/src/commands/command-clear.ts @@ -23,9 +23,7 @@ const getConfirmation = (args: Record ) => { export async function commandClear(args: Record) { dotenv.configDotenv({ path: args.env || resolve('.env') }) const token = process.env.token || process.env.DISCORD_TOKEN; - const appid = process.env.applicationId || process.env.APPLICATION_ID; assert(token, 'Could not find a token for this bot in .env or commandline. Do you have DISCORD_TOKEN in env?'); - assert(appid, 'Could not find an application id for this bot in .env or commandline. Do you have APPLICATION_ID in env?'); const confirmation = await getConfirmation(args); @@ -34,7 +32,7 @@ export async function commandClear(args: Record) { text: `Deleting ALL application commands...`, spinner: 'aesthetic', }).start(); - const rest = Rest.create(appid, token); + const rest = await Rest.create(token); let guildCommands: Record try { guildCommands = JSON.parse(readFileSync('.sern/command-data-remote.json', 'utf-8')) diff --git a/src/commands/publish.ts b/src/commands/publish.ts index 781c662..3e5616f 100644 --- a/src/commands/publish.ts +++ b/src/commands/publish.ts @@ -15,7 +15,7 @@ export async function publish(commandDir: string | undefined, args: Partial { @@ -181,7 +180,7 @@ const spin = ora(`Publishing ${cyanBright('Global')} commands`); globalCommands.length && spin.start(); -const rest = Rest.create(appid, token); +const rest = await Rest.create(token); const res = await rest.updateGlobal(globalCommands); let globalCommandsResponse: unknown; diff --git a/src/rest.ts b/src/rest.ts index a9c89dd..c7106ff 100644 --- a/src/rest.ts +++ b/src/rest.ts @@ -11,12 +11,22 @@ const publishablesIntoJson = (ps: PublishableModule[]) => 4 ); -export const create = (appid: string, token: string) => { - const globalURL = new URL(`${appid}/commands`, baseURL); +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, { diff --git a/src/types/config.d.ts b/src/types/config.d.ts index bc9dae0..0179a39 100644 --- a/src/types/config.d.ts +++ b/src/types/config.d.ts @@ -13,7 +13,7 @@ export interface sernConfig { export interface TheoreticalEnv { DISCORD_TOKEN: string; - APPLICATION_ID: string; + APPLICATION_ID?: string; MODE: 'PROD' | 'DEV'; [name: string]: string; }