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 fs from 'fs-extra';
import path from 'path'; import path from 'path';
import config from './config.json'; import config from './config.json';
import Command from './class/Command'
const options: any = { const options: any = {
getAllUsers: true, getAllUsers: true,
@ -11,13 +12,11 @@ const options: any = {
} }
export default class Client extends Eris.Client { export default class Client extends Eris.Client {
public commands: Map<string, any>; public commands: Map<string, Command>;
public aliases: Map<string, string>;
constructor() { constructor() {
super(config.token, options); super(config.token, options);
this.commands = new Map(); this.commands = new Map();
this.aliases = new Map();
} }
public loadCommand(commandPath: string) { public loadCommand(commandPath: string) {

View File

@ -1,6 +1,8 @@
import { promisify } from 'util'; import { promisify } from 'util';
import childProcess from 'child_process'; import childProcess from 'child_process';
import nodemailer from 'nodemailer'; import nodemailer from 'nodemailer';
import Command from './class/Command'
import Client from './Client'
export default class Util { export default class Util {
constructor() {} constructor() {}
@ -17,4 +19,14 @@ export default class Util {
} }
return result; 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[] aliases?: string[]
client: Client client: Client
permissions?: { roles: string[], users: string[] } permissions?: { roles: string[], users: string[] }
guildOnly?: boolean
public run (message: Message, args: string[]) {} public run (message: Message, args: string[]) {}
constructor(client: Client) { constructor(client: Client) {
this.name = 'None' this.name = 'None'
@ -16,6 +17,7 @@ export default class Command {
this.usage = 'No usage given' this.usage = 'No usage given'
this.enabled = false this.enabled = false
this.aliases = [] this.aliases = []
this.guildOnly = true
this.client = client 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)
}
}