Skip to content

Commit

Permalink
command clear
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jan 25, 2024
1 parent 3f994d6 commit 55c54c2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
46 changes: 46 additions & 0 deletions src/commands/command-clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as Rest from '../rest.js'
import assert from 'node:assert'
import dotenv from 'dotenv'
import type { CommandData, GuildId } from '../utilities/types.js';
import { readFileSync, writeFile } from 'node:fs'
import { resolve } from 'node:path'
import prompts from 'prompts';

export async function command_clear(args: Record<string,any>) {
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 response = await prompts({
type: 'confirm',
name: 'confirmation',
message: 'Are you sure you want to delete your application commands?',
initial: true
}, { onCancel: () => (console.log("Cancelled operation ( ̄┰ ̄*)"), process.exit(0)) });
if (response.confirmation) {
console.log('Deleting application commands...');
const rest = Rest.create(appid, token);
let guildCommands: Record<GuildId, CommandData[]>
try {
guildCommands = JSON.parse(readFileSync('.sern/command-data-remote.json', 'utf-8'))
} catch(e) {
console.error("Something went wrong while trying to fetch .sern/command-data-remote.json")
console.error("Have you published your commands yet?")
console.error("cannot properly delete all guild commands without this")
throw e;
}
await rest.updateGlobal([]);
delete guildCommands.global
for (const guildId in guildCommands) {
await rest.putGuildCommands(guildId, []);
}
writeFile('.sern/command-data-remote.json', "{}", (err) => {
if(err) throw err
})
} else {
console.log('Operation canceled. ( ̄┰ ̄*)');
}

}
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.command_clear(...args))));

program
.command('build')
Expand Down

0 comments on commit 55c54c2

Please sign in to comment.