-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.js
287 lines (254 loc) · 12.1 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
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
const fs = require('fs');
const Discord = require('discord.js');
const globalSettings = require('./globalSettings.dev.json');
const serverSettings = require('./serverSettings.json');
const isUserAdmin = require('./src/isUserAdmin');
const getPrompt = require('./src/getPrompt');
const logMessage = require('./src/logMessage');
let customCommands = require('./customCommands/customcommands.json');
let customResponses = require('./customCommands/customresponses.json');
// Merge all languages
let lang = {};
let commands = {};
lang.english = require('./languages/english.json');
commands.english = require('./languages/commands/english.json');
lang.french = require('./languages/french.json');
commands.french = require('./languages/commands/french.json');
const client = new Discord.Client();
const nanoWords = [1667, 3333, 5000, 6667, 8333, 10000, 11667, 13333, 15000, 16667, 18333, 20000, 21667, 23333, 25000, 26667, 28333, 30000, 31667, 33333, 35000, 36667, 38333, 40000, 41667, 43333, 45000, 46667, 48333, 50000];
String.prototype.replaceAll = function (search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
client.on('ready', () => {
console.log(lang.english.consoleLanguage);
});
client.on('message', message => {
if (!message.author.bot) {
function createNewCustomCommand(command, response, guildCustomCommands, guildCustomResponses) {
guildCustomCommands.push(command);
guildCustomResponses.push(response);
customCommands[guildprop.id] = guildCustomCommands;
customResponses[guildprop.id] = guildCustomResponses;
fs.writeFile('./customCommands/customcommands.json', JSON.stringify(customCommands), (err) => {
if (err) {
console.log("An error occured while saving the custom commands for server: " + guildprop.id);
console.log(err);
}
});
fs.writeFile('./customCommands/customresponses.json', JSON.stringify(customResponses), (err) => {
if (err) {
console.log("An error occured while saving the custom response for server: " + guildprop.id);
console.log(err);
}
});
return "new";
}
function addCustomCommand(command, response) {
if (command == response) {
return "same";
}
if (!customCommands[guildprop.id]) {
customCommands[guildprop.id] = [];
let guildCustomCommands = [];
let guildCustomResponses = [];
return createNewCustomCommand(command, response, guildCustomCommands, guildCustomResponses);
} else {
let guildCustomCommands = customCommands[guildprop.id];
let guildCustomResponses = customResponses[guildprop.id];
if (guildCustomCommands.some(function (e) { return e == command })) {
guildCustomResponses[guildCustomCommands.indexOf(command)] = response;
fs.writeFile('./customCommands/customresponses.json', JSON.stringify(customResponses), (err) => {
if (err) {
console.log("An error occured while saving the custom response for server: " + guildprop.id);
console.log(err);
}
});
return "changed";
} else {
return createNewCustomCommand(command, response, guildCustomCommands, guildCustomResponses);
}
}
}
let guildprop = {};
if (message.guild) {
guildprop = {
"id": message.guild.id,
"name": message.guild.name
}
} else {
if (message.channel.recipient) {
// When the message is a DM
guildprop = {
"id": message.channel.id,
"name": message.channel.recipient.username
}
} else {
if (message.channel.name != null) {
// When the message is a group DM and a name was set for this group
guildprop = {
"id": message.channel.id,
"name": message.channel.name
}
} else {
// When the message is a group DM, but no name was set for this group
let dmNames = message.channel.recipients.nicks.array();
guildprop = {
"id": message.channel.id,
"name": dmNames
}
}
}
}
let messageLanguage = '';
if (serverSettings[guildprop.id]) {
messageLanguage = serverSettings[guildprop.id].language;
} else {
messageLanguage = 'english';
}
if (!customCommands[guildprop.id]) {
customCommands[guildprop.id] = [];
}
if (!customResponses[guildprop.id]) {
customResponses[guildprop.id] = [];
}
// Check custom commands
if (customCommands[guildprop.id].some(function (e) { return e == message.content })) {
const customCommandNumber = customCommands[guildprop.id].indexOf(message.content);
message.channel.send(customResponses[guildprop.id][customCommandNumber]);
}
// Register new commands
if (message.content.startsWith("!!custom") && isUserAdmin(message.member)) {
const messageArgs = message.content.split("--");
if (!messageArgs[2] || messageArgs[1].trim() == "" || messageArgs[2].trim() == "") {
message.channel.send(lang[messageLanguage].customSpecifyCommand)
} else {
const addCommand = addCustomCommand(messageArgs[1].trim(), messageArgs[2].trim());
if (addCommand == "new") {
message.channel.send(lang[messageLanguage].customCommandAdded);
} else if (addCommand == "changed") {
message.channel.send(lang[messageLanguage].customCommandChanged);
} else if (addCommand == "same") {
message.channel.send(lang[messageLanguage].customCommandsNeedToBeDifferent);
} else {
message.channel.send(lang[messageLanguage].somethingWentWrong);
}
}
}
// Delete custom command
if (message.content.startsWith("!delete")) {
const commandToDelete = message.content.substring(8);
if (customCommands[guildprop.id].indexOf(commandToDelete) > -1) {
const commandIndex = customCommands[guildprop.id].indexOf(commandToDelete)
customCommands[guildprop.id].splice(commandIndex, 1);
customResponses[guildprop.id].splice(commandIndex, 1);
message.channel.send("The custom command was deleted.");
fs.writeFile('./customCommands/customcommands.json', JSON.stringify(customCommands), (err) => {
if (err) {
console.log("An error occured while saving the custom commands for server: " + guildprop.id);
console.log(err);
}
});
fs.writeFile('./customCommands/customresponses.json', JSON.stringify(customResponses), (err) => {
if (err) {
console.log("An error occured while saving the custom responses for server: " + guildprop.id);
console.log(err);
}
});
} else {
message.channel.send("The command you want to remove from the channel does not exist.");
}
}
// Get wordcount during NaNoWriMo
if (message.content == commands[messageLanguage].wordcount) {
let today = new Date();
let dd = today.getDate();
let mm = today.getMonth() + 1;
if (mm < 11) {
message.channel.send(lang[messageLanguage].NanoNotStartedYet);
} else if (mm > 11) {
message.channel.send(lang[messageLanguage].NanoOver);
} else {
let todayWords = nanoWords[dd - 1]
message.channel.send(lang[messageLanguage].todayWordcount + todayWords + ".");
}
}
// Who da best?
if (message.content.toLowerCase() == 'who da best?') {
let whoDaBestRand = Math.floor(Math.random() * 100)
if (whoDaBestRand == 1) {
message.channel.send("Omlahid is da best!");
} else if (whoDaBestRand == 2) {
message.channel.send("I am. I am the best.");
} else {
message.reply(lang[messageLanguage].whoDaBest)
}
}
// Cheer! :cheer:
if (message.content == commands[messageLanguage].cheer) {
message.channel.send(lang[messageLanguage].cheeringMessage);
}
// Prompts
if (message.content == commands[messageLanguage].prompt) {
let currentPrompt = getPrompt();
message.channel.send(lang[messageLanguage].showPrompt + currentPrompt);
logMessage("received prompt: " + currentPrompt, message.author.username, guildprop.name)
}
// !help
if (message.content == commands[messageLanguage].help || message.content == "!help") {
message.channel.send(lang[messageLanguage].helpMessage);
}
// Pokemon
if (message.content.includes("pokemon")) {
logMessage(" mentionned pokemon", message.author.username, guildprop.name);
}
// Glow Cloud
if (message.content.includes("glow cloud")) {
message.channel.send("All Hail the Glow Cloud");
logMessage("All Hail the Glow Cloud", "", guildprop.name);
}
if (message.content.startsWith("!language") && isUserAdmin(message.member)) {
let id = guildprop.id;
let enteredCommand = message.content.split(" ");
let newServerLanguage = enteredCommand[1].toLowerCase();
if (serverSettings[id]) {
if (lang[newServerLanguage]) {
serverSettings[id].language = newServerLanguage;
fs.writeFile('serverSettings.json', JSON.stringify(serverSettings), (err) => {
if (err) {
logMessage("An error occured while trying to save the server settings.", "", guildprop.name)
console.log(err);
}
});
message.channel.send("The language for this server was changed to " + newServerLanguage);
} else {
message.channel.send('This language is invalid. The available languages are: English and French.');
}
} else {
let newServerSettings = {
"language": newServerLanguage,
"approvedAdmins": []
}
serverSettings[id] = newServerSettings;
let pushSettings = JSON.stringify(serverSettings);
fs.writeFile('serverSettings.json', pushSettings, (err) => {
if (err) {
console.log("An error occured while trying to save the server settings.");
console.log(err);
}
});
message.channel.send("The language for this server was changed to " + newServerLanguage + ".");
}
}
}
});
client.on("error", () => {
console.log("An unexpected error occurred on Discord's end. The bot will continue to run as expected.");
});
// Client token, required for the bot to work
try {
client.login(globalSettings.token);
}
catch (e) {
console.log("ERROR: No account was linked to your bot. Please provide a valid authentication token. You can change it in the globalSettings.json file.")
}