-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube.js
56 lines (49 loc) · 1.8 KB
/
youtube.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const Discord = require('discord.js')
const axios = require('axios')
const searchYouTube = require("youtube-api-v3-search");
function formatYouTubeCardEmbed(title, description, videoId, image){
return new Discord.MessageEmbed()
.setColor(process.env.EMBED_COLORS)
.setTitle(title)
.setURL(`https://www.youtube.com/watch?v=${videoId}`)
// .setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription(description)
.setImage(image)
.setTimestamp()
}
function pollingLatestVideos(client){
console.log('Polling YouTube Videos...')
// hack with hardcoded channel id - need an automated way to get it...
const channel = client.channels.cache.get(process.env.YOUTUBE_ANNOUNCEMENTS_ID);
const yt = client.emojis.cache.find(emoji => emoji.name === "yt");
const options = {
channelId: process.env.YOUTUBE_CHANNEL_ID,
part:'snippet',
type:'video',
maxResults: '10',
order: 'date'
}
searchYouTube(process.env.YOUTUBE_API_KEY, options)
.then(res => {
res.items.map(upload => {
let currentDateTime = (new Date()).toISOString()
let minutesAgoStarted = Math.floor(new Date(currentDateTime) - new Date(upload.snippet.publishTime)) / 60000
// if video was uploaded in the last 1hr...
if(minutesAgoStarted < 60){
channel.send(
`${yt} New DannyDanku YouTube video uploaded! \n https://www.youtube.com/watch?v=${upload.id.videoId}.`,
formatYouTubeCardEmbed(
upload.snippet.title,
upload.snippet.description,
upload.id.videoId,
upload.snippet.thumbnails.high.url
)
)
}
}
)
}
)
.catch(err => console.log(err))
}
module.exports = pollingLatestVideos