From 3b9231644a66001daf21a7f22775d8282308d893 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 21 Dec 2021 18:37:11 +0000 Subject: [PATCH] Update commands to new system --- src/Register.ts | 37 ++++++++++ src/client/client.ts | 29 +++++++- src/client/events.ts | 5 +- src/client/util.ts | 134 +++++++++++++--------------------- src/commands/ban.ts | 2 +- src/contracts/ICommandItem.ts | 6 ++ src/contracts/IEventItem.ts | 6 ++ src/vylbot.ts | 5 ++ 8 files changed, 137 insertions(+), 87 deletions(-) create mode 100644 src/Register.ts create mode 100644 src/contracts/ICommandItem.ts create mode 100644 src/contracts/IEventItem.ts diff --git a/src/Register.ts b/src/Register.ts new file mode 100644 index 0000000..d8390bf --- /dev/null +++ b/src/Register.ts @@ -0,0 +1,37 @@ +import { CoreClient } from "./client/client"; +import About from "./commands/about"; +import Ban from "./commands/ban"; +import Clear from "./commands/clear"; +import Evaluate from "./commands/eval"; +import Help from "./commands/help"; +import Kick from "./commands/kick"; +import Mute from "./commands/mute"; +import Poll from "./commands/poll"; +import Role from "./commands/role"; +import Rules from "./commands/rules"; +import Unmute from "./commands/unmute"; +import Warn from "./commands/warn"; +import MemberEvents from "./events/MemberEvents"; +import MessageEvents from "./events/MessageEvents"; + +export default class Register { + public static RegisterCommands(client: CoreClient) { + client.RegisterCommand("about", new About()); + client.RegisterCommand("ban", new Ban()); + client.RegisterCommand("clear", new Clear()); + client.RegisterCommand("eval", new Evaluate()); + client.RegisterCommand("help", new Help()); + client.RegisterCommand("kick", new Kick()); + client.RegisterCommand("mute", new Mute()); + client.RegisterCommand("poll", new Poll()); + client.RegisterCommand("role", new Role()); + client.RegisterCommand("rules", new Rules()); + client.RegisterCommand("unmute", new Unmute()); + client.RegisterCommand("warn", new Warn()); + } + + public static RegisterEvents(client: CoreClient) { + client.RegisterEvent(new MemberEvents()); + client.RegisterEvent(new MessageEvents()); + } +} \ No newline at end of file diff --git a/src/client/client.ts b/src/client/client.ts index 9337496..25e9467 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -1,10 +1,17 @@ import { Client } from "discord.js"; import * as dotenv from "dotenv"; +import ICommandItem from "../contracts/ICommandItem"; +import IEventItem from "../contracts/IEventItem"; +import { Command } from "../type/command"; +import { Event } from "../type/event"; import { Events } from "./events"; import { Util } from "./util"; export class CoreClient extends Client { + private _commandItems: ICommandItem[]; + private _eventItems: IEventItem[]; + private _events: Events; private _util: Util; @@ -12,6 +19,9 @@ export class CoreClient extends Client { super(); dotenv.config(); + this._commandItems = []; + this._eventItems = []; + this._events = new Events(); this._util = new Util(); } @@ -22,11 +32,26 @@ export class CoreClient extends Client { if (!process.env.FOLDERS_COMMANDS) throw "FOLDERS_COMMANDS is not defined in .env"; if (!process.env.FOLDERS_EVENTS) throw "FOLDERS_EVENTS is not defined in .env"; - super.on("message", this._events.onMessage); + super.on("message", (message) => this._events.onMessage(message, this._commandItems)); super.on("ready", this._events.onReady); super.login(process.env.BOT_TOKEN); - this._util.loadEvents(this); + this._util.loadEvents(this, this._eventItems); + } + + public RegisterCommand(name: string, command: Command) { + const item: ICommandItem = { + Name: name, + Command: command, + }; + + this._commandItems.push(item); + } + + public RegisterEvent(event: Event) { + const item: IEventItem = { + Event: event, + }; } } diff --git a/src/client/events.ts b/src/client/events.ts index 417c24d..ca0d058 100644 --- a/src/client/events.ts +++ b/src/client/events.ts @@ -1,5 +1,6 @@ import { Message } from "discord.js"; import { IBaseResponse } from "../contracts/IBaseResponse"; +import ICommandItem from "../contracts/ICommandItem"; import { Util } from "./util"; export interface IEventResponse extends IBaseResponse { @@ -20,7 +21,7 @@ export class Events { // Emit when a message is sent // Used to check for commands - public onMessage(message: Message): IEventResponse { + public onMessage(message: Message, commands: ICommandItem[]): IEventResponse { if (!message.guild) return { valid: false, message: "Message was not sent in a guild, ignoring.", @@ -42,7 +43,7 @@ export class Events { message: "Command name was not found", }; - const res = this._util.loadCommand(name, args, message); + const res = this._util.loadCommand(name, args, message, commands); if (!res.valid) { return { diff --git a/src/client/util.ts b/src/client/util.ts index e45f1c0..61ec0f9 100644 --- a/src/client/util.ts +++ b/src/client/util.ts @@ -5,6 +5,8 @@ import { IBaseResponse } from "../contracts/IBaseResponse"; import { Command } from "../type/command"; import { Event } from "../type/event"; import { ICommandContext } from "../contracts/ICommandContext"; +import ICommandItem from "../contracts/ICommandItem"; +import IEventItem from "../contracts/IEventItem"; export interface IUtilResponse extends IBaseResponse { context?: { @@ -16,8 +18,7 @@ export interface IUtilResponse extends IBaseResponse { // Util Class export class Util { - - public loadCommand(name: string, args: string[], message: Message): IUtilResponse { + public loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]): IUtilResponse { if (!message.member) return { valid: false, message: "Member is not part of message", @@ -36,97 +37,66 @@ export class Util { const folder = process.env.FOLDERS_COMMANDS; - if (existsSync(`${process.cwd()}/${folder}/`)) { - if (existsSync(`${process.cwd()}/${folder}/${name}.ts`)) { - const commandFile = require(`${process.cwd()}/${folder}/${name}.ts`).default; - const command = new commandFile() as Command; + const item = commands.find(x => x.Name == name); - const requiredRoles = command._roles; + if (!item) { + message.reply('Command not found'); - if (!command._category) command._category = "none"; - - for (const i in requiredRoles) { - if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) { - message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`); - - return { - valid: false, - message: `You require the \`${requiredRoles[i]}\` role to run this command`, - }; - } - } - - const context: ICommandContext = { - name: name, - args: args, - message: message, - } - - // Run the command and pass the command context with it - command.execute(context); - - return { - valid: true, - context: { - name: name, - args: args, - message: message, - } - } - } else { - return { - valid: false, - message: "File does not exist", - } - } - } else { return { valid: false, - message: "Command folder does not exist", + message: "Command not found" + }; + } + + const requiredRoles = item.Command._roles; + + for (const i in requiredRoles) { + if (!message.member.roles.cache.find(role => role.name == requiredRoles[i])) { + message.reply(`You require the \`${requiredRoles[i]}\` role to run this command`); + + return { + valid: false, + message: `You require the \`${requiredRoles[i]}\` role to run this command` + }; } } + + const context: ICommandContext = { + name: name, + args: args, + message: message + }; + + item.Command.execute(context); + + return { + valid: true, + context: context + } } // Load the events - loadEvents(client: Client): IUtilResponse { + loadEvents(client: Client, events: IEventItem[]): IUtilResponse { const folder = process.env.FOLDERS_EVENTS; - - if (existsSync(`${process.cwd()}/${folder}/`)) { - const eventFiles = readdirSync(`${process.cwd()}/${folder}/`); - - for (let i = 0; i < eventFiles.length; i++) { - if (eventFiles[i].includes('.ts')) { - const eventName = eventFiles[i].split('.')[0]; - - const file = require(`${process.cwd()}/${folder}/${eventName}.ts`); - - const event = new file[eventName]() as Event; - - // Load events - client.on('channelCreate', event.channelCreate); - client.on('channelDelete', event.channelDelete); - client.on('channelUpdate', event.channelUpdate); - client.on('guildBanAdd', event.guildBanAdd); - client.on('guildBanRemove', event.guildBanRemove); - client.on('guildCreate', event.guildCreate); - client.on('guildMemberAdd', event.guildMemberAdd); - client.on('guildMemberRemove', event.guildMemberRemove); - client.on('guildMemberUpdate', event.guildMemberUpdate); - client.on('message', event.message); - client.on('messageDelete', event.messageDelete); - client.on('messageUpdate', event.messageUpdate); - client.on('ready', event.ready); - } - } - return { - valid: true, - } - } else { - return { - valid: false, - message: "Event folder does not exist", - } + events.forEach((e) => { + client.on('channelCreate', e.Event.channelCreate); + client.on('channelDelete', e.Event.channelDelete); + client.on('channelUpdate', e.Event.channelUpdate); + client.on('guildBanAdd', e.Event.guildBanAdd); + client.on('guildBanRemove', e.Event.guildBanRemove); + client.on('guildCreate', e.Event.guildCreate); + client.on('guildMemberAdd', e.Event.guildMemberAdd); + client.on('guildMemberRemove', e.Event.guildMemberRemove); + client.on('guildMemberUpdate', e.Event.guildMemberUpdate); + client.on('message', e.Event.message); + client.on('messageDelete', e.Event.messageDelete); + client.on('messageUpdate', e.Event.messageUpdate); + client.on('ready', e.Event.ready); + }); + + return { + valid: true } } } diff --git a/src/commands/ban.ts b/src/commands/ban.ts index eb8c830..30a17fe 100644 --- a/src/commands/ban.ts +++ b/src/commands/ban.ts @@ -5,7 +5,7 @@ import PublicEmbed from "../helpers/embeds/PublicEmbed"; import { Command } from "../type/command"; import { ICommandContext } from "../contracts/ICommandContext"; -export default class Bane extends Command { +export default class Ban extends Command { constructor() { super(); diff --git a/src/contracts/ICommandItem.ts b/src/contracts/ICommandItem.ts new file mode 100644 index 0000000..c8246c9 --- /dev/null +++ b/src/contracts/ICommandItem.ts @@ -0,0 +1,6 @@ +import { Command } from "../type/command"; + +export default interface ICommandItem { + Name: string, + Command: Command, +} \ No newline at end of file diff --git a/src/contracts/IEventItem.ts b/src/contracts/IEventItem.ts new file mode 100644 index 0000000..51bb881 --- /dev/null +++ b/src/contracts/IEventItem.ts @@ -0,0 +1,6 @@ + +import { Event } from "../type/event"; + +export default interface IEventItem { + Event: Event, +} \ No newline at end of file diff --git a/src/vylbot.ts b/src/vylbot.ts index 4b418b8..56cbdc4 100644 --- a/src/vylbot.ts +++ b/src/vylbot.ts @@ -1,5 +1,6 @@ import { CoreClient } from "./client/client"; import * as dotenv from "dotenv"; +import Register from "./Register"; dotenv.config(); @@ -22,4 +23,8 @@ requiredConfigs.forEach(config => { }); const client = new CoreClient(); + +Register.RegisterCommands(client); +Register.RegisterEvents(client); + client.start(); \ No newline at end of file