-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
66 lines (50 loc) · 1.65 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
const tmi = require('tmi.js');
const fs = require('fs');
const Axios = require('axios');
const config = require("./config.json");
// check if data.json exists
if (!fs.existsSync("./data.json"))
fs.writeFileSync("./data.json", JSON.stringify({}));
const client = new tmi.Client({
connection: {
secure: true,
reconnect: true
},
identity: {
username: config.username,
password: config.password
},
channels: [config.target_channel]
});
const onMessage = async (channel, tags, message, self) => {
let author = tags['display-name'];
let getUser = config.target_users
.filter(i => i.user.toLowerCase() == author.toLowerCase());
if (getUser.length != 1)
return;
getUser = getUser[0];
console.log(`${author} -> ${message}`);
let data = JSON.parse(fs.readFileSync("./data.json"));
fs.writeFileSync("./data.json", JSON.stringify({ ...data, [`lastSent_${getUser.user}`]: new Date() }));
console.log(`<${author}> ${message}`);
message = `${getUser.user_badge || ""} ${author}: ${message}`;
if (getUser.notice && (new Date() - new Date(data[`lastSent_${getUser.user}`])) > config.notify_after) {
message = getUser.notice_message + "\n" + message;
}
notifyServer(message);
}
const notifyServer = async (message) => {
return Axios({
method: "POST",
url: config.webhook_url,
headers: {
"Content-Type": "application/json"
},
data: {
content: message
}
});
}
client.connect();
client.on('message', onMessage);
console.log(`Bot started! Targeting for ${config.target_users.length} users`);