-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
317 lines (266 loc) · 9.57 KB
/
bot.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
require("dotenv").config();
/* Internal API imports */
const express = require("express");
const bodyParser = require("body-parser");
const notFound = require("./api/errors/notFound");
const errorHandler = require("./api/errors/errorHandler");
const verifyInternalToken = require("./api/common/verifyAuthToken");
/* App instantiation */
const app = express();
// API routers
const twitchRouter = require("./api/twitch/twitch.router");
/* Class Imports */
const fs = require("fs");
const Discord = require("discord.js");
const Logger = require("./lib/logger");
/* Get defined log level and instantiate a new logger */
const { LOG_LEVEL } = process.env;
const logger = new Logger(LOG_LEVEL);
/* Internal/Callback URL based on dev/production environment */
const environment = process.env.NODE_ENV || "development";
const BASE_URL =
environment === "development"
? process.env.DEV_BASE_URL
: process.env.PROD_BASE_URL;
/* Other general imports */
const { prefix } = require("./resources/config.json");
const { BOT_TOKEN, PORT = 3000, INTERNAL_ACCESS_TOKEN } = process.env;
const { default: axios } = require("axios");
/* Initialize discord client */
const client = new Discord.Client();
/* Initialize valid commands and cooldowns for client */
client.commands = new Discord.Collection();
client.cooldowns = new Discord.Collection();
/* Collect list of all command folders/categories */
const commandFolders = fs.readdirSync("./commands");
/* Collect each command from every folder */
commandFolders.forEach((folder) => {
const commandFiles = fs
.readdirSync(`./commands/${folder}`)
.filter((file) => file.endsWith(".js"));
/* Map each command to a list for use */
commandFiles.forEach((file) => {
const command = require(`./commands/${folder}/${file}`);
client.commands.set(command.name, command);
});
});
/**
* Bot startup
* Includes:
* - Server listener for event subs and API endpoints
* - Locates announcements channel
*/
client.on("ready", () => {
console.log("Ready to work!");
const listener = app.listen(PORT, () => {
console.log(`Listening on Port ${PORT}!`);
});
});
/**
* Chat message listener
* Handles commands and general
* logic regarding them
*/
client.on("message", (message) => {
/* Ignore messages without the recognized prefix */
if (!message.content.startsWith(prefix) || message.author.bot) return;
/* Retrieve command + any user arguments from sent message */
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
/* Check if the command exists/is valid */
if (!client.commands.has(commandName)) return;
/* Retrieve the requested command function from our commands list */
const command = client.commands.get(commandName);
/* Get "commands on cooldown" list from the client */
const { cooldowns } = client;
/* Set command on cooldown (if it has a universal use cooldown) */
if (!cooldowns.has(command.name))
cooldowns.set(command.name, new Discord.Collection());
/* Establish cooldown duration and expiration for current execution */
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000; // convert to seconds from ms
/* Check if the user is currently on "command cooldown" */
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(
`please wait ${timeLeft.toFixed(
1
)} more second(s) before reusing the \`${command.name}\` command.`
);
}
}
/* Command will execute - set user on a short timeout to prevent command spam */
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
/* Execute the command - throw an error if anything goes wrong */
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply(`There was an error trying to execute ${commandName}`);
}
});
/* Login bot using token */
client.login(BOT_TOKEN);
/**
* API INSTANTIATION - not how I would normally have formatted this
* =============================================================================
* API routes and declarations were originally in their own file, however there
* were issues utilizing the discord 'client' object outside of this file,
* and so to avoid those issues and any circular dependencies, the highest
* level of the API is in this file.
*/
/* Body parser to utilize raw body bytes for Twitch signature verification */
app.use(
bodyParser.json({
verify: (req, res, buf) => {
req.rawBody = buf;
},
})
);
/* Baseline JSON parsing */
app.use(express.json());
/* Route handler for Twitch EventSub */
app.use("/twitch", twitchRouter);
/* Handle internal POST request to send a discord notification */
app.post("/discord/twitch", verifyInternalToken, (request, response) => {
logger.info({
action: "Send payload to Discord for embed/notification",
location: __dirname,
status: 202,
});
/* Let requester know of success */
response.sendStatus(202);
/* Destructure "event" object from payload */
const { event } = request.body;
logger.trace({
action: "Log Event Object",
location: "API route /discord/twitch",
notes: [event],
});
/* Proceed to embed message function */
twitchLive(event);
});
/* API error handlers */
app.use(notFound);
app.use(errorHandler);
/**
* Takes in useful payload information
* from Twitch live notification (EventSub)
* Processes information into a useful, concise embed
* for Discord
* @param {Object} event
*/
async function twitchLive(event) {
try {
/* Twitch's "lilac" color */
const embedColor = "#B9A3E3";
/* Get useful event information from payload data */
const {
broadcaster_user_id,
broadcaster_user_login,
broadcaster_user_name,
started_at,
} = event;
/* Embed author name */
const embedAuthor = "TheChosenWaffle's Discord Bot";
/* Create stream title from user name */
const streamTitle = `${broadcaster_user_name} is live NOW on Twitch!`;
/* Create stream URL from user login */
const streamURL = `https://www.twitch.tv/${broadcaster_user_login}`;
/* Format day and time information for stream start */
const liveDate = new Date(started_at);
const streamDay = liveDate.toDateString();
const streamTime = liveDate.toTimeString();
/* Twitch logo URL */
const twitchLogo = "https://i.imgur.com/esBdzQP.png";
/* Create the embed message for the announcement channel */
const twitchEmbed = new Discord.MessageEmbed()
.setColor(embedColor)
.setTitle(streamTitle)
.setURL(streamURL)
.setAuthor(embedAuthor)
.setThumbnail(twitchLogo)
.addFields(
{ name: "\u200B", value: "\u200B" },
{ name: "Stream Date:", value: streamDay },
{ name: "Stream Start Time:", value: streamTime }
);
logger.trace({
action: "Log Discord Embed Object",
location: `'twitchLive' in ${__dirname}`,
notes: [`${twitchEmbed}`],
});
/* Internal URL to GET all channel IDs for related broadcaster */
const GET_URL = `${BASE_URL}twitch/announcements?broadcaster_id=${broadcaster_user_id}`;
/* Set headers with auth token */
const headers = {
Authorization: `Bearer ${INTERNAL_ACCESS_TOKEN}`,
};
/* Retrieve all channel IDs */
await axios
.get(await GET_URL, { headers })
.then((response) => {
logger.info({
action: "Get All Channel IDs Success",
location: `'twitchLive' in ${__dirname}`,
status: response.status,
notes: [JSON.stringify(response.data, null, 2)],
});
/* Format response to account for Axios "data" nesting */
return response.data.data;
})
.then((channel_IDs) => {
/* Cycle through channel IDs and send notification */
channel_IDs.forEach(({ channel_id }) => {
/**
* TODO: handle what happens when a discord bot is no longer part
* of a server, DEL request for DB. Test with heroku, to see if this
* is even an issue yet
*/
/* Send the embed message to the announcements channel by channel ID */
client.channels
.fetch(`${channel_id}`)
.then((channel) => channel.send("@everyone", twitchEmbed))
.catch((error) => {
logger.error({
action: "Fetch Channel ID",
location: `'twitchLive' in ${__dirname}`,
notes: [`Error: ${error}`],
});
});
});
})
.then(() => {
/* Success! */
logger.info({
action: "Send a Discord notification for a Twitch notification",
location: `'twitchLive' in ${__dirname}`,
notes: [
`Payload data: ${JSON.stringify(event, null, 2)}`,
`All messages sent!`,
],
});
})
.catch((error) => {
logger.error({
action: "Get All Channel IDs Failure",
location: `'twitchLive' in ${__dirname}`,
status: error.status,
notes: [`Error: ${error.message}`],
});
});
} catch (error) {
logger.error({
action: "Send a Discord notification for a Twitch notificaiton FAILURE",
location: `'twitchLive' in ${__dirname}`,
notes: [
`Payload data: ${JSON.stringify(event, null, 2)}`,
`Error Message: ${error}`,
],
});
}
}