From b1571a5e60df289a96b8144e1a7ab2b171f59d88 Mon Sep 17 00:00:00 2001 From: Wissididom <30803034+Wissididom@users.noreply.github.com> Date: Sat, 3 Aug 2024 20:40:58 +0200 Subject: [PATCH] Generate our own embed for each webhook message (#42) --- index.js | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 158497f..c8382d4 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -import { WebhookClient } from "discord.js"; +import { WebhookClient, EmbedBuilder } from "discord.js"; import "dotenv/config"; const token = await fetch( @@ -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) { @@ -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, }); @@ -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 + ? `` + : "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); }