-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
346 lines (295 loc) · 9.95 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
346
const Discord = require("discord.js");
const client = new Discord.Client();
// Requires file called config.json. Containing {"token":"token"}
const config = require("./config.json");
const fs = require('fs');
// Runs when activated
client.once("ready", () => {
console.log("Ready!");
var value = Math.floor(Math.random() * (20)) + 1;
client.user.setPresence({ game: { name: "Rolled a " + value, type: 0 } });
client.user.setStatus("online");
});
// Runs when disconnecting
client.on("disconnect", () => {
console.log("Shutting down");
client.user.setStatus("invisible");
});
// Error handler
client.on("error", (e) => console.error(e));
client.on("warn", (e) => console.warn(e));
client.on("debug", (e) => console.info(e));
let pingCounter = 0;
// Runs when message is sent
client.on('message', message => {
// Gets the prefix from the config file. {"prefix":"prefix"}
const prefix = config.prefix;
// Removes the prefix from message, and then shifts to lowercase
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// Username of player
const playerUsername = message.author.username;
// Time and Date
const time = new Date().getTime();
const date = new Date(time);
// if (message.channel.id == "663516517798772786") {
// switch(message.content.toLowerCase) {
// case "172":
// message.channel.send("173");
// break;
// case "178":
// message.channel.send("Children of the Swamp");
// break;
// }
// }
if (message.content.startsWith("178")) {
message.channel.send("Children of the Swamp");
}
// To prevent addition processing beyond when called upon. As well as to prevent bots from activating.
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
const commandLog = playerUsername + " just tried to run " + command + " at " + date;
const log = config.log;
// Sends log to console, and records to text file.
console.log(commandLog);
fs.appendFile(log, '\r\n' + commandLog, function(err) {
if (err) throw err;
});
if (command !== "ping") {
pingCounter = 0;
}
switch(command) {
case "roll": {
// Creates variables for use in the try catch
let number;
let addition;
// For both, tries to process, then if there is nothing there, catches the error
try {
// Replaces anything not included by the [^] with nothing so that only numbers remain
number = args[0].replace(/[^0-9-+]/gi, '');
} catch (error) {
number = 0;
}
try {
addition = args[1].replace(/[^0-9-+]/gi, '');
} catch (error) {
addition = 0;
}
// Records number
console.log("Input: " + number + " Addition: " + addition);
/*
* Run the dice roll, by taking a value between 0 and 1,
* multiplying it by the number, adding 1 to it to allow
* for 0 and the actual number, then returns it.
*/
message.channel.send("Rolling a d" + number + ".");
var value = Math.floor(Math.random() * (number)) + 1;
var modified = (value + parseInt(addition));
if (addition === 0) {
message.channel.send("Rolled: " + value);
} else {
message.channel.send("Rolled: " + value + ". With modifier: " + modified);
}
console.log("Output: " + value);
client.user.setPresence({ game: { name: "Rolled a " + value, type: 0 } });
break;
}
case "multiroll": {
// Creates variables for use in the try catch
let number;
let times;
let addition;
let sum;
// For all, tries to process, then if there is nothing there, catches the error
try {
// Replaces anything not included by the [^] with nothing so that only numbers remain
number = args[0].replace(/[^0-9-+]/gi, '');
} catch (error) {
number = 0;
}
try {
times = args[1].replace(/[^0-9]/gi, '');
if (message.author.id !== config.ownerID) {
if (times > 20) {times = 20;}
}
} catch (error) {
times = 1;
}
try {
addition = args[2].replace(/[^0-9-+]/gi, '');
} catch (error) {
addition = 0;
}
if (args[3] === "true") {
sum = true;
} else {
sum = false;
}
// Records number
console.log("Input: " + number + " Times: " + times + " Addition: " + addition);
/*
* Run the dice roll, by taking a value between 0 and 1,
* multiplying it by the number, adding 1 to it to allow
* for 0 and the actual number, then returns it.
* Loops over with the set amount of times, and if
* sum is true, sums up the number.
*/
message.channel.send("Rolling a d" + number + " " + times + " times.");
let totalValue = "";
let totalModified = "";
let totalValueSum = 0;
let totalModifiedSum = 0;
for (let index = 0; index < times; index++) {
value = Math.floor(Math.random() * (number)) + 1;
modified = (value + parseInt(addition));
if (index > 0) {
totalValue = totalValue + ", ";
totalModified = totalModified + ", ";
if (sum === true) {
totalValueSum = totalValueSum + value;
totalModifiedSum = totalModifiedSum + modified;
}
}
totalValue = totalValue + value;
totalModified = totalModified + modified;
}
if (addition === 0) {
message.channel.send("Rolled: " + totalValue);
if (sum === true) {message.channel.send("Sum: " + totalValueSum);}
} else {
message.channel.send("Rolled: " + totalValue + ".");
if (sum === true) {message.channel.send("Sum: " + totalValueSum);}
message.channel.send("With modifier: " + totalModified + ".");
if (sum === true) {message.channel.send("Sum: " + totalModifiedSum);}
}
console.log("Output: " + totalValue);
client.user.setPresence({ game: { name: "Rolled a " + value, type: 0 } });
break;
}
case "ping": {
if (pingCounter == 7) {
message.channel.send("Im still alive okay?");
pingCounter++;
} else if (pingCounter == 12) {
message.channel.send("Plz stop");
pingCounter++;
} else if (pingCounter >= 15) {
pingCounter++;
} else {
message.channel.send("Pong!").then((msg) => {
// Amends message with the time taken.
msg.edit("Pong! Took " + (new Date().getTime() - message.createdTimestamp) + " ms to respond");
});
pingCounter++;
}
break;
}
// Secret command. Cole wanted a pong command.
case "pong": {
message.channel.send("the same thing as ping - Cole Dewis 2019");
break;
}
// To ping isaiah
case "isaiah": {
// Gets ID of User
const user = message.author.id;
// Details
const record = '\r\n' + "<@" + user + ">";
const file = config.file;
// Adds ID to the list for record keeping/knowing who pinged the most
fs.appendFile(file, record, function(err) {
if (err) throw err;
});
// Pings Isaiah
message.channel.send("<@596693413651546114>");
break;
}
case "prefix": {
// Condition is if the person sending is the owner based on id
if (message.author.id == config.ownerID) {
// Gets prefix from command (!prefix + would take the +)
const newPrefix = message.content.split(" ").slice(1, 2)[0];
if (typeof newPrefix === "undefined") {
return;
} else {
// Change the configuration in memory
config.prefix = newPrefix;
// Responds to changed prefix
message.channel.send("Prefix changed to " + newPrefix);
// Console record
console.log("Prefix changed to " + newPrefix);
// Save the file
fs.writeFile("./config.json", JSON.stringify(config), () => console.error);
}
} else {
// Responds to denied user
message.channel.send("Sorry " + playerUsername + ", but you don't have access to this command.");
message.channel.send("If you believe this is an error, please check the config.json file, and correct your ownerID.");
message.channel.send("I mean, if you have access to the file.");
}
break;
}
case "github": {
message.channel.send("Code can be found at: https://github.com/Kulkinz/dnd-bot. So you know it isn't rigged.");
break;
}
case "xkcd": {
let xkcd;
// For both, tries to process, then if there is nothing there, catches the error
try {
// Replaces anything not included by the [^] with nothing so that only numbers remain
xkcd = args[0].replace(/[^0-9-+]/gi, '');
} catch (error) {
message.channel.send("Please put proper xkcd number;");
break;
}
// Sends link to respective xkcd comic
message.channel.send("https://xkcd.com/" + xkcd + "/");
break;
}
case "whatif": {
let whatif;
// For both, tries to process, then if there is nothing there, catches the error
try {
// Replaces anything not included by the [^] with nothing so that only numbers remain
whatif = args[0].replace(/[^0-9-+]/gi, '');
} catch (error) {
message.channel.send("Please put proper xkcd what if? number;");
break;
}
// Sends link to respective xkcd what-if
message.channel.send("https://what-if.xkcd.com/" + whatif + "/");
break;
}
case "help": {
// Sends to channel information that it was DM'd
message.channel.send("Sent to DMs");
// Sends to DMs. Creates embed, and sends it.
const helpEmbed = new Discord.RichEmbed()
.setColor('#ab2c85')
.setTitle("Commands")
.setDescription(playerUsername + ". The current commands are as follows and I quote;")
.addField(prefix + "help", "This command.")
.addField(prefix + "roll d[number] <+/-modifier>", "Rolls a dice up to the given number. Can set an optional modifier.")
.addField(prefix + "multiroll d[number] <amount> <+/-modifier> <sum=true/false>", "Rolls multiple dice up to the given number. Can set amount, modifier, or if you would like to sum the numbers up.")
.addField(prefix + "xkcd [number]", "Pulls up the respective xkcd comic.")
.addField(prefix + "whatif [number]", "Pulls up the respective xkcd what if?.")
.addField(prefix + "github", "Links to the github.")
.addField(prefix + "ping", "Pong!")
.setTimestamp();
message.author.send(helpEmbed);
// Admin commands
const adminEmbed = new Discord.RichEmbed()
.setColor('f542b9')
.setTitle("Admin Commands")
.addField(prefix + "prefix [Prefix]", "Changes Prefix.")
.setTimestamp();
// If the user is an owner, sends owner commands.
if (message.author.id == config.ownerID) {
message.author.send(adminEmbed);
}
break;
}
}
});
// Allows login
client.login(config.token);