-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (116 loc) · 3.94 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
import "dotenv/config";
import Database from "./database/sqlite.js";
import { handleDcfLogin, getChannelFollowers, getUser } from "./twitchApi.js";
const INCLUDE_FOLLOWS = process.env.INCLUDE_FOLLOWS.toLowerCase() == "true";
const INCLUDE_UNFOLLOWS = process.env.INCLUDE_UNFOLLOWS.toLowerCase() == "true";
const formatField = (label, value, link = null) => {
return `\n**${label}**: ${value ? (link ? `[${value}](<${link}>)` : value) : "``(not available)``"}`;
};
async function buildContent(follower, isFollow) {
let content = isFollow ? "**User Followed!**" : "**User Unfollowed!**";
content += formatField(
"Display-Name",
follower.user_name,
`https://www.twitch.tv/${follower.user_login}`,
);
content += formatField(
"User-Name",
follower.user_name,
`https://www.twitch.tv/${follower.user_login}`,
);
content += formatField(
"User-ID",
follower.user_id,
`https://www.twitch.tv/${follower.user_login}`,
);
if (follower.followed_at) {
let followedAt = Math.floor(
new Date(follower.followed_at).getTime() / 1000,
);
content += `\n**Followed At**: <t:${followedAt}:F> (<t:${followedAt}:R>)`;
} else {
content += "\n**Followed At**: ``(not available)``";
}
let user = follower.user_id ? await getUser(follower.user_id, "id") : null;
if (user && user.created_at) {
let createdAt = Math.floor(new Date(user.created_at).getTime() / 1000);
content += `\n**Created At**: <t:${createdAt}:F> (<t:${createdAt}:R>)`;
} else {
content += "\n**Created At**: ``(not available)``";
}
return content;
}
async function postToDiscord(content) {
const response = await fetch(`${process.env.DISCORD_WEBHOOK_URL}?wait=true`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
content,
allowed_mentions: { parse: [] }, // Do not allow any kind of pings
}),
});
if (!response.ok) {
console.error(
`${response.status} ${response.statusText}`,
await response.text(),
);
}
}
const syncFollowers = async () => {
setInterval(async () => {
// Run every 5 seconds
try {
const followers = await getChannelFollowers(
Database,
process.env.BROADCASTER_ID,
);
const newFollowerList = followers.followers;
const lastFollowerList = Database.getFollowers();
if (!lastFollowerList.length) {
// Don't need to compare lists if the old list doesn't exist yet
return Database.saveFollowerList(newFollowerList);
}
const lastFollowerSet = new Set(lastFollowerList.map((f) => f.user_id));
const addedFollowers = newFollowerList.filter(
(f) => !lastFollowerSet.has(f.user_id),
);
console.log("added:", addedFollowers);
const removedFollowers = lastFollowerList.filter(
(f) => !newFollowerList.some((nf) => nf.user_id === f.user_id),
);
console.log("removed:", removedFollowers);
for (const follower of addedFollowers) {
Database.saveFollower(
follower.user_id,
follower.user_name,
follower.user_login,
follower.followed_at,
);
if (INCLUDE_FOLLOWS)
await postToDiscord(await buildContent(follower, true));
}
for (const follower of removedFollowers) {
Database.deleteFollower(
follower.user_id,
follower.user_name,
follower.user_login,
follower.followed_at,
);
if (INCLUDE_UNFOLLOWS)
await postToDiscord(await buildContent(follower, false));
}
} catch (err) {
console.log("Error getting followers:", err);
}
}, 5000);
};
if ((await fetch(process.env.DISCORD_WEBHOOK_URL)).ok) {
await handleDcfLogin(Database, syncFollowers);
} else {
console.log(
`Webhook response wasn't between 200 and 299 inclusive! (Status: ${webhookGetResponse.status} - ${webhookGetResponse.statusText})`,
);
// Discord Webhook probably doesn't exist
}