-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtleBot.js
82 lines (64 loc) · 2.23 KB
/
turtleBot.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
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
var fs = require('fs');
var dire = fs.readdirSync('./TurtlePics');
function newTurtlePic() {
var index = Math.floor(Math.random()*dire.length);
return dire[index];
}
function checkRequest(mess) {
sendIndex = mess.indexOf("send")
turtleIndex = mess.indexOf("turtle")
var theNum = mess.replace( /^\D+/g, '');
var nTurtles = parseInt(theNum, 10);
if (nTurtles > 1000) {
nTurtles = 1000
}
return nTurtles;
}
client.on("ready", () => {
console.log("turtle");
});
client.on('disconnect', function(erMsg, code) {
console.log('---- Bot disconnected from Discord with code', code, 'for reason:', erMsg, '----');
client.connect();
});
client.on('message', (message) => {
// don't reply to other bots
if (message.author.bot) return;
// send turtle pictures
if (message.content.toLowerCase().startsWith("/turtle")) {
message.channel.send({ files: ['./TurtlePics/' + newTurtlePic()]});
return;
}
// send a number of requsted turtle emojis
turtleIndex = message.content.toLowerCase().indexOf("turtle");
sendIndex = message.content.toLowerCase().indexOf("send");
if ((turtleIndex > sendIndex) && (turtleIndex != -1) && (sendIndex != -1)) {
nTurtles = checkRequest(message.content.toLowerCase());
// calculate how many turtles are needed beyond potentially maxed-out messages
var remainder = nTurtles % 250;
// each message can have at max 250 turtle emoji.
// Calculate how many n of these maxed messages are needed
nMax = Math.floor(nTurtles/250);
// send nMax 250-turtle messages
if (nMax != 0) {
emote250 = "🐢".repeat(250);
for (nMax; nMax > 0; nMax--) {
message.channel.send(emote250);
}
}
// send remainder of requested turtles
if (remainder != 0) {
message.channel.send("🐢".repeat(r));
}
return;
}
// randomly react to messages with turtle emoji
var num = Math.random();
if (num <= 0.01) {
message.react("🐢"); return;
}
});
client.login(config.token);