-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
218 lines (195 loc) · 6.12 KB
/
bot.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Import and setup files and modules
const eventHandlers = {
ready: require("./Events/ready.js"),
guildCreate: require("./Events/guildCreate.js"),
guildUpdate: require("./Events/guildUpdate.js"),
guildDelete: require("./Events/guildDelete.js"),
channelDelete: require("./Events/channelDelete.js"),
guildRoleDelete: require("./Events/guildRoleDelete.js"),
guildMemberAdd: require("./Events/guildMemberAdd.js"),
guildMemberUpdate: require("./Events/guildMemberUpdate.js"),
guildMemberRemove: require("./Events/guildMemberRemove.js"),
guildBanAdd: require("./Events/guildBanAdd.js"),
guildBanRemove: require("./Events/guildBanRemove.js"),
messageCreate: require("./Events/messageCreate.js"),
messageUpdate: require("./Events/messageUpdate.js"),
messageDelete: require("./Events/messageDelete.js"),
presenceUpdate: require("./Events/presenceUpdate.js"),
userUpdate: require("./Events/userUpdate.js"),
voiceChannelJoin: require("./Events/voiceChannelJoin.js"),
voiceStateUpdate: require("./Events/voiceStateUpdate.js"),
voiceChannelLeave: require("./Events/voiceChannelLeave.js")
};
const database = require("./Database/Driver.js");
const auth = require("./Configuration/auth.json");
const config = require("./Configuration/config.json");
const winston = require("winston");
// Set up default winston logger
winston.add(winston.transports.File, {
filename: "bot-out.log"
});
// Connect to and initialize database
database.initialize(config.db_url, err => {
if(err) {
winston.error("Failed to connect to database", err);
} else {
const db = database.get();
// Get bot client from platform and login
const bot = require("./Platform/Platform.js")(db, auth, config);
bot.connect().then(() => {
winston.info("Started bot application");
}, (err) => {
winston.error("Failed to connect to discord", err)
});
/* After guilds and users have been created (first-time only)
* Will also trigger the shard message information
*/
bot.once("ready", () => {
eventHandlers.ready(bot, db, config, winston);
});
// Server joined by bot
bot.on("guildCreate", svr => {
if(bot.isReady) {
eventHandlers.guildCreate(bot, db, config, winston, svr).catch(err => {
winston.error(err);
});
}
});
// Server details updated (name, icon, etc.)
bot.on("guildUpdate", (svr, oldsvrdata) => {
if(bot.isReady) {
eventHandlers.guildUpdate(bot, db, config, winston, svr, oldsvrdata).catch(err => {
winston.error(err);
});
}
});
// Server left by bot or deleted
bot.on("guildDelete", (svr, unavailable) => {
if(bot.isReady && !unavailable) {
eventHandlers.guildDelete(bot, db, config, winston, svr).catch(err => {
winston.error(err);
});
}
});
// Server channel deleted
bot.on("channelDelete", ch => {
if(bot.isReady) {
eventHandlers.channelDelete(bot, db, config, winston, ch).catch(err => {
winston.error(err);
});
}
});
// Server role deleted
bot.on("guildRoleDelete", (svr, role) => {
if(bot.isReady) {
eventHandlers.guildRoleDelete(bot, db, config, winston, svr, role).catch(err => {
winston.error(err);
});
}
});
// User joined server
bot.on("guildMemberAdd", (svr, member) => {
if(bot.isReady) {
eventHandlers.guildMemberAdd(bot, db, config, winston, svr, member).catch(err => {
winston.error(err);
});
}
});
// User details updated on server (role, nickname, etc.)
bot.on("guildMemberUpdate", (svr, member, oldmemberdata) => {
if(bot.isReady) {
eventHandlers.guildMemberUpdate(bot, db, config, winston, svr, member, oldmemberdata).catch(err => {
winston.error(err);
});
}
});
// User left or kicked from server
bot.on("guildMemberRemove", (svr, member) => {
if(bot.isReady) {
eventHandlers.guildMemberRemove(bot, db, config, winston, svr, member).catch(err => {
winston.error(err);
});
}
});
// User banned from server
bot.on("guildBanAdd", (svr, usr) => {
if(bot.isReady) {
eventHandlers.guildBanAdd(bot, db, config, winston, svr, usr).catch(err => {
winston.error(err);
});
}
});
// User unbanned from server
bot.on("guildBanRemove", (svr, usr) => {
if(bot.isReady) {
eventHandlers.guildBanRemove(bot, db, config, winston, svr, usr).catch(err => {
winston.error(err);
});
}
});
// Message sent on server
bot.on("messageCreate", msg => {
if(bot.isReady) {
eventHandlers.messageCreate(bot, db, config, winston, msg).catch(err => {
winston.error(err);
});
}
});
// Message edited
bot.on("messageUpdate", (newMsg, oldMsg) => {
if(bot.isReady) {
eventHandlers.messageUpdate(bot, db, winston, newMsg, oldMsg).catch(err => {
winston.error(err);
});
}
});
// Message deleted
bot.on("messageDelete", msg => {
if(bot.isReady) {
eventHandlers.messageDelete(bot, db, config, winston, msg).catch(err => {
winston.error(err);
});
}
});
// User status changed (afk, new game, etc.)
bot.on("presenceUpdate", (member, oldpresence) => {
if(bot.isReady) {
eventHandlers.presenceUpdate(bot, db, config, winston, member, oldpresence).catch(err => {
winston.error(err);
});
}
});
// User updated (name, avatar, etc.)
bot.on("userUpdate", (usr, oldusrdata) => {
if(bot.isReady) {
eventHandlers.userUpdate(bot, db, config, winston, usr, oldusrdata).catch(err => {
winston.error(err);
});
}
});
// User joined server voice channel
bot.on("voiceChannelJoin", (member, ch) => {
if(bot.isReady) {
eventHandlers.voiceChannelJoin(bot, db, config, winston, member, ch).catch(err => {
winston.error(err);
});
}
});
// User voice connection details updated on server (muted, deafened, etc.)
bot.on("voiceStateUpdate", (member, oldvoice) => {
if(bot.isReady) {
eventHandlers.voiceStateUpdate(bot, db, config, winston, member, oldvoice).catch(err => {
winston.error(err);
});
}
});
// User left server voice channel
bot.on("voiceChannelLeave", (member, ch) => {
if(bot.isReady) {
eventHandlers.voiceChannelLeave(bot, db, config, winston, member, ch).catch(err => {
winston.error(err);
});
}
});
}
});