UNPKG

kakashi-self-handler

Version:
125 lines (113 loc) 4.08 kB
const { Collection, REST, Routes } = require("discord.js"); const { readdirSync, existsSync } = require("fs"); const { green, yellow, red } = require("chalk"); const path = require("path"); async function handleGuildCommands( kakashi ) { if (!existsSync(kakashi.GuildCommands.path)) throw new Error( red( `{Kakashi Guild Command Handler} - Path provided doesn't exist.\n'${kakashi.GuildCommands.path}'` ) ); const folderPath = kakashi.GuildCommands.path const pathArray = folderPath.split("/"); const commandFolders = readdirSync(folderPath); kakashi.commands = new Collection(); kakashi.commandArray = []; for (const folder of commandFolders) { const commandFiles = readdirSync(`${folderPath}/${folder}`); for (const file of commandFiles) { const command = require(path.join( require.main.path, pathArray[pathArray.length - 1], folder, file )); if (command == {} || !command.data) throw new Error( red( `{Kakashi Guild Command Handler} - Command is missing data.\n'${folderPath}/${folder}/${file}'` ) ); kakashi.commands.set(command.data.name, command); kakashi.commandArray.push(command.data.toJSON()); console.log( green(`{Kakashi Guild Commands Handler} - Registered command "${command.data.name}"`) ); } } try { console.log(green("{Kakashi Guild Commands Handler} - Started refreshing application (/) commands.")); const rest = new REST({ version: "9" }).setToken(kakashi.GuildCommands.token); await rest.put(Routes.applicationGuildCommands(kakashi.GuildCommands.clientId, kakashi.GuildCommands.guildId), { body: kakashi.commandArray, }); console.log( green( "{Kakashi Guild Commands Handler} - Successfully reloaded application (/) commands. They're ready to use!" ) ); } catch (error) { throw new Error( red( `{Kakashi Guild Command Error Handler} - ${error}` ) ); } } async function handleGlobalCommands(kakashi) { if (!existsSync(kakashi.GuildCommands.path)) throw new Error( red( `{Kakashi Guild Command Handler} - Path provided doesn't exist.\n'${kakashi.GlobalCommands.path}'` ) ); const folderPath = kakashi.GlobalCommands.path const pathArray = folderPath.split("/"); const commandFolders = readdirSync(folderPath); kakashi.commands = new Collection(); kakashi.commandArray = []; for (const folder of commandFolders) { const commandFiles = readdirSync(`${folderPath}/${folder}`); for (const file of commandFiles) { const command = require(path.join( require.main.path, pathArray[pathArray.length - 1], folder, file )); if (command == {} || !command.data) throw new Error( red( `{Kakashi Global Command Handler} - Command is missing data.\n'${folderPath}/${folder}/${file}'` ) ); kakashi.commands.set(command.data.name, command); kakashi.commandArray.push(command.data.toJSON()); console.log( green(`{Kakashi Global Commands Handler} - Registered command "${command.data.name}"`) ); } try { console.log(green("{Kakashi Global Commands Handler} - Started refreshing application (/) commands.")); const rest = new REST({ version: "9" }).setToken(kakashi.GlobalCommands.token); await rest.put(Routes.applicationCommands(kakashi.GlobalCommands.clientId), { body: kakashi.commandArray, }); console.log( green( "{Kakashi Global Commands Handler} - Successfully reloaded application (/) commands. Since you're using Global commands, updates to the code can take time to show across guilds." ) ); } catch (error) { throw new Error( red( `{Kakashi Global Command Error Handler} - ${error}` ) ); } } } module.exports = {handleGuildCommands, handleGlobalCommands}