-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdacbot.js
89 lines (73 loc) · 2.5 KB
/
dacbot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const config = require("./config.json");
const fs = require('fs');
const Discord = require("discord.js");
const BotApi = require("./classes/botapi.js");
const BotWSListener = require("./classes/botwslistener");
const eoswrapper = require('./classes/eoswrapper.js');
const mongowrapper = require('./classes/mongowrapper.js');
class DacBot{
constructor(config){
this.config = config;
this.client = new Discord.Client();
this.embed = require("discord.js").RichEmbed;
this.eos = new eoswrapper();
this.init();
}
async init(){
await this.connectdb();
this.loadEvents();
this.loadCommands();
this.loadTasks();
await this.client.login(this.config.bot.token);
if(this.config.bot.api.enable){
this.botapi = new BotApi(this);
}
if(this.config.bot.ws.enable){
this.botws = new BotWSListener(this);
}
}
loadCommands(){
this.commands = [];
let files = fs.readdirSync(this.config.bot.commands);
files = files.filter(f => /\.js$/.test(f) );
files.forEach(f => {
const cmd_obj = new (require(`${this.config.bot.commands}/${f}`) )(this);
if(!cmd_obj.disable){
this.commands.push(cmd_obj);
}
});
}
loadEvents(){
let files = fs.readdirSync(this.config.bot.events);
files = files.filter(f => /\.js$/.test(f) );
files.forEach(f => {
console.log(`./events/${f}`)
const event = require(`${this.config.bot.events}/${f}`);
let eventName = f.split(".")[0];
this.client.on(eventName, event.bind(null, this) );
});
}
loadTasks(){
this.tasks = [];
let files = fs.readdirSync(this.config.bot.tasks);
files = files.filter(f => /\.js$/.test(f) );
files.forEach(f => {
let taskname = f.split(".")[0];
const task = new (require(`${this.config.bot.tasks}/${f}`) )(this, taskname);
this.tasks.push(task);
});
}
getCommand(cmd_name){
return this.commands.find(cmd => cmd.name == cmd_name);
}
getTask(task_name){
return this.tasks.find(tsk => tsk.name == task_name);
}
async connectdb(){
if(!this.mongo){
this.mongo = new mongowrapper(this.config.mongo.url, this.config.mongo.dbname);
await this.mongo.connect();
}
}
}
let test = new DacBot(config);