From 657853f5f5904a9a2373dec626422e136d10aef6 Mon Sep 17 00:00:00 2001 From: Lars de Kooter Date: Wed, 8 Jun 2022 19:30:08 +0200 Subject: [PATCH] Launch --- .gitignore | 3 ++ README.md | 2 + Structures/Command.js | 25 +++++++++ Structures/CustomError.js | 24 +++++++++ Structures/KooterHandler.js | 102 ++++++++++++++++++++++++++++++++++++ Structures/index.js | 5 ++ 6 files changed, 161 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 Structures/Command.js create mode 100644 Structures/CustomError.js create mode 100644 Structures/KooterHandler.js create mode 100644 Structures/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..55467c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +package-lock.json +package.json +node_modules/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a62b26b --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# KooterHandler +This is a command handler that works as far as i know. It works for discord.js \ No newline at end of file diff --git a/Structures/Command.js b/Structures/Command.js new file mode 100644 index 0000000..070c4c7 --- /dev/null +++ b/Structures/Command.js @@ -0,0 +1,25 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const Discord = require('discord.js'); + +/** + * + * @param {Discord.CommandInteraction} interaction + * @param {Discord.Client} client + */ +function ExecuteFunction(interaction, client) {}; + +class Command { + /** + * @typedef {{ data: SlashCommandBuilder, test: boolean, name: string execute: ExecuteFunction}} CommandOptions + * @param {CommandOptions} options + */ + constructor(options) { + this.name = options.name, + this.data = options.data, + this.test = options.test; + this.execute = options.execute; + + } +} + +module.exports = Command; \ No newline at end of file diff --git a/Structures/CustomError.js b/Structures/CustomError.js new file mode 100644 index 0000000..c4e3cd3 --- /dev/null +++ b/Structures/CustomError.js @@ -0,0 +1,24 @@ +class CustomError extends Error { + /** + * @typedef {Object} ErrorOptions + * @property {String} message + * @property {Number} code + */ + /** + * @param {ErrorOptions} error + */ + constructor(error) { + super(); + /** + * The error message + * @type {String} + */ + this.message = error.message; + /** + * The error code + * @type {Number | string} + */ + this.code = error.code; + } +} +module.exports = CustomError; \ No newline at end of file diff --git a/Structures/KooterHandler.js b/Structures/KooterHandler.js new file mode 100644 index 0000000..46d4a18 --- /dev/null +++ b/Structures/KooterHandler.js @@ -0,0 +1,102 @@ +const CustomError = require('./CustomError') +const path = require('path'); +const {Client, Collection} = require('discord.js'); +const { readdirSync } = require("fs"); +const { dirname } = require("path"); + +class KooterHandler { + /** + * @typedef {Object} KooterHandlerOptions + * @property {string} [CommandsFolder] The folder name the commands are located + * @property {string} [EventsFolder] The folder name the events are located + * @property {string[]} guildIds Array of test guild ids + * @property {string[]} ownerIds Array of bot owner IDs + * @property {Boolean} loadCommands + */ + /** + * @param {Client} client + * @param {KooterHandlerOptions} options + */ + constructor(client, options) { + if(!options || !options.guildIds || !options.ownerIds || !client || !client instanceof Client) throw new CustomError({ message: 'Failed to load assets. Missing information', code: 'MISSING_INFORMATION' }); + // Command Handler + if(options.CommandsFolder) { + const commandFolder = path.join(__dirname.split('node_modules\\')[0], options.CommandsFolder); + const commandFiles = readdirSync(commandFolder).filter(file => file.endsWith('.js')); + + client.commands = new Collection(); + commandFiles.forEach(file => { + const command = require(`${commandFolder}/${file}`); + client.commands.set(command.data.name, command) + }); + + client.on('interactionCreate', async (interaction) => { + if(!interaction.isCommand()) return; + const command = client.commands.get(interaction.commandName) + if(!command) throw new CustomError({ message: 'Didnt find the command that ran!', code: 'COMMAND_NOT_FOUND' }); + try { + await command.execute(interaction, client); + } catch (error) { + console.error(error) + if(interaction.replied || interaction.deferred) return await interaction.editReply('Oops: something went wrong on our side!'); + await interaction.reply('Oops: something went wrong on our side!'); + } + }) + } + // Event Handler + if(options.EventsFolder) { + const eventsFolder = path.join(__dirname.split('node_modules\\')[0], options.EventsFolder); + readdirSync(eventsFolder) + .filter(file => file.endsWith('.js')) + .forEach(file => { + const event = require(`${eventsFolder}/${file}`); + client.on(event.event, event.run.bind(null, client)) + }) + } + // Loading Commands + // console.log(options.loadCommands) + if(options.loadCommands == true) { + console.log('hey') + const commandFolder = path.join(__dirname.split('node_modules\\')[0], options.CommandsFolder); + const commandFiles = readdirSync(commandFolder).filter(file => file.endsWith('.js')); + + const privateCommands = []; + const commands = []; + + commandFiles.forEach(file => { + const command = require(`${commandFolder}/${file}`); + if(command.test === true) privateCommands.push(command.data.toJSON()) + else commands.push(command.data.toJSON()) + }); + (async () => { + function load() { + return new Promise(async (resolve, reject) => { + try { + options.guildIds.forEach(async id => { + await client.guilds.cache.get(id).commands.set(privateCommands); + // await wait() + console.log('loaded') + resolve() + }) + } catch (error) { + reject(error) + } + }) + } + await load() + console.log('Loaded all commands') + })() + } + } +}; + + + + +function wait() { + return new Promise(resolve => { + setTimeout(resolve, 3000); + }) +} + +module.exports = KooterHandler \ No newline at end of file diff --git a/Structures/index.js b/Structures/index.js new file mode 100644 index 0000000..9f0cab3 --- /dev/null +++ b/Structures/index.js @@ -0,0 +1,5 @@ +const KooterHandler = require('./KooterHandler'); +const Command = require('./Command'); + +module.exports.KooterHandler = KooterHandler; +module.exports.Command = Command; \ No newline at end of file