Skip to content

Commit

Permalink
Generate our own embed for each webhook message (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wissididom authored Aug 3, 2024
1 parent e62875c commit b1571a5
Showing 1 changed file with 109 additions and 1 deletion.
110 changes: 109 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WebhookClient } from "discord.js";
import { WebhookClient, EmbedBuilder } from "discord.js";
import "dotenv/config";

const token = await fetch(
Expand Down Expand Up @@ -76,14 +76,20 @@ const clips = (
console.log(JSON.stringify(clips, null, 2));
let creatorIds = [];
let videoIds = [];
let gameIds = [];
if (clips.length < 1) process.exit(); // No clips to post
for (let i = 0; i < clips.length; i++) {
creatorIds.push(clips[i].creator_id);
if (clips[i].video_id.length > 0) {
videoIds.push(clips[i].video_id);
}
if (clips[i].game_id.length > 0) {
gameIds.push(clips[i].game_id);
}
}
creatorIds = [...new Set(creatorIds)]; // Remove duplicate entries
videoIds = [...new Set(videoIds)]; // Remove duplicate entries
gameIds = [...new Set(gameIds)]; // Remove duplicate entries
let usersQuery;
let profileImageUrls = [];
if (creatorIds.length > 0 && creatorIds.length <= 100) {
Expand Down Expand Up @@ -126,6 +132,31 @@ if (videoIds.length > 0 && videoIds.length <= 100) {
} else if (videoIds.length > 100) {
console.error("More than 100 videos to look up");
}
let gamesQuery;
let gameNames = [];
if (gameIds.length > 0 && gameIds.length <= 100) {
gamesQuery = "?id=" + gameIds.join("&id=");
gameNames = (
await fetch(`https://api.twitch.tv/helix/games${gamesQuery}`, {
headers: {
"Client-ID": process.env.TWITCH_CLIENT_ID,
Authorization: `Bearer ${access_token}`,
},
})
.then((res) => res.json())
.catch((err) => console.error(err))
).data.map((x) => {
return {
id: x.id,
name: x.name,
boxart: x.box_art_url
.replace("{width}", "600")
.replace("{height}", "800"),
};
});
} else if (gameIds.length > 100) {
console.error("More than 100 games to look up");
}
const webhookClient = new WebhookClient({
url: process.env.DISCORD_WEBHOOK_URL,
});
Expand All @@ -145,6 +176,83 @@ for (let i = 0; i < clips.length; i++) {
avatarURL: profileImageUrls.find((x) => x.id == clips[i].creator_id)
?.profileImageUrl,
content,
embeds: [
new EmbedBuilder()
.setDescription(`[${clips[i].title.trim()}](${clips[i].url})`)
.addFields(
{
name: "Game",
value: clips[i].game_id
? gameNames.find((x) => x.id == clips[i].game_id)?.name
: "N/A",
inline: true,
},
{
name: "Streamer",
value: clips[i].broadcaster_name
? clips[i].broadcaster_name
: "N/A",
inline: true,
},
{
name: "Clipper",
value: clips[i].creator_name ? clips[i].creator_name : "N/A",
inline: true,
},
{
name: "VOD",
value: clips[i].video_id
? `[${clips[i].video_id}](https://www.twitch.tv/videos/${clips[i].video_id})`
: "N/A",
inline: true,
},
{
name: "Language",
value: clips[i].language ? clips[i].language : "N/A",
inline: true,
},
{
name: "Views",
value: clips[i].view_count
? clips[i].view_count.toString()
: "N/A",
inline: true,
},
{
name: "Created At",
value: clips[i].created_at
? `<t:${new Date(clips[i].created_at).getTime() / 1000}:F>`
: "N/A",
inline: true,
},
{
name: "Duration",
value: clips[i].duration ? `${clips[i].duration} seconds` : "N/A",
inline: true,
},
{
name: "VOD Offset",
value: clips[i].vod_offset
? `${clips[i].vod_offset} seconds`
: "N/A",
inline: true,
},
{
name: "Featured",
value:
clips[i].is_featured === false || clips[i].is_featured === true
? clips[i].is_featured.toString()
: "N/A",
inline: true,
},
)
.setThumbnail(
clips[i].game_id
? gameNames.find((x) => x.id == clips[i].game_id)?.boxart
: undefined,
)
.setImage(clips[i].thumbnail_url),
],
})
.catch((err) => console.error);
}

0 comments on commit b1571a5

Please sign in to comment.