Skip to content

Commit

Permalink
Twitch: auto post replays
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPresso committed Jan 21, 2024
1 parent 1d3bd83 commit 354a955
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"twitch": {
"message": "%userName% is live!",
"channel": "YourChannel",
"discordChannel": "123456789",
"userId": "YourUserID",
"liveDiscordChannel": "123456789",
"replayDiscordChannel": "123456789",
"clientId": "",
"clientSecret": ""
},
Expand Down
18 changes: 17 additions & 1 deletion scheduled/twitchLive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions utils/TwitchAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 354a955

Please sign in to comment.