Skip to content

Commit

Permalink
Register a discord command to get yt stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Elanis committed May 10, 2022
1 parent ee50982 commit ba97019
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions YoutubeInternal.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export default class YoutubeInternal {
return finalResult;
}

static formatDuration(totalDuration) {
return totalDuration.hours.toString().padStart(2, '0') + ':' + totalDuration.minutes.toString().padStart(2, '0') + ':' + totalDuration.seconds.toString().padStart(2, '0');
}

static async processPlaylistForModule(PLAYLIST_ID, API_KEY) {
let videoCount = 0;
let totalDuration = { hours: 0, minutes: 0, seconds: 0 };
Expand Down Expand Up @@ -102,8 +106,8 @@ export default class YoutubeInternal {

return acc;
}, { hours: 0, minutes: 0, seconds: 0 });
console.log('Total duration: ', totalDuration.hours.toString().padStart(2, '0') + ':' + totalDuration.minutes.toString().padStart(2, '0') + ':' + totalDuration.seconds.toString().padStart(2, '0'));
console.log('Total duration: ', YoutubeInternal.formatDuration(totalDuration));

return { totalDuration, videoCount };
}
}
}
15 changes: 15 additions & 0 deletions main.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ export default class Youtube {
Youtube.update();

Youtube.interval = setInterval(Youtube.update, 5 * 60 * 1000); // Update every 5 minutes

Discord.registerCmd('ytstats', Youtube.showStatusOnDiscord);
}

static async showStatusOnDiscord(discordClient, message, words) {
const content = await YoutubeInternal.processPlaylistForModule(config.PLAYLIST_ID, config.API_KEY);

if(!content) { return; }

const {
totalDuration,
videoCount
} = content;

Discord.sendMessage(`Youtube status: ${videoCount} videos, ${YoutubeInternal.formatDuration(totalDuration)}`);
}

static close() {
Expand Down

0 comments on commit ba97019

Please sign in to comment.