diff --git a/src/client/client.ts b/src/client/client.ts index 36b915b..33f2926 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -57,10 +57,11 @@ export class CoreClient extends Client { this._util.loadEvents(this, this._eventItems); } - public RegisterCommand(name: string, command: Command) { + public RegisterCommand(name: string, command: Command, serverId?: string) { const item: ICommandItem = { Name: name, Command: command, + ServerId: serverId, }; this._commandItems.push(item); diff --git a/src/client/events.ts b/src/client/events.ts index a8d5085..5c81e42 100644 --- a/src/client/events.ts +++ b/src/client/events.ts @@ -1,18 +1,8 @@ import { Message } from "discord.js"; -import { IBaseResponse } from "../contracts/IBaseResponse"; import ICommandItem from "../contracts/ICommandItem"; import SettingsHelper from "../helpers/SettingsHelper"; import { Util } from "./util"; -export interface IEventResponse extends IBaseResponse { - context?: { - prefix: string; - name: string; - args: string[]; - message: Message; - } -} - export class Events { private _util: Util; @@ -22,58 +12,21 @@ export class Events { // Emit when a message is sent // Used to check for commands - public async onMessage(message: Message, commands: ICommandItem[]): Promise { - if (!message.guild) return { - valid: false, - message: "Message was not sent in a guild, ignoring.", - }; - - if (message.author.bot) return { - valid: false, - message: "Message was sent by a bot, ignoring.", - }; + public async onMessage(message: Message, commands: ICommandItem[]) { + if (!message.guild) return; + if (message.author.bot) return; const prefix = await SettingsHelper.GetSetting("bot.prefix", message.guild.id); - if (!prefix) { - return { - valid: false, - message: "Prefix not found", - }; - } + if (!prefix) return; if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) { const args = message.content.substring(prefix.length).split(" "); const name = args.shift(); - if (!name) return { - valid: false, - message: "Command name was not found", - }; + if (!name) return; - const res = await this._util.loadCommand(name, args, message, commands); - - if (!res.valid) { - return { - valid: false, - message: res.message, - }; - } - - return { - valid: true, - context: { - prefix: prefix, - name: name, - args: args, - message: message, - }, - }; - } - - return { - valid: false, - message: "Message was not a command, ignoring.", + await this._util.loadCommand(name, args, message, commands); } } diff --git a/src/client/util.ts b/src/client/util.ts index 951a632..7c73776 100644 --- a/src/client/util.ts +++ b/src/client/util.ts @@ -1,9 +1,5 @@ // Required Components import { Client, Message } from "discord.js"; -import { readdirSync, existsSync } from "fs"; -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"; @@ -12,53 +8,37 @@ import StringTools from "../helpers/StringTools"; import { CommandResponse } from "../constants/CommandResponse"; import ErrorMessages from "../constants/ErrorMessages"; -export interface IUtilResponse extends IBaseResponse { - context?: { - name: string; - args: string[]; - message: Message; - } -} - // Util Class export class Util { - public async loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]): Promise { - if (!message.member) return { - valid: false, - message: "Member is not part of message", - }; - - if (!message.guild) return { - valid: false, - message: "Message is not part of a guild", - }; + public async loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]) { + if (!message.member) return; + if (!message.guild) return; const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", message.guild?.id); const disabledCommands = disabledCommandsString?.split(","); if (disabledCommands?.find(x => x == name)) { message.reply(process.env.COMMANDS_DISABLED_MESSAGE || "This command is disabled."); - - return { - valid: false, - message: "Command is disabled", - }; + return; } - const folder = process.env.FOLDERS_COMMANDS; + const item = commands.find(x => x.Name == name && !x.ServerId); + const itemForServer = commands.find(x => x.Name == name && x.ServerId == message.guild?.id); - const item = commands.find(x => x.Name == name); + let itemToUse: ICommandItem; - if (!item) { - message.reply('Command not found'); + if (!itemForServer) { + if (!item) { + message.reply('Command not found'); + return; + } - return { - valid: false, - message: "Command not found" - }; + itemToUse = item; + } else { + itemToUse = itemForServer; } - const requiredRoles = item.Command._roles; + const requiredRoles = itemToUse.Command._roles; for (const i in requiredRoles) { if (message.guild) { @@ -66,20 +46,12 @@ export class Util { if (!setting) { message.reply("Unable to verify if you have this role, please contact your bot administrator"); - - return { - valid: false, - message: "Unable to verify if you have this role, please contact your bot administrator" - }; + return; } if (!message.member.roles.cache.find(role => role.name == setting)) { message.reply(`You require the \`${StringTools.Capitalise(setting)}\` role to run this command`); - - return { - valid: false, - message: `You require the \`${StringTools.Capitalise(setting)}\` role to run this command` - }; + return; } } } @@ -90,39 +62,24 @@ export class Util { message: message }; - const precheckResponse = item.Command.precheck(context); - const precheckAsyncResponse = await item.Command.precheckAsync(context); + const precheckResponse = itemToUse.Command.precheck(context); + const precheckAsyncResponse = await itemToUse.Command.precheckAsync(context); if (precheckResponse != CommandResponse.Ok) { message.reply(ErrorMessages.GetErrorMessage(precheckResponse)); - - return { - valid: false, - message: ErrorMessages.GetErrorMessage(precheckResponse) - }; + return; } if (precheckAsyncResponse != CommandResponse.Ok) { message.reply(ErrorMessages.GetErrorMessage(precheckAsyncResponse)); - - return { - valid: false, - message: ErrorMessages.GetErrorMessage(precheckAsyncResponse) - }; + return; } - item.Command.execute(context); - - return { - valid: true, - context: context - } + itemToUse.Command.execute(context); } // Load the events - loadEvents(client: Client, events: IEventItem[]): IUtilResponse { - const folder = process.env.FOLDERS_EVENTS; - + loadEvents(client: Client, events: IEventItem[]) { events.forEach((e) => { client.on('channelCreate', e.Event.channelCreate); client.on('channelDelete', e.Event.channelDelete); @@ -138,9 +95,5 @@ export class Util { client.on('messageUpdate', e.Event.messageUpdate); client.on('ready', e.Event.ready); }); - - return { - valid: true - } } } diff --git a/src/contracts/ICommandItem.ts b/src/contracts/ICommandItem.ts index c8246c9..89acb50 100644 --- a/src/contracts/ICommandItem.ts +++ b/src/contracts/ICommandItem.ts @@ -3,4 +3,5 @@ import { Command } from "../type/command"; export default interface ICommandItem { Name: string, Command: Command, + ServerId?: string, } \ No newline at end of file diff --git a/src/registry.ts b/src/registry.ts index 0792dc3..d80fa6c 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -35,7 +35,7 @@ export default class Registry { client.RegisterCommand("setup", new Setup()); client.RegisterCommand("config", new Config()); client.RegisterCommand("code", new Code()); - client.RegisterCommand("disable", new Disable()) + client.RegisterCommand("disable", new Disable()); } public static RegisterEvents(client: CoreClient) {