From f921a6760847fcf559168001e58da90b01957682 Mon Sep 17 00:00:00 2001 From: Nathaniel Mason Date: Fri, 22 Apr 2022 21:01:17 +0100 Subject: [PATCH] better support for sanboxing commands --- config-example.json | 8 ++++++-- start.js | 50 +++++++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/config-example.json b/config-example.json index 7f2ecde..a89d9ee 100644 --- a/config-example.json +++ b/config-example.json @@ -5,8 +5,12 @@ "release": "anitrox_dev", "build": "Stable", "footerTxt": "Anitrox, made with <3 by IDeletedSystem64 | 2018-2022", - "sandbox": false, - "sandboxGuild": "Set the value above to true and put a server ID here to use local slash commands instead of global. Used for debugging purposes", + "sandbox": { + "enabled": false, + "id": "788391989460205571", + "refreshLocal": false, + "refreshGlobal": false + }, "statuses": [ "with np!help", diff --git a/start.js b/start.js index f83cf76..b8a21d2 100755 --- a/start.js +++ b/start.js @@ -32,27 +32,41 @@ client.generateErrorMessage = (errorMsg, avatarURL) => ({ client.on('error', (e) => console.log(`[ERROR] ${e}`)); client.on('warn', (e) => (`[WARN] ${e}`)); client.once('ready', async () => { - // const commands = config.sandbox ? client.guilds.cache.get(config.sandboxGuild)?.commands : client.application.commands; + const sandboxSettings = config.sandbox; + const localCommands = client.guilds.cache.get(sandboxSettings.id)?.commands; + const globalCommands = client.application.commands; + let existingLocal = await localCommands.fetch(); + let existingGlobal = await globalCommands.fetch(); - // Be careful about running the code below, there's a 200-per-day limit on creating slash commands - // if (config.sandbox) { - // console.log('deleting previous commands from sandbox'); - // const localCommands = await commands.fetch(); - // localCommands.forEach(async x => { - // await commands.delete(x); - // }); + if (sandboxSettings.enabled) { + if (sandboxSettings.refreshLocal) { + console.log('deleting previous local commands'); + existingLocal.forEach(async x => { + await localCommands.delete(x); + }); + existingLocal = new Discord.Collection(); + } - // console.log('deleting global commands'); - // const globalCommands = await client.application.commands.fetch(); - // globalCommands.forEach(async x => { - // await client.application.commands.delete(x); - // }); - // } + if (sandboxSettings.refreshGlobal) { + console.log('deleting previous global commands'); + existingGlobal.forEach(async x => { + await client.application.commands.delete(x); + }); + existingGlobal = new Discord.Collection(); + } + } - // client.commands.forEach(async command => { - // await commands.create(command); - // console.log(command); - // }); + client.commands.forEach(async command => { + console.log(command); + if (sandboxSettings.enabled && !existingLocal.map(x => x.name).includes(command.name)) { + await localCommands.create(command); + // console.log(`created new local command ${command.name}`); + } + if (!existingGlobal.map(x => x.name).includes(command.name)) { + await globalCommands.create(command); + // console.log(`created new global command ${command.name}`); + } + }); console.clear(); console.log(' ___ _ __ ');