Skip to content

Commit

Permalink
Launch
Browse files Browse the repository at this point in the history
  • Loading branch information
larsdekooter committed Jun 8, 2022
0 parents commit 657853f
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package-lock.json
package.json
node_modules/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# KooterHandler
This is a command handler that works as far as i know. It works for discord.js
25 changes: 25 additions & 0 deletions Structures/Command.js
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions Structures/CustomError.js
Original file line number Diff line number Diff line change
@@ -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;
102 changes: 102 additions & 0 deletions Structures/KooterHandler.js
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions Structures/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const KooterHandler = require('./KooterHandler');
const Command = require('./Command');

module.exports.KooterHandler = KooterHandler;
module.exports.Command = Command;

0 comments on commit 657853f

Please sign in to comment.