kakashi-self-handler
Version:
package handler for discord bot
286 lines (248 loc) • 8.18 kB
JavaScript
const handleEvents = require('./handleEvents');
const handletopGG = require('./topGG')
const {handleGlobalCommands, handleGuildCommands} = require('./handleCommand')
const handleComponents = require('./Components')
const handleInteraction = require('./Interaction')
const handleMessageCommand = require('./MessageCommand')
const handleMessage = require('./Message')
const {bgYellow, yellow, red} = require('chalk')
class Kakashi {
constructor(client, options) {
if (!client) {
throw new Error("{Kakashi Client} - A valid Discord client must be provided.");
}
if (!options || typeof options !== 'object') {
throw new Error(red("{Kakashi Client} - Options must be provided as an object."));
}
this.client = client;
this.vote = this.validateVoteOptions(options.vote|| {
url: "",
webhook: {
token: "",
port: "",
auth: "",
paht: '',
},
embed: {
enabled: false,
message: false,
emoji: false,
footer: false,
},
button: {
enabled: true,
emoji: false
}
});
this.cooldown = this.validateCooldownOptions(options.cooldown || {
embed: {
enabled: false,
message: "",
footer: false,
emoji: false,
},
button: {
enabled: false,
}
});
this.support = this.validateSupportOptions(options.support || {
server: "",
button: {
enabled: false,
emoji: false,
}
});
this.GuildCommands = this.validateGuildCommands(options.GuildCommands)|| {
path: "",
clientId: "",
guildId: "",
token: ""
};
this.GlobalCommands = this.validateGlobalCommands(options.GlobalCommands)|| {
path: "",
clientId: "",
token: ""
};
this.MessageCommands = this.validateMessageCommands(options.MessageCommands) || {
path: "",
};
this.Events = this.validateEventsOptions(options.Events);
this.Components = this.validateComponentOptions(options.Components) || {
path: "",
};
console.log(bgYellow("{Kakashi Handler} - Welcome to the system"));
if(this.vote) {
handletopGG(this)
}
console.log(yellow("{Kakashi Client} - Registering Events"));
console.log(yellow("{Kakashi Client} - Registering Components"));
handleEvents(this.Events.path, client);
if(this.GuildCommands && this.GuildCommands.path) {
console.log(yellow("{Kakashi Client} - Registering Guild Commands"));
handleGuildCommands(this)
}
if(this.GlobalCommands && this.GlobalCommands.path) {
console.log(yellow("{Kakashi Client} - Registering Global Commands"));
handleGlobalCommands(this)
}
if(this.MessageCommands && this.MessageCommands.path) {
console.log(yellow("{Kakashi Client} - Registering Message Commands"));
handleMessageCommand(this)
}
if(this.Components && this.Components.path) {
console.log(yellow("{Kakashi Client} - Registering Components"));
handleComponents(this)
}
console.log(yellow("{Kakashi Client} - Handler is ready"));
}
Interaction(interaction) {
this.cooldowns = this.cooldowns || new Map(); // Initialize if not already set
return handleInteraction(interaction, this);
}
Message(message, prefix) {
this.cooldowns = this.cooldowns || new Map(); // Initialize if not already set
return handleMessage(message, prefix, this);
}
validateCooldownOptions(cooldown) {
if (!cooldown) {
throw new Error("cooldown object is required.");
}
const requiredFields = ['embed', 'button'];
requiredFields.forEach(field => {
if (!cooldown[field]) {
throw new Error(`cooldown.${field} is required.`);
}
});
return cooldown;
}
validateVoteOptions(vote) {
if (!vote) {
throw new Error("vote object is required.");
}
const requiredFields = [
'url',
'webhook'
];
requiredFields.forEach(field => {
if (!vote[field]) {
throw new Error(`vote.${field} is required.`);
}
});
const { webhook } = vote;
if (typeof webhook !== 'object') {
throw new Error(`vote.webhook should be an object.`);
}
const webhookFields = [
'token',
'auth',
'port',
'path'
];
webhookFields.forEach(field => {
if (!webhook[field]) {
throw new Error(`vote.webhook.${field} is required.`);
}
});
return vote;
}
validateGuildCommands(commands) {
if (!commands) {
return;
}
const requiredFields = [
'path',
'clientId',
'guildId',
'token'
];
requiredFields.forEach(field => {
if (!commands || !commands[field]) {
throw new Error(`GuildCommands.${field} is required.`);
}
});
return commands;
}
validateGlobalCommands(commands) {
if (!commands) {
return;
}
const requiredFields = [
'path',
'clientId',
'token'
];
requiredFields.forEach(field => {
if (!commands || !commands[field]) {
throw new Error(`GuildCommands.${field} is required.`);
}
});
return commands;
}
validateMessageCommands(command) {
if (!command) {
return;
}
const requiredFields = [
'path',
];
requiredFields.forEach(field => {
if (!command || !command[field]) {
throw new Error(`Message Commands.${field} is required.`);
}
});
return command;
}
validateEventsOptions(events) {
if (!events) {
return {
path: ""
};
}
const requiredFields = [
'path'
];
requiredFields.forEach(field => {
if (!events[field]) {
throw new Error(`Events.${field} is required.`);
}
});
return events;
}
validateComponentOptions(component) {
if (!component) {
return {
path: ""
};
}
const requiredFields = [
'path'
];
requiredFields.forEach(field => {
if (!component[field]) {
throw new Error(`Components.${field} is required.`);
}
});
return component;
}
validateSupportOptions(support) {
if (!support) {
throw new Error("support object is required.");
}
const requiredFields = [
'server',
];
requiredFields.forEach(field => {
if (!support[field]) {
throw new Error(`support.${field} is required.`);
}
});
return support;
}
static Create(client, options) {
const kakashi = new Kakashi(client, options);
return kakashi;
}
}
module.exports = {
Kakashi
};