-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
630 lines (563 loc) · 24 KB
/
app.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
const Discord = require("discord.js");
const RiotRequest = require("riot-lol-api");
const bot = new Discord.Client();
require('dotenv/config');
const YT_KEY = process.env.YT_KEY; //Youtube key
const token = process.env.DISCORD_TOKEN; //Discord token
const riotRequest = new RiotRequest("my_api_key"); //Riot api key -- still need to do this for when I add league stuff to the bot
const ytdl = require("ytdl-core");
const { YouTube } = require("better-youtube-api");
const youtube = new YouTube(YT_KEY, { cache: false });
const axios = require("axios");
const PREFIX = "uwu "; //Command prefix for people to interact with the bot
var servers = {};
bot.on("ready", () => {
bot.user.setStatus("available");
bot.user.setPresence({
game: {
name: PREFIX + "commands",
},
});
console.log("This bot is online!");
});
bot.on("message", async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(PREFIX)) return;
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case "coinflip":
if (Math.floor(Math.random() * 2) == 1) {
message.reply("heads");
} else {
message.reply("tails");
}
break;
case "commands":
let embed = new Discord.MessageEmbed()
.setTitle("List of Commands, UwU")
.addField(PREFIX + "bedtime", "I read a bedtime story out loud.")
.addField(PREFIX + "coinflip", "50/50 heads or tails.")
.addField(PREFIX + "dance", "I dance for u.")
.addField(PREFIX + "joke", "I tell a joke.")
.addField(PREFIX + "cat", "I send u a cat photo.")
.addField(PREFIX + "voiceping", "I ping everyone in your voice channel.")
.addField(
PREFIX + "cap [# of captains] except @User1 @User2",
"I pick team captains randomly from your voice channel. Optionally, to exclude specific users, type 'except' and @mention them in the message." +
"\n E.g: '" +
PREFIX +
"cap 2' or '" +
PREFIX +
"cap 2 except @" +
message.author.username +
"'"
)
.addField(
PREFIX + "randomteams [# of teams] [name 1],[name 2],...,[name n]",
"I generate random teams from a given list of players separated by commas.\n E.g.: '" +
PREFIX +
"randomteams 2 Faker, Darth Vader, Bjergsen, Squidward Tentacles'"
)
.addField(
PREFIX + "randomteamsvoice [# of teams] except @User1 @User2",
"I generate random teams from everyone in your current voice channel, not including my fellow bots. Optionally, to exclude specific users, type 'except' and @mention them in the message." +
"\n E.g: '" +
PREFIX +
"randomteamsvoice 2' or '" +
PREFIX +
"randomteamsvoice 2 except @" +
message.author.username +
"' or '" +
PREFIX +
"rtv 2' or '" +
PREFIX +
"rtv 2 except @" +
message.author.username +
"'"
)
.addField(
PREFIX + "rtv [# of teams] except @User1 @User2", "Same as randomteamsvoice, just shortened so you don't have to type it all out."
)
.addField(
PREFIX + "play song/video title or YouTube URL",
"Add a song to the queue. I'll join the voice channel and sing to you UwU. \nE.g. '" +
PREFIX +
"play spaghetti asmr' or '" +
PREFIX +
"play https://youtu.be/oN-HuMlf3bk'"
)
.addField(PREFIX + "skip", "Skips the current song.")
.addField(
PREFIX + "stop",
"Clears the song queue and I leave the voice channel :(."
)
.setColor(0xde78e3);
message.reply("Hello I'm the UwU bot");
message.channel.send(embed);
break;
//------- Real commands start here --------\\
//----- Random GArbage commands -----\\
case "dance":
try {
let response = await axios.get(
"http://api.giphy.com/v1/gifs/random?api_key=VvFigFsfhXlS3ZZws76zv9qgJ3hBvUpB&tag=dancing&rating=pg"
);
let danceResponse = response.data;
console.log(danceResponse);
message.channel.send(danceResponse.data.embed_url);
} catch (err) {
console.log(err)
message.channel.send(
"I can't dance right now :(."
);
}
break;
case "bedtime":
message.channel.send("Once upon a time there was an uwu, the end.", {
tts: true,
});
break;
case "joke":
try {
let response = await axios.get(
"https://official-joke-api.appspot.com/random_joke"
);
let joke = response.data;
message.channel.send(joke.setup + "\n" + joke.punchline + " UwU.");
} catch {
message.channel.send(
"Where does UwU bot get its jokes?\nA bad joke API cause it's broken now UwU :(."
);
}
break;
//----- RANDOM IMAGES -----\\
case "cat":
try {
let response = await axios.get(
"https://api.thecatapi.com/v1/images/search?size=full"
);
let catResponse = response.data[0];
console.log(catResponse);
let catEmbed = new Discord.MessageEmbed()
.setImage(catResponse.url)
.setColor(0xde78e3);
message.channel.send(catEmbed);
} catch (err) {
console.log(err)
message.channel.send(
"Where does UwU bot get its cats?\nA bad cat API cause it's broken now UwU :(."
);
}
break;
case "anime":
break;
//----- League stuff? -----\\
case "randomteams":
//Will randomly separate a list of users evenly into a specified number of teams (up to 5 teams)
//use case is: uwu randomteams [# of teams] [name 1],[name 2],[name 3],...,[name n]
if (args[1] === "fromimage") {
//Find the most recent image that has been uploaded by the user
//use some javascript OCR library to get the names of everyone in the custom lobby
//randomize teams
message.channel.send("I can't do that yet uwu.");
return;
}
//check if argument is a number
console.log(args.length);
console.log("Num teams " + args[1]);
var numteams = parseInt(args[1]);
if (!Number.isInteger(numteams)) {
message.channel.send(
"Provide the number of teams you want to make (1 through 5).\nE.g.: " +
PREFIX +
"randomteams 2 Faker, Bjergsen, Bdd, Panama Hat"
);
return;
}
//Check if argument is between 1 and 5
if (!(numteams >= 1 && numteams <= 5)) {
message.channel.send(
"You can only make between 1 and 5 teams.\nE.g.: " +
PREFIX +
"randomteams 2 Faker, Bjergsen, Bdd, Panama Hat"
);
return;
}
let players = message.content
.substring(PREFIX.length + "randomteams n".length)
.split(",");
//Filter the blanks
var filtered = players.filter(function (el) {
return el != null && el.trim() != "";
});
if (filtered.length == 0) {
message.channel.send(
"Please enter at least one name to make teams, uwu.\nE.g.: " +
PREFIX +
"randomteams 2 Faker, Bjergsen, Bdd, Panama Hat"
);
return;
}
if (filtered.length < numteams) {
message.channel.send(
"You didn't give me enough players to make that many teams, :(."
);
return;
}
//randomize array
shuffle(filtered);
var split = chunkArrayInGroups(
filtered,
Math.ceil(filtered.length / numteams)
);
console.log(split);
let team_embed = new Discord.MessageEmbed()
.setTitle("Teams")
.setColor(0xde78e3)
.setThumbnail(
"https://vignette.wikia.nocookie.net/leagueoflegends/images/1/1e/UWU_Emote.png/revision/latest?cb=20190917205601"
);
for (var i = 0; i < numteams; i++) {
var team_str = "";
for (var j = 0; j < split[i].length; j++)
team_str += split[i][j].trim() + "\n";
team_embed.addField("Team " + (i + 1), team_str);
}
message.channel.send(team_embed);
break;
case "rtv":
case "randomteamsvoice":
//Will randomly separate users in voice channel evenly into a specified number of teams (up to 50 teams)
//Check if user is in a voice channel
if (!message.member.voice.channel) {
message.channel.send(
"You need to be in a voice channel to do this. :("
);
return;
}
//check if argument is a number
console.log(message.content);
console.log(message.mentions.users);
console.log(args.length);
console.log("Num teams " + args[1]);
var numteams = parseInt(args[1]);
var excludeOn = message.content.includes("except");
if (!Number.isInteger(numteams)) {
var errMessage =
"Provide the number of teams you want to make (1 through 50).\nE.g.: " +
PREFIX +
"randomteamsvoice 2";
if (excludeOn) {
errMessage += " except <@" + message.author.id + ">";
}
message.channel.send(errMessage);
return;
}
//Check if argument is between 1 and 5
if (!(numteams >= 1 && numteams <= 50)) {
var errMessage =
"You can only make between 1 and 50 teams.\nE.g.: " +
PREFIX +
"randomteamsvoice 2";
if (excludeOn) {
errMessage += " except <@" + message.author.id + ">";
}
message.channel.send(errMessage);
return;
}
if (excludeOn && message.mentions.users.size == 0) {
message.channel.send(
"It looks like you tried to exclude people but didn't @ mention anyone (you can exclude multiple people too by just @ing them all). Try: " +
PREFIX +
"randomteamsvoice 2 except <@" +
message.author.id +
">"
);
}
var excludeOn = message.content.includes("except");
var filtered = []; // from voice channel
message.member.voice.channel.members.forEach(function (
guildMember,
guildMemberId
) {
console.log(guildMemberId, guildMember.user.username);
if (!guildMember.user.bot) {
//No bots and exclude any mentions
if (excludeOn) {
if (!message.mentions.users.has(guildMemberId)) {
filtered.push("<@" + guildMemberId + ">");
}
} else {
filtered.push("<@" + guildMemberId + ">");
}
}
});
if (filtered.length == 0) {
message.channel.send(
"Theres no available humans in your voice channel, uwu."
);
return;
}
if (filtered.length < numteams) {
message.channel.send(
"There aren't enough available players in your channel to make that many teams, :(."
);
return;
}
//randomize array
shuffle(filtered);
var split = chunkArrayInGroups(
filtered,
Math.ceil(filtered.length / numteams)
);
console.log(split);
let team_embed2 = new Discord.MessageEmbed()
.setTitle("Teams")
.setColor(0xde78e3)
.setThumbnail(
"https://vignette.wikia.nocookie.net/leagueoflegends/images/1/1e/UWU_Emote.png/revision/latest?cb=20190917205601"
);
for (var i = 0; i < numteams; i++) {
var team_str = "";
for (var j = 0; j < split[i].length; j++)
team_str += split[i][j].trim() + "\n";
team_embed2.addField("Team " + (i + 1), team_str);
}
message.channel.send(team_embed2);
break;
case "cap":
//Will randomly pick captains from the user's voice channel
//Check if user is in a voice channel
if (!message.member.voice.channel) {
message.channel.send(
"You need to be in a voice channel to do this. :("
);
return;
}
//check if argument is a number
console.log(message.content);
console.log(message.mentions.users);
console.log(args.length);
console.log("Num teams " + args[1]);
var numcaps = parseInt(args[1]);
var excludeOn = message.content.includes("except");
if (!Number.isInteger(numcaps)) {
var errMessage =
"Provide the number of captains you want to pick (1 through 50).\nE.g.: " +
PREFIX +
"cap 2";
if (excludeOn) {
errMessage += " except <@" + message.author.id + ">";
}
message.channel.send(errMessage);
return;
}
//Check if argument is between 1 and 5
if (!(numcaps >= 1 && numcaps <= 50)) {
var errMessage =
"You can only pick between 1 and 50 captains.\nE.g.: " +
PREFIX +
"cap 2";
if (excludeOn) {
errMessage += " except <@" + message.author.id + ">";
}
message.channel.send(errMessage);
return;
}
if (excludeOn && message.mentions.users.size == 0) {
message.channel.send(
"It looks like you tried to exclude people but didn't @ mention anyone (you can exclude multiple people too by just @ing them all). Try: " +
PREFIX +
"cap 2 except <@" +
message.author.id +
">"
);
}
var filtered = []; // from voice channel
message.member.voice.channel.members.forEach(function (
guildMember,
guildMemberId
) {
console.log(guildMemberId, guildMember.user.username);
if (!guildMember.user.bot) {
//No bots and exclude any mentions
if (excludeOn) {
if (!message.mentions.users.has(guildMemberId)) {
filtered.push("<@" + guildMemberId + ">");
}
} else {
filtered.push("<@" + guildMemberId + ">");
}
}
});
if (filtered.length == 0) {
message.channel.send(
"There are no available humans in your voice channel, uwu."
);
return;
}
if (filtered.length < numcaps) {
message.channel.send(
"There aren't enough available players in your channel to pick that many captains, :(."
);
return;
}
//randomize array
shuffle(filtered);
let team_embed3 = new Discord.MessageEmbed()
.setTitle("Team Captains")
.setColor(0xe748f0)
.setThumbnail(
"https://vignette.wikia.nocookie.net/leagueoflegends/images/1/1e/UWU_Emote.png/revision/latest?cb=20190917205601"
);
for (var i = 0; i < numcaps; i++) {
team_embed3.addField("Captain " + (i + 1), filtered[i]);
}
message.channel.send(team_embed3);
break;
case "voiceping":
//Will randomly separate a usrs in voice channel evenly into a specified number of teams (up to 5 teams)
//Check if user is in a voice channel
if (!message.member.voice.channel) {
message.channel.send(
"You need to be in a voice channel to do this. :("
);
return;
}
let resp = ""
message.member.voice.channel.members.forEach(function (
guildMember,
guildMemberId
) {
console.log(guildMemberId, guildMember.user.username);
if (!guildMember.user.bot) {
resp = resp + guildMember.toString() + " ";
}
});
if (resp == "") {
message.channel.send(
"There are no available humans in your voice channel, uwu."
);
return;
}
message.channel.send(resp);
break;
//----- MUSIC BOT THINGS -----\\
case "play":
function play(connection, message) {
var server = servers[message.guild.id];
const stream = ytdl(server.queue[0], {
filter: "audioonly",
highWaterMark: 1 << 25,
});
console.log("ATTEMPT PLAY: " + server.queue[0]);
server.dispatcher = connection.play(stream).on("error", error => console.error(error));
server.queue.shift();
server.dispatcher.on("finish", function () {
if (server.queue[0]) {
play(connection, message);
} else {
connection.disconnect();
}
});
}
var search_term = message.content.substring(PREFIX.length + 5).trim();
console.log(search_term);
if (!search_term) {
message.channel.send(
"You need to provide a YouTube link or a song title."
);
return;
}
if (!message.member.voice.channel) {
message.channel.send(
'You need to be in a voice channel to play music!'
);
return;
}
if (!message.member.voice.channel.joinable) {
message.channel.send("UwU, I wasnt able to join the voice channel. :(");
return;
}
if (!servers[message.guild.id]) servers[message.guild.id] = { queue: [] }; // Create a song queue for the server you're in
var server = servers[message.guild.id];
try {
//const video = await youtube.getVideo(search_term);
const response = await youtube.searchVideos(search_term, 1);
console.log(response.results.length + " " + search_term);
console.log(response.results);
if (response.results.length === 0) {
message.channel.send("UwU, I couldn't find this video. :(");
return;
}
console.log(
"TITLE: " +
response.results[0].title +
"\n" +
"URL: " +
response.results[0].shortUrl
);
server.queue.push(response.results[0].url);
let embed = new Discord.MessageEmbed()
.setTitle("Added to the queue, UwU")
.addField("Title", response.results[0].title, false)
.addField("Link", response.results[0].shortUrl, false)
.addField("Requester", message.author, false)
.setColor(0xde78e3)
.setThumbnail(response.results[0].thumbnails.high.url);
message.channel.send(embed);
if (!message.member.voice.connection) {
message.member.voice.channel.join().then(function (connection) {
play(connection, message);
});
}
} catch (e) {
console.log(e);
message.channel.send(
"UwU, something went wrong looking for this video. :("
);
}
break;
case "skip":
var server = servers[message.guild.id];
if (server.dispatcher) server.dispatcher.end();
message.channel.send("Skipped the song UwU.");
break;
case "stop":
var server = servers[message.guild.id];
if (message.member.voice.connection) {
for (var i = server.queue.length - 1; i >= 0; i--) {
server.queue.splice(i, 1);
}
server.dispatcher.end();
message.channel.send(
"Ended song queue, leaving the voice channel UwU."
);
console.log("stopped the queue");
}
break;
default:
message.reply(
"I don't recognize that command. Type 'uwu commands' to see a list of commands and their syntax UwU."
);
break;
}
});
bot.login(token);
function shuffle(array) {
var idx = array.length;
var rand, temp;
while (idx !== 0) {
rand = Math.floor(Math.random() * idx--);
temp = array[idx];
array[idx] = array[rand];
array[rand] = temp;
}
return array;
}
function chunkArrayInGroups(arr, size) {
var chunked = [];
for (var i = 0; i < arr.length; i += size)
chunked.push(arr.slice(i, i + size));
return chunked;
}