-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
172 lines (144 loc) Β· 4.42 KB
/
server.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
require("dotenv").config();
const express = require("express");
const app = express();
const doesTheDogDie = require("./src/does-the-dog-die");
const { getCurrentCategoryByUsername } = require("./src/twitch");
const { twitchBot, handleBotClientMessage } = require("./src/twitch-bot");
const { getUsers, getUser, deleteUser, createUser } = require("./src/db");
const { TWITCH_BOT_USERNAME, TWITCH_IGNORED_CATEGORIES_JSON } = process.env;
const TWITCH_IGNORED_CATEGORIES = JSON.parse(TWITCH_IGNORED_CATEGORIES_JSON);
console.log(
`[Content Warning Bot] Ignored categories: ${TWITCH_IGNORED_CATEGORIES.join(
", "
)}`
);
twitchBot.on("connected", () => {
const currentChannels = twitchBot
.getChannels()
.map(channelWithHash => channelWithHash.substring(1));
const users = getUsers();
users.forEach(user => {
if (currentChannels.includes(user.username)) {
return;
}
twitchBot.join(user.username);
});
});
async function handleMessagesSentInContentWarningBotChannel(
channel,
message,
username
) {
console.log({ message, username });
const existingUser = getUser(username);
console.log({ existingUser });
if (message.includes("!start") || message.includes("!join")) {
if (existingUser) {
twitchBot.say(
channel,
`@${username}, you have already started using the bot. Make sure to mod me with: /mod ContentWarningBot...`
);
return;
}
try {
await twitchBot.join(username);
createUser(username);
twitchBot.say(
channel,
`@${username}, you have started using the bot. Make sure to mod me with: /mod ContentWarningBot. Try !cw or !contentwarning in your channel's chat.`
);
} catch (e) {
console.error(e);
twitchBot.say(
channel,
`@${username}, something went wrong. Please try again later...`
);
}
return;
}
if (message.includes("!stop") || message.includes("!leave")) {
if (!existingUser) {
twitchBot.say(channel, `@${username}, you have stopped using the bot.`);
return;
}
try {
await twitchBot.part(username);
deleteUser(username);
twitchBot.say(channel, `@${username}, you have stopped using the bot.`);
} catch (e) {
console.error(e);
twitchBot.say(
channel,
`@${username}, something went wrong. Please try again later...`
);
}
return;
}
}
async function handleContentWarningMessage(channel) {
const categoryName = await getCurrentCategoryByUsername(channel);
console.log("categoryName", categoryName);
if (!categoryName || categoryName.length === 0) {
twitchBot.say(
channel,
`No content warnings for streams with no category/game`
);
return;
}
if (TWITCH_IGNORED_CATEGORIES.includes(categoryName)) {
twitchBot.say(channel, `No content warnings for β${categoryName}β`);
return;
}
try {
const { contentWarnings, url } = await doesTheDogDie({
gameTitle: categoryName,
isSensitiveEnabled: true
});
if (contentWarnings && contentWarnings.length === 0) {
twitchBot.say(
channel,
`Didn't find any crowdsourced content warnings for β${categoryName}β. See more at ${url}`
);
return;
}
const contentWarningsString = contentWarnings.join(", ");
twitchBot.say(
channel,
`Content warnings for β${categoryName}β: ${contentWarningsString}. See more at ${url}`
);
} catch (e) {
twitchBot.say(
channel,
`Couldn't find any content warnings for β${categoryName}β on doesthedogdie.com...`
);
}
}
twitchBot.on("message", async (channelWithHash, data, message) => {
const channel = channelWithHash.substring(1);
const { username } = data;
// if it's the bot saying things don't do anything
if (username.toLowerCase() === TWITCH_BOT_USERNAME.toLowerCase()) {
return;
}
if (channel.toLowerCase() === TWITCH_BOT_USERNAME.toLowerCase()) {
await handleMessagesSentInContentWarningBotChannel(
channel,
message,
username
);
return;
}
if (message.includes("!cw") || message.includes("!contentwarning")) {
await handleContentWarningMessage(channel);
return;
}
});
app.get("/", (request, response) => {
response.send("hello");
});
app.get("/restart", () => {
process.exit();
});
const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port);
});