Added command handler

merge-requests/1/merge
Bsian 2019-10-15 00:04:07 +01:00
parent 1dd2052b0c
commit a211bc3dbd
No known key found for this signature in database
GPG Key ID: 097FB9A291026091
4 changed files with 46 additions and 3 deletions

View File

@ -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<string, any>;
public aliases: Map<string, string>;
public commands: Map<string, Command>;
constructor() {
super(config.token, options);
this.commands = new Map();
this.aliases = new Map();
}
public loadCommand(commandPath: string) {

View File

@ -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
}
}
}
}

View File

@ -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
}
}

View File

@ -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)
}
}