From a211bc3dbd13af73fd312f4f5118e9db5ef58d11 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 15 Oct 2019 00:04:07 +0100 Subject: [PATCH] Added command handler --- src/Client.ts | 5 ++--- src/Util.ts | 12 ++++++++++++ src/class/Command.ts | 2 ++ src/events/messageCreate.ts | 30 ++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/events/messageCreate.ts diff --git a/src/Client.ts b/src/Client.ts index e8bb57f..9caf5a7 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -3,6 +3,7 @@ import mongoose from 'mongoose'; import fs from 'fs-extra'; import path from 'path'; import config from './config.json'; +import Command from './class/Command' const options: any = { getAllUsers: true, @@ -11,13 +12,11 @@ const options: any = { } export default class Client extends Eris.Client { - public commands: Map; - public aliases: Map; + public commands: Map; constructor() { super(config.token, options); this.commands = new Map(); - this.aliases = new Map(); } public loadCommand(commandPath: string) { diff --git a/src/Util.ts b/src/Util.ts index 689daf0..d670f2a 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -1,6 +1,8 @@ import { promisify } from 'util'; import childProcess from 'child_process'; import nodemailer from 'nodemailer'; +import Command from './class/Command' +import Client from './Client' export default class Util { constructor() {} @@ -17,4 +19,14 @@ export default class Util { } return result; } + + public resolveCommand(client: Client, command: string): Command { + if (client.commands.has(command)) return client.commands.get(command) + for (const cmd of client.commands.values()) { + if (!cmd.aliases) continue + for (const alias of cmd.aliases) { + if (command === alias.toLowerCase()) return cmd + } + } + } } \ No newline at end of file diff --git a/src/class/Command.ts b/src/class/Command.ts index 944c3ca..7f1f391 100644 --- a/src/class/Command.ts +++ b/src/class/Command.ts @@ -9,6 +9,7 @@ export default class Command { aliases?: string[] client: Client permissions?: { roles: string[], users: string[] } + guildOnly?: boolean public run (message: Message, args: string[]) {} constructor(client: Client) { this.name = 'None' @@ -16,6 +17,7 @@ export default class Command { this.usage = 'No usage given' this.enabled = false this.aliases = [] + this.guildOnly = true this.client = client } } diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts new file mode 100644 index 0000000..0460f4e --- /dev/null +++ b/src/events/messageCreate.ts @@ -0,0 +1,30 @@ +import Client from '../Client' +import { prefix } from '../config.json' +import { Message, TextChannel } from 'eris' +import Util from '../Util' +import Command from '../class/Command' + +export default class { + client: Client + constructor (client: Client) { + this.client = client + } + + async run(message: Message) { + const noPrefix: string[] = message.content.slice(prefix.length).trim().split(/ +/g) + const command: string = noPrefix[0].toLowerCase() + const resolved: Command = new Util().resolveCommand(this.client, command) + if (!resolved) return + if (resolved.guildOnly && !(message.channel instanceof TextChannel)) return + const hasUserPerms: boolean = resolved.permissions.users.includes(message.author.id) + let hasRolePerms: boolean = false + for (const role of resolved.permissions.roles) { + if (message.member && message.member.roles.includes(role)) { + hasRolePerms = true; break + } + } + if (!hasRolePerms && !hasUserPerms) return + const args: string[] = noPrefix.slice(1) + resolved.run(message, args) + } +} \ No newline at end of file