2021-05-10 21:10:34 +02:00
|
|
|
const DiscordClient = require('./DiscordClient');
|
|
|
|
const StaticEventEmitter = require('./StaticEventEmitter');
|
|
|
|
|
|
|
|
module.exports = class Bot extends StaticEventEmitter {
|
|
|
|
static start(token) {
|
|
|
|
DiscordClient.start(token);
|
|
|
|
this.bindEventListeners();
|
|
|
|
this.commands = new Map();
|
|
|
|
|
|
|
|
this.admin = [];
|
2021-05-11 04:08:48 +02:00
|
|
|
this.prefix = "/"; // never change this
|
2021-05-10 21:10:34 +02:00
|
|
|
|
|
|
|
this.loadCommands();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bindEventListeners() {
|
|
|
|
this.on("chat.receive", msg => {
|
|
|
|
console.log(msg.author.username + ": " + msg.content);
|
|
|
|
|
|
|
|
let m = {
|
|
|
|
referer: msg.author,
|
|
|
|
|
|
|
|
};
|
2021-05-11 04:08:48 +02:00
|
|
|
|
|
|
|
this.runCommand(msg);
|
2021-05-10 21:10:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.on("chat.send", msg => {
|
|
|
|
DiscordClient.sendChat(msg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static runCommand(msg) {
|
|
|
|
this.commands.forEach(cmd => {
|
|
|
|
let usedCommand = false;
|
|
|
|
cmd.cmd.forEach(c => {
|
|
|
|
if (msg.cmd == c) {
|
|
|
|
usedCommand = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!usedCommand) return;
|
|
|
|
|
|
|
|
if (msg.rank == 'admin') {
|
|
|
|
cmd.func(msg, true);
|
|
|
|
} else {
|
|
|
|
cmd.func(msg, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static fixUsage(str) {
|
|
|
|
return str.split("&PREFIX").join(this.prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getRank(id) {
|
|
|
|
if (this.admin.indexOf(id) !== -1) {
|
|
|
|
return "admin";
|
|
|
|
} else {
|
|
|
|
return "none";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:08:48 +02:00
|
|
|
static async loadCommands() {
|
|
|
|
(require('./Commands'))(this);
|
2021-05-10 21:10:34 +02:00
|
|
|
}
|
|
|
|
}
|