forked from roydejong/timbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
live-embed.js
70 lines (60 loc) · 2.39 KB
/
live-embed.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
const { EmbedBuilder } = require('discord.js');
const moment = require('moment');
const humanizeDuration = require("humanize-duration");
const config = require('./config.json');
class LiveEmbed {
static createForStream(streamData) {
const isLive = streamData.type === "live";
const allowBoxArt = config.twitch_use_boxart;
let msgEmbed = new EmbedBuilder();
msgEmbed.setColor(isLive ? "#9146ff" : "#808080");
msgEmbed.setURL(`https://twitch.tv/${(streamData.login || streamData.user_name).toLowerCase()}`);
// Thumbnail
let thumbUrl = streamData.profile_image_url;
if (allowBoxArt && streamData.game && streamData.game.box_art_url) {
thumbUrl = streamData.game.box_art_url;
thumbUrl = thumbUrl.replace("{width}", "288");
thumbUrl = thumbUrl.replace("{height}", "384");
}
msgEmbed.setThumbnail(thumbUrl);
if (isLive) {
// Title
msgEmbed.setTitle(`:red_circle: **${streamData.user_name} is live on Twitch!**`);
msgEmbed.addFields({ name: "Title", value: streamData.title, inline: false });
} else {
msgEmbed.setTitle(`:white_circle: ${streamData.user_name} was live on Twitch.`);
msgEmbed.setDescription('The stream has now ended.');
msgEmbed.addFields({ name: "Title", value: streamData.title, inline: true });
}
// Add game
if (streamData.game) {
msgEmbed.addFields({ name: "Game", value: streamData.game.name, inline: false });
}
if (isLive) {
// Add status
msgEmbed.addFields({ name: "Status", value: isLive ? `Live with ${streamData.viewer_count} viewers` : 'Stream has ended', inline: true });
// Set main image (stream preview)
let imageUrl = streamData.thumbnail_url;
imageUrl = imageUrl.replace("{width}", "1280");
imageUrl = imageUrl.replace("{height}", "720");
let thumbnailBuster = (Date.now() / 1000).toFixed(0);
imageUrl += `?t=${thumbnailBuster}`;
msgEmbed.setImage(imageUrl);
// Add uptime
let now = moment();
let startedAt = moment(streamData.started_at);
msgEmbed.addFields({
name: "Uptime",
value: humanizeDuration(now - startedAt, {
delimiter: ", ",
largest: 2,
round: true,
units: ["y", "mo", "w", "d", "h", "m"]
}),
inline: true
});
}
return msgEmbed;
}
}
module.exports = LiveEmbed;