This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
forked from bartosjiri/twitch-lurker-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lurker.js
72 lines (58 loc) · 1.81 KB
/
lurker.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
const tmi = require("tmi.js");
const dateFormat = require("dateformat");
const config = require("./config");
const {username, token, channels} = config
if (!username) {
console.error("[ERROR] You need to provide a username!");
return;
}
if (!token) {
console.error("[ERROR] You need to provide a client token!");
return;
}
if (!channels || channels.length < 1) {
console.error("[ERROR] You need to provide at least one channel!");
return;
}
const user = username.toLowerCase();
const tmiOptions = {
connection: {
reconnect: true,
secure: true
},
identity: {
username: user,
password: token
},
channels: channels
}
const getCurrentTime = () => {
const d = new Date();
return `[${dateFormat(d, "yyyy-mm-dd HH:MM:ss")}]`;
}
const client = new tmi.client(tmiOptions);
client.connect();
client.on("logon", () => {
console.log(`${getCurrentTime()} Connecting to the Twitch server as user "${user}"...`);
});
client.on("join", (channel, username) => {
if (username == user) {
console.log(`${getCurrentTime()} Joined channel "${channel.substring(1)}".`);
}
});
client.on("subgift", (channel, username, _, recipient) => {
if (recipient.toLowerCase() == user) {
console.log(`${getCurrentTime()} Received a subscription gift from user "${username}" in channel "${channel.substring(1)}"!`);
}
});
client.on("reconnect", () => {
console.log(`${getCurrentTime()} Trying to reconnect to the Twitch server...`);
});
client.on("part", (channel, username) => {
if (username == user) {
console.log(`${getCurrentTime()} Disconnected from channel "${channel.substring(1)}".`);
}
});
client.on("disconnected", (reason) => {
console.log(`${getCurrentTime()} Disconnected from the Twitch server. Reason: "${reason}".`);
});