-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
190 lines (184 loc) · 6.1 KB
/
main.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
const fs = require("fs");
const Commands = require("./src/commands/Commands.js");
const Config = require("./src/Config.js");
const DataStore = require("./lib/DataStore.js");
const Discord = require("discord.js");
const RemindersDataStore = require("./src/RemindersDataStore.js");
const TwitchAlertsDataStore = require("./src/TwitchAlertsDataStore.js");
const TwitchAPI = require("./lib/TwitchAPI.js");
const TwitchEventSubHandlers = require("./src/TwitchEventSubHandlers.js");
const TwitchEventSubServer = require("./lib/TwitchEventSubServer.js");
async function onMessage(client, message) {
// Log all messages sent by bot
if (message.author.id === client.user.id) {
console.log(`Sent message: "${message.content}"`);
return;
}
let _mention = null;
let command = null;
let args = null;
const messageParts = message.content.split(" ").filter((part) => part !== "");
switch (message.channel.type) {
case "dm":
if (message.mentions.has(client.user)) {
[_mentions, command, ...args] = messageParts;
} else {
[command, ...args] = messageParts;
}
console.log(
`Direct messaged by ${message.author.username}: "${message.content}"`,
);
break;
case "text":
// Only respond to message where the bot is mentioned explicitly
if (!message.mentions.has(client.user) || message.mentions.everyone) {
return;
}
[_mention, command, ...args] = messageParts;
console.log(
`Mentioned by ${message.author.username} in channel ${message.channel.id}: "${message.content}"`,
);
break;
default:
console.log(
`Received message from ${message.author.username} in unknown channel type "${message.channel.type}": "${message.content}"`,
);
}
switch ((command || "").toLowerCase()) {
case "reminders":
await Commands.reminders.viewReminders(message);
return;
case "remind":
await Commands.reminders.addReminder(message, args.join(" "));
return;
case "forget":
await Commands.reminders.removeReminders(message, args);
return;
case "quote":
await Commands.quote.randomQuote(message);
return;
case "who":
await Commands.quote.randomQuoteAuthor(message);
return;
case "subs":
await Commands.twitch.listSubscriptions(message);
return;
case "sub":
await Commands.twitch.subscribe(message, args);
return;
case "unsub":
await Commands.twitch.unsubscribe(message, args);
return;
case "view_live_symbol":
await Commands.twitch.viewLiveSymbol(message);
return;
case "set_live_symbol":
await Commands.twitch.setLiveSymbol(message, args[0]);
return;
case "clear_live_symbol":
await Commands.twitch.clearLiveSymbol(message);
return;
default:
await message.channel.send(
"Valid commands are " +
"`reminders`, " +
"`remind <user/role> to <do something> in <time>`, " +
"`forget <reminder #>`, " +
"`quote`, " +
"`who`, " +
"`subs`, " +
"`sub <username>`, " +
"`unsub <username>`, " +
"`view_live_symbol`, " +
"`set_live_symbol <symbol>`, and " +
"`clear_live_symbol`.",
);
return;
}
}
async function init() {
const {
DISCORD_TOKEN,
DISCORD_MESSAGE_CACHE_MAX_SIZE,
DISCORD_MESSAGE_CACHE_LIFETIME_SECONDS,
DISCORD_MESSAGE_SWEEP_INTERVAL_SECONDS,
REMINDERS_CHECK_INTERVAL_SECONDS,
} = Config.get();
const client = new Discord.Client({
messageCacheMaxSize: DISCORD_MESSAGE_CACHE_MAX_SIZE,
messageCacheLifetime: DISCORD_MESSAGE_CACHE_LIFETIME_SECONDS,
messageSweepInterval: DISCORD_MESSAGE_SWEEP_INTERVAL_SECONDS,
});
client.on("message", onMessage.bind(this, client));
await client.login(DISCORD_TOKEN);
console.log(`Logged in as ${client.user.tag}!`);
// Refresh Twitch EventSub subscriptions on startup
await TwitchEventSubServer.startServer(
TwitchEventSubHandlers.getEventHandler(client),
);
await refreshTwitchSubscriptions();
// Check for any reminders that need to be announced regularly
await announceReminders(client);
client.setInterval(
announceReminders.bind(this, client),
REMINDERS_CHECK_INTERVAL_SECONDS * 1000,
);
// Stop any typing from previous runs
await stopAllTyping(client);
}
async function stopAllTyping(client) {
await Promise.all(
client.channels.cache.array().map(async (channel) => {
if (channel.type === "text") {
await channel.stopTyping(/*force*/ true);
}
}),
);
}
async function refreshTwitchSubscriptions() {
await TwitchAPI.clearSubscriptions();
TwitchAlertsDataStore.getUsers().forEach(async (username) => {
const userInfo = await TwitchAPI.getUserInfo(username);
await TwitchAPI.createStreamChangeSubscription(userInfo.id);
});
}
async function announceReminders(discordClient) {
const channels = await Promise.all(
RemindersDataStore.getChannelsWithReminders().map(
async (channelID) => await discordClient.channels.fetch(channelID),
),
);
for (let channelIdx = 0; channelIdx < channels.length; channelIdx++) {
const channel = channels[channelIdx];
if (channel.type !== "dm" && channel.type !== "text") {
continue;
}
const reminders = RemindersDataStore.getRemindersForChannel(channel.id);
// Iterate in reverse so removing reminders does not mess up the indexes of the rest
for (
let reminderIdx = reminders.length - 1;
reminderIdx >= 0;
reminderIdx--
) {
const { content, target, timestamp } = reminders[reminderIdx];
if (timestamp <= Date.now()) {
let mention = null;
switch (target.type) {
case "role":
mention = `<@&${target.id}>`;
break;
case "user":
mention = `<@${target.id}>`;
break;
}
if (mention !== null) {
await channel.send(
`${mention}, this is your reminder to **${content}**.`,
);
}
RemindersDataStore.removeReminderForChannel(channel.id, reminderIdx);
}
}
}
}
init();