Skip to content

Commit

Permalink
Add auto clips posting
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPresso committed Jan 21, 2024
1 parent 354a955 commit e541072
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
9 changes: 6 additions & 3 deletions classes/Judy.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ module.exports = class Judy {
tempChannelsCounters: new Map(),
twitch: {
prevState: null,
scheduledEvent: null
scheduledEvent: null,
lastExecTime: null
}
}

Expand Down Expand Up @@ -70,9 +71,11 @@ module.exports = class Judy {
const scheduledTask = require(`../scheduled/${file}`),
name = file.split('.')[0];

schedule.scheduleJob(scheduledTask.schedule, () => {
schedule.scheduleJob(scheduledTask.schedule, async () => {
try {
scheduledTask.task(this._client);
this._client.logger.debug(`Running ${name} scheduled task.`)
await scheduledTask.task(this._client);
this._client.logger.debug(`Done running ${name} scheduled task.`)
} catch (e) {
console.error(e);
}
Expand Down
2 changes: 2 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
}
},
"twitch": {
"enable": false,
"message": "%userName% is live!",
"channel": "YourChannel",
"userId": "YourUserID",
"liveDiscordChannel": "123456789",
"replayDiscordChannel": "123456789",
"clipDiscordChannel": "123456789",
"clientId": "",
"clientSecret": ""
},
Expand Down
22 changes: 21 additions & 1 deletion scheduled/twitchLive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
schedule: "* * * * *",
task: async client => {
const config = client.config.twitch || {};
if(!config.channel)
if(!config.channel || !config.enable)
return;

const token = await Twitch.getToken(config.clientId, config.clientSecret)
Expand All @@ -17,6 +17,8 @@ module.exports = {
if(!response)
return;

postClips(client, token.data.access_token);

const newState = response.data.data[0];
const oldState = client._state.twitch.prevState;

Expand Down Expand Up @@ -103,6 +105,24 @@ async function postReplay(client, twitchToken) {
client.channels.resolve(client.config.twitch.replayDiscordChannel)?.send(videos.data.data[0].url)
}

async function postClips(client, token) {
const clips = await Twitch.getClips(
client.config.twitch.userId,
client._state.twitch.lastExecTime || new Date().toISOString(),
client.config.twitch.clientId,
token
);

if(!clips || !clips.data)
return;

for(const clip of (clips.data.data || [])) {
client.channels.resolve(client.config.twitch.clipDiscordChannel)?.send(clip.url);
}

client._state.twitch.lastExecTime = new Date().toISOString();
}

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 @@ -34,5 +34,17 @@ module.exports = {
}
}
).catch(console.error)
},

getClips: (userId, startedAt, clientId, token) => {
return Axios.get(
`https://api.twitch.tv/helix/clips?broadcaster_id=${userId}&started_at=${startedAt}`,
{
headers: {
"Client-ID": clientId,
"Authorization": `Bearer ${token}`
}
}
).catch(console.error)
}
}

0 comments on commit e541072

Please sign in to comment.