diff --git a/src/Client.ts b/src/Client.ts index 7a775dd..1d7c9cb 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -7,12 +7,13 @@ import Account, { AccountInterface } from './models/Account.js'; import Moderation, { ModerationInterface } from './models/Moderation.js'; import emojis from './stores/emojis.js'; import Util from './Util.js'; +import Command from './class/Command' export default class Client extends Eris.Client { public util: Util; - public commands: Map; + public commands: Map; public aliases: Map; diff --git a/src/Util.ts b/src/Util.ts index 1f12fc8..8fff849 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -2,6 +2,7 @@ import { promisify } from 'util'; import childProcess from 'child_process'; import nodemailer from 'nodemailer'; import Client from './Client'; +import Command from './class/Command' export default class Util { public client: Client; @@ -27,4 +28,14 @@ export default class Util { // @ts-ignore this.client.guilds.get('446067825673633794').channels.get('595788220764127272').createMessage(`\`\`\`ts\n${error.stack}\`\`\``); } + + 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 + } + } + } } diff --git a/src/class/Command.ts b/src/class/Command.ts index 50dd7dc..6e8a9f4 100644 --- a/src/class/Command.ts +++ b/src/class/Command.ts @@ -13,17 +13,16 @@ export default class Command { aliases?: string[] client: Client - permissions?: { roles?: string[], users?: string[] } - - public run(message: Message, args: string[]) {} - + guildOnly?: boolean + public run (message: Message, args: string[]) {} constructor(client: Client) { - this.name = 'None'; - this.description = 'No description given'; - this.usage = 'No usage given'; - this.enabled = false; - this.aliases = []; - this.client = client; + this.name = 'None' + this.description = 'No description given' + 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