-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingbot.js
114 lines (108 loc) · 3.25 KB
/
pingbot.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
const Discord = require('discord.js');
const client = new Discord.Client();
const token = "some token";
let intervalIds = new Map();
client.on('ready', () => {
client.user.setStatus('!pingme');
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
let tokens = msg.content.split(' ');
if(tokens[0]=="!stop" || tokens[0]=="!abort") {
let id = intervalIds.get(msg.author);
if(id==undefined) {
msg.reply("I am litteraly not even pinging you what do you want me to do");
return;
}
clearInterval(id);
msg.reply("Stopped.");
intervalIds.set(msg.author, undefined);
return;
}
if (tokens[0] === '!pingme') {
if(tokens.length<3) {
msg.reply("This command requires the following arguments: !pingme <interval> <dm|here>");
return;
}
let stringTime = timeString(tokens[1]);
if(stringTime!=null) {
if(parseInt(tokens[1].match(/\d/g).join(''))<2) {
if(parseInt(tokens[1].match(/\d/g).join(''))==0) {
msg.reply("That would really break it..");
return;
}
msg.reply("Note: The pings may not be every second because of the chat cooldown.");
}
if(tokens[2]=="here")
msg.reply("Pinging you here every "+stringTime);
else
msg.reply("Pinging you in dms every "+stringTime);
let time = timeMilliseconds(tokens[1]);
if(time!=null) {
if(ping(msg.author)==null) {
msg.reply("Why did you block me :(((");
}
intervalIds.set(msg.author, setInterval(ping, time, msg.author, tokens[2], msg.channel));
}
} else {
msg.reply("Invalid time unit. Use 1s, 1m, 1h, 1d");
}
}
});
function ping(sender, channelSelected, channel) {
if(channelSelected!="here") {
sender.send(`${sender.toString()}`).catch(err => {
clearInterval(intervalIds.get(sender));
return null;
});
} else {
channel.send(`${sender.toString()}`).catch(err => {
clearInterval(intervalIds.get(sender));
return null;
});
}
return 0;
}
function timeMilliseconds(string) {
const digits = string.match(/\d/g);
if(string.match(/s/g)[0]) {
return parseInt(digits.join(''))*1000;
} else if(string.match(/m/g)[0]) {
return parseInt(digits.join(''))*60000;
} else if(string.match(/h/g)[0]) {
return parseInt(digits.join(''))*60000*60;
} else if(string.match(/d/g)[0]) {
return parseInt(digits.join(''))*60000*60*24;
}
return null;
}
function timeString(string) {
const regx = /\D/g;
const match = string.match(regx);
const digits = string.match(/\d/g);
if(match==null || match.length>1) {
return null;
}
if(match[0]=="s") {
if(digits.join('')=="1")
return 'second';
else
return digits.join('')+" seconds";
} else if(match[0]=="m") {
if(digits.join('')=="1")
return 'minute';
else
return digits.join('')+" minutes";
} else if(match[0]=="h") {
if(digits.join('')=="1")
return 'hour';
else
return digits.join('')+" hours";
} else if(match[0]=="d") {
if(digits.join('')=="1")
return 'day';
else
return digits.join('')+" days";
}
}
client.login(token);