-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 657853f
Showing
6 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package-lock.json | ||
package.json | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |