-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
256 lines (214 loc) · 8.13 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
/**
* NodeJS Twitch Permit-Bot via tmi.js
*
* @author Jonas Berner <[email protected]>
* @version 1.0.5
* @copyright 04.02.2021 Jonas Berner
*/
console.log(`EchtkPvL Twitch Permit-Bot (NodeJS ${process.version})`);
let tmi, client;
var config;
var timer_obj = [];
var permit_obj = [];
let { Resolver } = require('dns').promises;
let dns = new Resolver({'timeout': 750});
dns.setServers(['1.1.1.1', '8.8.8.8']);
try {
tmi = require('tmi.js');
config = require("./config");
} catch(error) {
console.log("Requirements missing! Try: npm install");
console.log(error);
process.exit(1);
}
// ---------------------------
// Setup
// ---------------------------
client = new tmi.Client({
options: {
debug: false
},
connection: {
reconnect: true,
secure: true
},
identity: {
username: config.username,
password: config.token
},
channels: config.channels
});
client.connect();
client.on('connected', (address, port) => {
console.log(`Connected: ${address}:${port}`);
client.color('Red');
});
client.on('join', (channel, username, self) => {
if(self) console.log(`${username} joined ${channel}`);
});
client.on('notice', (channel, msgid, message) => {
console.log(`notice: [${channel}] ${msgid} => ${message}`);
});
process.on('SIGINT', function() {
client.disconnect();
console.log("SIGINT");
process.exit();
});
// ---------------------------
// chat, action or whisper
// ---------------------------
client.on('message', (channel, userstate, message, self) => {
if(self) return;
if(userstate["message-type"] == "whisper") return;
check_link(channel, userstate, message, self); // Link-Protection
commands(channel, userstate, message, self);
checkCaptials(channel, userstate, message, self);
});
// ---------------------------
// Functions
// ---------------------------
function commands(channel, userstate, message, self) {
if(!message.startsWith('!') && !message.startsWith('#')) return;
const args = message.slice(1).split(' ');
const command = args.shift().toLowerCase();
const user = userstate['display-name'];
if(
(!isNaN(timer_obj[user + command]) && new Date().getTime() < timer_obj[user + command])
|| (!isNaN(timer_obj[command]) && new Date().getTime() < timer_obj[command])
|| (!isNaN(timer_obj[user]) && new Date().getTime() < timer_obj[user])
){
client.say(channel, `@${user} you are on cooldown!`);
return;
}
switch(command){
case "help":
case "commands":
timer_obj[command] = new Date().getTime() + (15 * 1000);
client.say(channel, `Commands: !echo !debug !uhr !social`);
break;
case "permit":
cmd_permit(channel, userstate, message, self, args, command);
break;
case "false":
timer_obj[user + command] = new Date().getTime() + (15 * 1000);
client.say(channel, `It's funny because it's true :D @${user}`);
break;
case "echo":
timer_obj[user + command] = new Date().getTime() + (15 * 1000);
client.say(channel, `@${user}, you said: "${args.join(' ')}"`);
break;
case "debug":
timer_obj[user + command] = new Date().getTime() + (60 * 1000);
client.say(channel, JSON.stringify(userstate));
break;
case "sounds":
timer_obj[user + command] = new Date().getTime() + (60 * 1000);
client.say(channel, "https://jvpeek.de/ext/sb/soundlist/?channel=echtkpvl");
break;
case "uhr":
case "uhrzeit":
uhrzeit = new Date().toISOString().replace(/\d{4}-\d{2}-\d{2}T/, '').replace(/\..+/, '');
timer_obj[user + command] = new Date().getTime() + (10 * 1000);
client.say(channel, `Hi ${user}, hier die Uhrzeit: ${uhrzeit}`);
break;
case "hp":
case "social":
case "socials":
case "website":
case "webseite":
case "homepage":
if(!isNaN(timer_obj[user + "social"]) && new Date().getTime() < timer_obj[user + "social"]){
client.say(channel, `@${user} you are on cooldown!`);
return;
}
timer_obj[user + "social"] = new Date().getTime() + (30 * 1000);
client.action(channel, `Homepage: https://echtkpvl.de - Twitter: https://twitter.com/EchtkPvL - GitHub: https://github.com/EchtkPvL - Insta: https://www.instagram.com/echtkpvl`);
break;
default:
return false;
}
console.log(`[${channel}][CMD] ${user}: ${message}`);
}
function cmd_permit(channel, userstate, message, self, args, command) {
var authorized = false;
var user = args[0].toLowerCase().replace('@', '');
if(userstate.badges !== null){
if(userstate.badges.moderator) authorized = true; // Mod
if(userstate.badges.broadcaster) authorized = true; // Streamer
if(userstate.badges.admin) authorized = true; // Twitch-Admin
if(userstate.badges.global_mod) authorized = true; // Global Mod
if(userstate.badges.staff) authorized = true; // Twitch-Staff
}
if(!authorized) return false;
var delay = 60;
if(args.length >= 2 && !isNaN(args[1]) && args[1] <= 3600) delay = args[1];
permit_obj[user] = new Date().getTime() + (delay * 1000);
client.say(channel, `@${user} you are now permited to post links for ${delay} seconds`);
}
function checkCaptials(channel, userstate, message, self) {
var count = message.replace(/\s/g, '').length;
var countRaw = message.replace(/\s/g, '').length;
var countCapitals = 0;
for (const key in userstate['emotes']){
for (const value in userstate['emotes'][key]){
emote = userstate['emotes'][key][value].split('-');
start = parseInt(emote[0]);
end = parseInt(emote[1]);
tmp = message.substr(start, (end - start) + 1);
countRaw -= tmp.replace(/\s/g, '').length;
countCapitals -= tmp.length - tmp.replace(/[A-Z]/g, '').length;
}
}
countCapitals += count - message.replace(/\s/g, '').replace(/[A-Z]/g, '').length;
if((countCapitals / countRaw) >= 0.9 && countRaw >= 10){
client.deletemessage(channel, userstate.id);
client.action(channel, `@${userstate['display-name']} stop spamming caps!`);
}
}
setInterval(() => {
for (const property in permit_obj)
if(new Date().getTime() >= permit_obj[property])
delete permit_obj[property];
for (const property in timer_obj)
if(new Date().getTime() >= timer_obj[property])
delete timer_obj[property];
}, 30 * 60 * 1000);
async function check_link(channel, userstate, message, self) {
var permit = false;
var user = userstate.username;
if(userstate.badges !== null){
if(userstate.badges.moderator) permit = true; // Mod
if(userstate.badges.subscriber) permit = true; // Sub
if(userstate.badges.broadcaster) permit = true; // Streamer
if(userstate.badges.admin) permit = true; // Twitch-Admin
if(userstate.badges.global_mod) permit = true; // Global Mod
if(userstate.badges.staff) permit = true; // Twitch-Staff
}
var RegEx = /([\d\w\- ]+\.)*([\d\w\- ]+\.[ ]*[a-z]{2,})/;
var match = RegEx.exec(message.toLowerCase());
if (match === null) return;
console.log(`[${channel}] Link detected: ${message}`);
var plain_link = match[2].replace(/\s/g, '');
// Whitelist
switch(plain_link){
case "twitch.tv":
case "echtkpvl.de":
case "github.com":
console.log(`[${channel}] Link in Whitelist: ${plain_link}`);
return;
}
try {
await dns.resolveNs(plain_link);
} catch(error) {
permit = true;
console.log(`[${channel}] Resolve-Error: ${error.code} (${plain_link})`);
}
if(permit) return;
if(!isNaN(permit_obj[user]) && new Date().getTime() <= permit_obj[user]){
return;
} else {
client.deletemessage(channel, userstate.id);
client.action(channel, `@${user} you are not permited to post links!`);
}
}
// EOF