-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
346 lines (311 loc) · 10.8 KB
/
index.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
Thanks to NoxCaos#4905 for letting me "borrow" some functions from the other project.
send, embedBuilder, direct, utils/cmd and utils/colors are all "borrowed" items, just slightly edited
*/
const Discord = require('eris')
const mongoose = require('mongoose')
const colors = require('./utils/colors')
const paginator = require('discord-paginator')
const {trigger} = require('./utils/cmd')
const config = require('./config.json')
const commands = require('./commands')
const {fetchOrCreateUser} = require("./modules/users");
const {fetchOrCreateGuild} = require('./modules/guilds')
const {
checkReminders,
deleteReminders
} = require('./modules/reminders')
const {
Channel,
Guild,
Member,
Message,
Reaction,
Role,
Voice,
} = require('./events')
module.exports.schemas = require('./tables')
const bot = new Discord(config.dev? config["token-dev"]: config["token-main"],
{
maxShards: config.shards, getAllUsers: true, autoreconnect:true,
intents: [
"directMessages",
"guildBans",
"guildEmojisAndStickers",
"guildInvites",
"guildMembers",
"guildMessages",
"guildVoiceStates",
"guildWebhooks",
"guilds",
"guildMessageReactions",
"guildPresences",
"guildIntegrations",
"directMessageReactions"]
})
const pgn = paginator.create({bot, pgnButtons: ['first', 'last', 'back', 'forward']})
const mongooseConnection = mongoose.connect(config.databaseURL, {useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true})
const send = (channel, message, content) => {
if ((typeof message === "string"))
message = embedBuilder(message, 'green')
if(message.description)
message.description = message.description.replace(/\s\s+/gm, '\n')
if(!message.color)
message.color = colors['green']
if (content)
return bot.createMessage(channel, {content: content, embed: message})
return bot.createMessage(channel, {embed: message})
}
const embedBuilder = (message, color) => {
if(typeof message === 'object') {
message.color = colors[color]
return message
}
return { description: `${message}`, color: colors[color] }
}
const direct = async (user, string, color = 'green') => {
const ch = await bot.getDMChannel(user.id)
return send(ch.id, embedBuilder(string, color))
}
const icons = {
0: '0️⃣',
1: '1️⃣',
2: '2️⃣',
3: '3️⃣',
4: '4️⃣',
5: '5️⃣',
6: '6️⃣',
7: '7️⃣',
8: '8️⃣',
9: '9️⃣',
10: '🔟',
true: `✅`,
false: `❌`,
}
const context = {
mongooseConnection,
bot,
pgn,
config,
icons,
send,
direct,
}
// Preparation for the future where I actually use buttons!
// bot.on('rawWS', async (event) => {
// if (event.t === 'INTERACTION_CREATE') {
// // console.log(event)
// let url = `/interactions/${event.d.id}/${event.d.token}/callback`
// bot.emit('INTERACTION_CREATE', event.d)
// let body = {
// "type": 4,
// "data": {
// "content": `<@${event.d.member.user.id}> YOU CLICKED THE TEST BUTTON!`
// }
// }
// return bot.requestHandler.request("POST", url, true, body)
// }
// })
//
// bot.on('INTERACTION_CREATE', async (data) => {
// console.log(data)
// })
/*
0 = Playing
2 = Listening To
3 = Watching
*/
bot.on('ready', async () => {
await bot.editStatus('online', {name: `over ${bot.guilds.size} server(s)`, type: 3})
console.log(`${bot.user.username} online in ${bot.guilds.size} guild(s) with ${bot.users.size} users in them`)
})
bot.on('messageCreate', async (message) => {
let curGuild = await fetchOrCreateGuild(message)
if (!curGuild) {
curGuild = {
prefix: '>'
}
}
if(message.author.bot || !message.content.startsWith(curGuild.prefix)) {
return
}
const reply = (string, color = 'green', content = '') => send(message.channel.id, embedBuilder(string, color), content)
const curUser = await fetchOrCreateUser(message)
const msg = message.content.trim().substr(curGuild.prefix.length).split(' ')
const ctx = Object.assign({}, context, {
message,
msg,
reply,
user: curUser,
guild: curGuild,
serverOwner: message.member.guild.ownerID !== message.author.id
})
const args = message.content.trim().substr(ctx.guild.prefix.length).toLowerCase().split(' ')
await trigger('cmd', ctx, args)
})
bot.on('error', (error) => {
if (error.code && (error.code == 1006 || error.code == 1001)) {
console.log('Connection reset')
} else {
console.log(error)
}
})
/*
----------------------------------------------------------------------------
Channel Events
----------------------------------------------------------------------------
*/
bot.on('channelCreate', async (channel) => {
await Channel.channel_create(context,channel)
})
bot.on('channelUpdate', async (channel, oldChannel) => {
await Channel.channel_update(context, channel, oldChannel)
})
bot.on('channelDelete', async (channel) => {
await Channel.channel_delete(context, channel)
})
bot.on('channelPinUpdate', async (channel, timestamp, oldtimestamp) => {
await Channel.channel_pin_update(context, channel, timestamp, oldtimestamp)
})
/*
----------------------------------------------------------------------------
Guild Events
----------------------------------------------------------------------------
*/
bot.on('guildCreate', async (guild) => {
await Guild.guild_create(context, guild)
})
bot.on('guildDelete', async (guild) => {
await Guild.guild_delete(context, guild)
})
bot.on('guildEmojisUpdate', async (guild, newEmoji, oldEmoji) => {
await Guild.emoji_update(context, guild, newEmoji, oldEmoji)
})
bot.on('guildUpdate', async (newGuild, oldGuild) => {
await Guild.guild_update(context, newGuild, oldGuild)
})
bot.on('guildBanAdd', async (guild, user) => {
await Guild.ban_add(context, guild, user)
})
bot.on('guildBanRemove', async (guild, user) => {
await Guild.ban_remove(context, guild, user)
})
bot.on('inviteDelete', async (guild, invite) => {
await Guild.invite_delete(context, guild, invite)
})
bot.on('inviteCreate', async (guild, oldInvite) => {
await Guild.invite_create(context, guild, oldInvite)
})
/*
----------------------------------------------------------------------------
Role Events
----------------------------------------------------------------------------
*/
bot.on('guildRoleCreate', async (guild, role) => {
await Role.role_create(context, guild, role)
})
bot.on('guildRoleDelete', async (guild, role) => {
await Role.role_delete(context, guild, role)
})
bot.on('guildRoleUpdate', async (guild, role, oldRole) => {
await Role.role_update(context, guild, role, oldRole)
})
/*
----------------------------------------------------------------------------
Member Events
----------------------------------------------------------------------------
*/
bot.on('guildMemberAdd', async (guild, member) => {
await Member.member_add(context, guild, member)
})
bot.on('guildMemberRemove', async (guild, member) => {
await Member.member_remove(context, guild, member)
})
bot.on('guildMemberUpdate', async (guild, member, oldMember) => {
if (!oldMember || member.bot) {
return
}
await Member.member_update(context, guild, member, oldMember)
})
/*
----------------------------------------------------------------------------
Message Events
----------------------------------------------------------------------------
*/
bot.on('messageDelete', async (message) => {
await Message.message_delete(context, message)
})
bot.on('messageDeleteBulk', async (messageBulk) => {
await Message.message_bulk_delete(context, messageBulk)
})
bot.on('messageUpdate', async (message, oldMessage) => {
await Message.message_update(context, message, oldMessage)
})
/*
----------------------------------------------------------------------------
Reaction Events
----------------------------------------------------------------------------
*/
bot.on('messageReactionAdd', async (message, reaction, member) => {
let fetchedMessage = await bot.getMessage(message.channel.id, message.id).catch(e => e)
if (!fetchedMessage)
return
await Reaction.reaction_add(context, fetchedMessage, reaction, member)
})
bot.on('messageReactionRemove', async (message, reaction, memberID) => {
let fetchedMessage = await bot.getMessage(message.channel.id, message.id).catch(e => e)
if (!fetchedMessage || !fetchedMessage.channel.guild)
return
let member = fetchedMessage.channel.guild.members.get(memberID)
await Reaction.reaction_remove(context, fetchedMessage, reaction, member)
})
bot.on('messageReactionRemoveEmoji', async (message, emoji) => {
await Reaction.reaction_remove_emoji(context, message, emoji)
})
bot.on('messageReactionRemoveAll', async (message) => {
await Reaction.reaction_remove_all(context, message)
})
/*
----------------------------------------------------------------------------
Presence Events
----------------------------------------------------------------------------
*/
bot.on('presenceUpdate', async (member, presence) => {
await Member.presence_update(context, member, presence)
})
/*
----------------------------------------------------------------------------
User Events
----------------------------------------------------------------------------
*/
bot.on('userUpdate', async (user, oldUser) => {
await Member.user_update(context, user, oldUser)
})
/*
----------------------------------------------------------------------------
Voice Events
----------------------------------------------------------------------------
*/
bot.on('voiceChannelJoin', async (member, voiceChannel) => {
await Voice.channel_join(context, member, voiceChannel)
})
bot.on('voiceChannelLeave', async (member, voiceChannel) => {
await Voice.channel_leave(context, member, voiceChannel)
})
bot.on('voiceChannelSwitch', async (member, newChannel, oldChannel) => {
await Voice.channel_switch(context, member, newChannel, oldChannel)
})
bot.on('voiceStateUpdate', async (member, oldVoiceState) => {
await Voice.state_update(context, member, oldVoiceState)
})
const tick = () => {
const now = new Date()
checkReminders(context, now)
}
const ctick = () => {
const now = new Date()
deleteReminders(context, now)
}
setInterval(tick.bind({}, context), 1000)
setInterval(ctick.bind({}, context), 60000)
bot.connect()