From 354a955b607135f9ccb102482798e7cd8b29a845 Mon Sep 17 00:00:00 2001 From: AlexPresso Date: Sun, 21 Jan 2024 13:45:29 +0100 Subject: [PATCH] Twitch: auto post replays --- config.example.json | 4 +++- scheduled/twitchLive.js | 18 +++++++++++++++++- utils/TwitchAPI.js | 12 ++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/config.example.json b/config.example.json index 8cdd233..c9f3f0a 100644 --- a/config.example.json +++ b/config.example.json @@ -24,7 +24,9 @@ "twitch": { "message": "%userName% is live!", "channel": "YourChannel", - "discordChannel": "123456789", + "userId": "YourUserID", + "liveDiscordChannel": "123456789", + "replayDiscordChannel": "123456789", "clientId": "", "clientSecret": "" }, diff --git a/scheduled/twitchLive.js b/scheduled/twitchLive.js index 359a787..9006b14 100644 --- a/scheduled/twitchLive.js +++ b/scheduled/twitchLive.js @@ -31,6 +31,7 @@ module.exports = { notifyLiveStart(client, newState); } else if(oldState && !newState) { stopLiveEvent(client, manager, previousEvent); + postReplay(client, token.data.access_token); } else { startOrEditLiveEvent(client, newState, manager, previousEvent); } @@ -51,7 +52,7 @@ async function notifyLiveStart(client, state) { .replace("{height}", 1080) ); - client.channels.resolve(client.config.twitch.discordChannel)?.send({content: "@everyone", embeds: [embed]}); + client.channels.resolve(client.config.twitch.liveDiscordChannel)?.send({content: "@everyone", embeds: [embed]}); } async function startOrEditLiveEvent(client, state, manager, previousEvent) { @@ -87,6 +88,21 @@ async function stopLiveEvent(client, manager, previousEvent) { client._state.twitch.prevState = null; } +async function postReplay(client, twitchToken) { + const videos = await Twitch.getVideos( + client.config.twitch.userId, + 'archive', + 'day', + client.config.twitch.clientId, + twitchToken + ); + + if(!videos || !videos.data) + return; + + client.channels.resolve(client.config.twitch.replayDiscordChannel)?.send(videos.data.data[0].url) +} + async function getOrFetchPreviousEvent(client, manager) { if(client._state.twitch.scheduledEvent) return client._state.twitch.scheduledEvent; diff --git a/utils/TwitchAPI.js b/utils/TwitchAPI.js index 8bebc80..9cd562a 100644 --- a/utils/TwitchAPI.js +++ b/utils/TwitchAPI.js @@ -22,5 +22,17 @@ module.exports = { getThumbnail: (url, width, height) => { return Axios.get(url.replace("{width}", width).replace("{height}", height), {responseType: "arraybuffer"}) + }, + + getVideos: (userId, type, period, clientId, token) => { + return Axios.get( + `https://api.twitch.tv/helix/videos?user_id=${userId}&type=${type}&period=${period}&sort=time`, + { + headers: { + "Client-ID": clientId, + "Authorization": `Bearer ${token}` + } + } + ).catch(console.error) } }