-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
335 lines (287 loc) · 13.1 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
const Discord = require('discord.js');
const client = new Discord.Client();
const config = require("./config/config.json");
const SQLite = require("better-sqlite3");
const sql = new SQLite('./config/database.sqlite');
const Music = require('./discord.js-musicbot-addon');
const fetch = require('node-fetch');
const schedule = require('node-schedule');
const process = require('process');
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
const DEBUG = 0;
const defaultGuildSettings = {
prefix: config.defaultPrefix,
logChannel: "the_archives",
modRole: "Mod",
adminRole: "Admin",
welcomeChannel: "welcome",
welcomeMessage: "Say hello to {{user}}, everyone! We all need a warm welcome sometimes :D"
}
let prefixObj = new Map(); // Make a new map.
const defaultMusicSettings = {
youtubeKey: config.youtubeAPIKey,
defaultPrefix: "???",
botPrefix: prefixObj,
messageNewSong: false,
bigPicture: false,
maxQueueSize: 0,
defVolume: 3,
anyoneCanSkip: true,
messageHelp: false,
anyoneCanAdjust: false,
anyoneCanLeave: true,
requesterName: true,
inlineEmbeds: true,
help: {
enabled: true, // True/False statement.
alt: ["player help"], // Array of alt names (aliases).
help: "Help for commands.", // String of help text.
name: "mhelp", // Name of the command.
usage: "{{botPrefix}}mhelp", // Usage text. {{prefix}} will insert the bots prefix.
exclude: false // Excludes the command from the help command.
},
advancedMode: { // The advancedMode object.
enabled: true
}
}
let musicSettings;
var musicStartSettings = defaultMusicSettings;
client.login(config.botToken);
client.on('ready', ()=> {
console.log('The bot is now online!');
client.user.setActivity('| ' + defaultGuildSettings.prefix, { type: 'WATCHING' })
//client.user.setActivity('and Listening to YOU', { type: 'WATCHING' })
// Check if the table "scores" exists.
const tableScores = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
if (!tableScores['count(*)']) {
// If the table isn't there, create it and setup the database correctly.
sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);").run();
// Ensure that the "id" row is always unique and indexed.
sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
sql.pragma("synchronous = 1");
sql.pragma("journal_mode = wal");
}
// And then we have two prepared statements to get and set the score data.
client.getScore = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
client.setScore = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (@id, @user, @guild, @points, @level);");
// Check if the table "guildSettings" exists.
const tableGuildSettings = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'guildSettings';").get();
if (!tableGuildSettings['count(*)']) {
// If the table isn't there, create it and setup the database correctly.
sql.prepare("CREATE TABLE guildSettings (id TEXT PRIMARY KEY, guild TEXT, prefix TEXT, logChannel TEXT, modRole TEXT, adminRole TEXT, welcomeChannel TEXT, welcomeMessage TEXT);").run();
// Ensure that the "id" row is always unique and indexed.
sql.prepare("CREATE UNIQUE INDEX idx_guildSettings_id ON guildSettings (id);").run();
sql.pragma("synchronous = 1");
sql.pragma("journal_mode = wal");
}
// And then we have prepared statements to get and set guildSettings data.
client.getGuildSettings = sql.prepare("SELECT * FROM guildSettings WHERE guild = ?");
client.setGuildSettings = sql.prepare("INSERT OR REPLACE INTO guildSettings (id, guild, prefix, logChannel, modRole, adminRole, welcomeChannel, welcomeMessage) VALUES (@id, @guild, @prefix, @logChannel, @modRole, @adminRole, @welcomeChannel, @welcomeMessage);");
// Check if the table "musicSettings" exists.
const tableMusicSettings = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'musicSettings';").get();
if (!tableMusicSettings['count(*)']) {
// If the table isn't there, create it and setup the database correctly.
sql.prepare("CREATE TABLE musicSettings (id TEXT PRIMARY KEY, guild TEXT, storedMusicSettings TEXT, tracked TEXT);").run();
// Ensure that the "id" row is always unique and indexed.
sql.prepare("CREATE UNIQUE INDEX idx_musicSettings_id ON musicSettings (id);").run();
sql.pragma("synchronous = 1");
sql.pragma("journal_mode = wal");
}
// And then we have prepared statements to get and set guildSettings data.
client.getMusicSettings = sql.prepare("SELECT * FROM musicSettings WHERE guild = ?");
client.getMusicSettingsByTracked = sql.prepare("SELECT * FROM musicSettings WHERE tracked = ?");
client.setMusicSettings = sql.prepare("INSERT OR REPLACE INTO musicSettings (id, guild, storedMusicSettings, tracked) VALUES (@id, @guild, @storedMusicSettings, @tracked);");
//Pull prefix data for music bot and initilize them
const trackedList = client.getMusicSettingsByTracked.all('true');
for (var i = 0; i < trackedList.length; i++) {
var tmpPrefix = JSON.parse(trackedList[i].storedMusicSettings);
Music.bot.updatePrefix(trackedList[i].guild, tmpPrefix.botPrefix);
}
});
client.on('message', async message => {
if (message.author.bot) return;
let score;
let guildSettings;
if (message.guild) {
// Sets default server settings
guildSettings = client.getGuildSettings.get(message.guild.id);
if (!guildSettings) {
guildSettings = { id: `${message.guild.id}-${client.user.id}`, guild: message.guild.id, prefix: defaultGuildSettings.prefix, logChannel: defaultGuildSettings.logChannel, modRole: defaultGuildSettings.modRole, adminRole: defaultGuildSettings.adminRole, welcomeChannel: defaultGuildSettings.welcomeChannel, welcomeMessage: defaultGuildSettings.welcomeMessage };
client.setGuildSettings.run(guildSettings);
guildSettings = client.getGuildSettings.get(message.guild.id);
}
musicSettings = client.getMusicSettings.get(message.guild.id);
if (!musicSettings) {
var tmpSqlMusicSettings = defaultMusicSettings;
tmpSqlMusicSettings.botPrefix = guildSettings.prefix;
musicSettings = { id: `${message.guild.id}-${client.user.id}`, guild: message.guild.id, storedMusicSettings: JSON.stringify(tmpSqlMusicSettings), tracked: 'true'};
client.setMusicSettings.run(musicSettings);
musicSettings = client.getMusicSettings.get(message.guild.id);
musicStartSettings = JSON.parse(musicSettings.storedMusicSettings);
Music.bot.updatePrefix(message.guild.id, musicStartSettings.botPrefix);
}
else {
musicStartSettings = JSON.parse(musicSettings.storedMusicSettings);
}
score = client.getScore.get(message.author.id, message.guild.id);
if (!score) {
score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, points: 0, level: 1 }
}
score.points++;
const curLevel = Math.floor(0.1 * Math.sqrt(score.points));
if(score.level < curLevel) {
score.level++;
message.reply(`You've leveled up to level **${curLevel}**. Ain't that dandy?`);
}
client.setScore.run(score);
if (guildSettings.prefix != musicStartSettings.botPrefix) {
musicStartSettings.botPrefix = guildSettings.prefix;
musicSettings.storedMusicSettings = JSON.stringify(musicStartSettings);
client.setMusicSettings.run(musicSettings);
musicSettings = client.getMusicSettings.get(message.guild.id);
musicStartSettings = JSON.parse(musicSettings.storedMusicSettings);
Music.bot.updatePrefix(message.guild.id, musicStartSettings.botPrefix);
}
}
else {
// DM Message
return;
}
const prefix = guildSettings.prefix;
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
try {
command.execute(message, args, prefix, guildSettings, client, Discord, Music, fetch);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
return;
});
client.on('messageDelete', async (message) => {
if (message.author.bot) return;
//Don't log a message by a bot
let guildSettings;
if (message.guild) {
// Sets default server settings
guildSettings = client.getGuildSettings.get(message.guild.id);
if (!guildSettings) {
guildSettings = { id: `${message.guild.id}-${client.user.id}`, guild: message.guild.id, prefix: defaultGuildSettings.prefix, logChannel: defaultGuildSettings.logChannel, modRole: defaultGuildSettings.modRole, adminRole: defaultGuildSettings.adminRole, welcomeChannel: defaultGuildSettings.welcomeChannel, welcomeMessage: defaultGuildSettings.welcomeMessage };
}
}
var prefix = guildSettings.prefix;
if (guildSettings.logChannel === "off") return; //Logging is disabled, break out.
// Firstly, we need a logs channel.
var logs;
if (guildSettings.logChannel === defaultGuildSettings.logChannel) {
logs = message.guild.channels.cache.find(channel => channel.name === guildSettings.logChannel);
} else {
logs = message.guild.channels.cache.find(channel => channel.id === guildSettings.logChannel);
}
// If there is no logs channel, we can create it if we have the 'MANAGE_CHANNELS' permission
// Remember, this is completely options. Use to your best judgement.
if (message.guild.me.hasPermission('MANAGE_CHANNELS') && !logs) {
await message.guild.channels.create(guildSettings.logChannel, { type: 'text' });
guildSettings = client.getGuildSettings.get(message.guild.id);
logs = message.guild.channels.resolve(channel => channel.name === guildSettings.logChannel);
}
const fetchedLogs = await message.guild.fetchAuditLogs({
limit: 1,
type: 'MESSAGE_DELETE',
});
const entry = fetchedLogs.entries.first();
let user = "";
if (entry.extra.channel.id === message.channel.id
&& (entry.target.id === message.author.id)
&& (entry.createdTimestamp > (Date.now() - 5000))
&& (entry.extra.count >= 1)) {
user = entry.executor;
} else {
user = message.author;
}
if (message.author.id != client.user.id) {
// Check if stealth command was called
if (message.content.startsWith(prefix)) {
const command = message.content.slice(prefix.length).toLowerCase();
if (command.startsWith("stealth")) {
return;
}
}
if (user.bot) return; //Don't log a message deleted by a bot
if (message.content === "") return; // Don't post when images were deleted
// Created Deleted Message Embed to send to channel.
const deletedMessage = {
title: "A message by **" + message.author.username + "** was deleted but it said:",
description: message.content,
color: 5159289,
timestamp: new Date(),
"footer": {
"icon_url": user.avatarURL,
"text": "Deleted By: " + entry.executor.tag,
},
"author": {
"name": message.author.tag,
"icon_url": message.author.avatarURL
},
"thumbnail": {
"url": client.user.avatarURL
},
fields: [
{
name: '\u200b',
value: " - Written By: **" + message.author.tag + '** In <#' + message.channel.id + '>',
},
{
name: 'On ' + message.createdAt,
value: '\u200b'
}
],
};
logs.send({ embed: deletedMessage });
} else {
//message.channel.send("The thing happened");
}
// If we do not have permissions, console log both errors
if (!logs) {
return console.log('The logs channel does not exist and cannot be created');
}
});
var musicSetup = Music.start(client, musicStartSettings);
var j = schedule.scheduleJob('0 0 0/6 * * *', function(){
console.log('Scheduled Job fired!');
console.log(new Date());
});
process.on('SIGINT', function onSigint () {
console.info('Got SIGTERM. Graceful shutdown start', new Date().toISOString())
// start graceul shutdown here
shutdown();
});
process.on('SIGTERM', function onSigterm () {
console.info('Got SIGTERM. Graceful shutdown start', new Date().toISOString())
// start graceul shutdown here
shutdown();
});
function shutdown() {
console.log('Received kill signal, shutting down gracefully');
try {
j.cancel();
sql.close();
} catch (error) {
console.error(error);
process.exit(1);
}
setTimeout(() => {
console.error('Could not close connections in time, forcefully shutting down');
process.exit(1);
}, 10000);
process.exit();
}