From 303ac0280c7c7c55f2670d49c9685b911670bc05 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sat, 27 Jan 2024 23:29:37 -0600 Subject: [PATCH] feat: command clear (#128) --- src/commands/command-clear.ts | 62 +++++++++++++++++++++++++++++++++++ src/commands/list.ts | 8 +++-- src/index.ts | 12 ++++--- 3 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 src/commands/command-clear.ts diff --git a/src/commands/command-clear.ts b/src/commands/command-clear.ts new file mode 100644 index 0000000..5302dca --- /dev/null +++ b/src/commands/command-clear.ts @@ -0,0 +1,62 @@ +import * as Rest from '../rest.js' +import assert from 'node:assert' +import dotenv from 'dotenv' +import ora from 'ora'; +import type { CommandData, GuildId } from '../utilities/types.js'; +import { readFileSync, writeFile } from 'node:fs' +import { resolve } from 'node:path' +import prompts from 'prompts'; + +const getConfirmation = (args: Record ) => { + if(args.yes) { + return args.yes + } else { + return prompts({ + type: 'confirm', + name: 'confirmation', + message: 'Are you sure you want to delete ALL your application commands?', + initial: true + }, { onCancel: () => (console.log("Cancelled operation ( ̄┰ ̄*)"), process.exit(1)) }) + .then(response => response.confirmation); + } +} +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); + + if (confirmation) { + const spin = ora({ + text: `Deleting ALL application commands...`, + spinner: 'aesthetic', + }).start(); + const rest = Rest.create(appid, token); + let guildCommands: Record + try { + guildCommands = JSON.parse(readFileSync('.sern/command-data-remote.json', 'utf-8')) + await rest.updateGlobal([]); + delete guildCommands.global + for (const guildId in guildCommands) { + await rest.putGuildCommands(guildId, []); + } + writeFile('.sern/command-data-remote.json', "{}", (err) => { + if(err) { + spin.fail("Error happened while writing to json:"); + console.error(err) + process.exit(1) + } + }) + spin.succeed(); + } catch(e) { + spin.fail("Something went wrong. "); + throw e; + } + } else { + console.log('Operation canceled. ( ̄┰ ̄*)'); + } + +} diff --git a/src/commands/list.ts b/src/commands/list.ts index 66fa974..d071ee3 100644 --- a/src/commands/list.ts +++ b/src/commands/list.ts @@ -14,9 +14,11 @@ export function list() { const globalCommands = commands.global; delete commands.global; - console.log(bold('Global Commands')); - for (const command of globalCommands) log(command); - + if(globalCommands) { + console.log(bold('Global Commands')); + for (const command of globalCommands) log(command); + } + console.log('\t'); for (const guildId in commands) { diff --git a/src/index.ts b/src/index.ts index 0bfab5a..0c58b01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,12 +43,16 @@ program // .option('--appId [applicationId]') .argument('[path]', 'path with respect to current working directory that will locate all published files') .action(async (...args) => importDynamic('publish.js').then((m) => m.publish(...args))) - ) - .addCommand( + ).addCommand( new Command('list') // .description('List all slash commands') - .action(async (...args) => importDynamic('list.js').then((m) => m.list(...args))) - ); + .action(async (...args) => importDynamic('list.js').then((m) => m.list(...args)))) + .addCommand( + new Command('clear') + .description('Clear and reset commands-data-remote.json and the api') + .option('-y, --yes', "Say yes to all prompts") + .option('-e, --env [path]', "Supply a path to a .env") + .action(async (...args) => importDynamic('command-clear.js').then((m) => m.commandClear(...args)))); program .command('build')