Repository navigation
Expand file tree
/
Copy pathBot.js
More file actions
132 lines (113 loc) · 2.84 KB
/
Copy pathBot.js
File metadata and controls
132 lines (113 loc) · 2.84 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//---------------------------------------------------------------------
// Bot
// Contains functions for controlling the bot.
//---------------------------------------------------------------------
const Bot = {};
Bot.DBM = null;
const DiscordJS = require('discord.js');
Bot.$cmds = {};
Bot.$evts = {};
Bot.bot = null;
Bot.init = function() {
this.initBot();
this.reformatData();
this.initEvents();
this.login();
};
Bot.initBot = function() {
this.bot = new DiscordJS.Client();
};
Bot.reformatData = function() {
this.reformatCommands();
this.reformatEvents();
};
Bot.reformatCommands = function() {
const data = this.DBM.Files.data.commands;
if(!data) return;
for(let i = 0; i < data.length; i++) {
const com = data[i];
if(com) {
if(this.DBM.Files.data.settings.case === 'false') {
this.$cmds[com.name.toLowerCase()] = com;
} else {
this.$cmds[com.name] = com;
}
}
}
};
Bot.reformatEvents = function() {
const data = this.DBM.Files.data.events;
if(!data) return;
for(let i = 0; i < data.length; i++) {
const com = data[i];
if(com) {
const type = com['event-type'];
if(!this.$evts[type]) this.$evts[type] = [];
this.$evts[type].push(com);
}
}
};
Bot.initEvents = function() {
this.bot.on('ready', this.onReady.bind(this));
this.bot.on('message', this.onMessage.bind(this));
this.DBM.Events.registerEvents(this.bot);
};
Bot.login = function() {
this.bot.login(this.DBM.Files.data.settings.token);
};
Bot.onReady = function() {
if(process.send) process.send('BotReady');
console.log('Bot is ready!');
this.preformInitialization();
};
Bot.preformInitialization = function() {
const bot = this.bot;
if(this.$evts["1"]) {
this.DBM.Events.onInitialization(bot);
}
if(this.$evts["3"]) {
this.DBM.Events.setupIntervals(bot);
}
};
Bot.onMessage = function(msg) {
if(!msg.author.bot) {
try {
if(!this.checkCommand(msg)) {
this.onAnyMessage(msg);
}
} catch(e) {
console.error(e);
}
}
};
Bot.checkCommand = function(msg) {
let command = this.checkTag(msg.content);
if(command) {
if(this.DBM.Files.data.settings.case === 'false') {
command = command.toLowerCase();
}
const cmd = this.$cmds[command];
if(cmd) {
this.DBM.Actions.preformActions(msg, cmd);
return true;
}
}
return false;
};
Bot.checkTag = function(content) {
const tag = this.DBM.Files.data.settings.tag;
const separator = this.DBM.Files.data.settings.separator || '\\s+';
content = content.split(new RegExp(separator))[0];
if(content.startsWith(tag)) {
return content.substring(tag.length);
}
return null;
};
Bot.onAnyMessage = function(msg) {
if(!msg.author.bot) {
if(this.$evts["2"]) {
this.DBM.Events.callEvents("2", 1, 0, 2, false, '', msg);
}
}
};
module.exports = Bot;