fishing-bot-mod/src/Bot.js

83 lines
2.0 KiB
JavaScript
Raw Normal View History

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) {
2021-05-11 04:57:53 +02:00
let role;
msg.member.guild.roles.cache.forEach(r => {
if (r.name.toString() == msg.member.user.id.toString()) {
role = r;
}
});
msg.p = {
name: msg.author.username,
_id: msg.author.id,
color: role.color
}
msg.a = msg.content;
if(msg.a[0] == "" && msg.p.id !== client.participantId) {
msg.a[0] = "/";
}
2021-05-10 21:10:34 +02:00
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
}
}