Skip to content

Instantly share code, notes, and snippets.

@binyamin
Last active February 21, 2019 01:59
Show Gist options
  • Select an option

  • Save binyamin/221bff976a1136adfad0d1eeeee65aea to your computer and use it in GitHub Desktop.

Select an option

Save binyamin/221bff976a1136adfad0d1eeeee65aea to your computer and use it in GitHub Desktop.
Youtube Video Checker Bot

Instructions:

  • requires npm install rss-parser
  • youtube channel id is prefilled
  • variable n in modules.js is the number of messages to check to tell if the video has been posted already,
  • guildname and channel name are Not prefilled
const Discord = require('discord.js');
...
const rss = require('./module.js');
// Check for new youtube videos from a channel
client.setInterval(() => {
let guild = client.guilds.find(g => g.id === <guild_id>); //Or, let guild = client.guilds.find(g => g.name === <guild_name>);
let channel = guild.channels.find(ch => ch.name === <channel_name>);
rss.updateFeed(channel);
}, 180000);
...
client.login(token);
const { RichEmbed } = require('discord.js');
const Parser = require('rss-parser');
let n = 3;
module.exports = {
updateFeed(channel) {
let rssParser = new Parser();
rssParser.parseURL('https://www.youtube.com/feeds/videos.xml?channel_id=UCVyRiMvfUNMA1UPlDPzG5Ow')
.then(feed => {
let last = feed.items[0];
const embed = new RichEmbed()
.setTitle(last.title)
.setAuthor(last.author)
.setURL(last.link)
.setImage(`https://img.youtube.com/vi/${last.id.replace('yt:video:', '')}/0.jpg`)
.setColor(0x00ad7d)
.setTimestamp(new Date(last.isoDate));
channel.fetchMessages({limit: n})
.then(msg => {
var msgArray = msg.array();
var id = '';
for (var i = 0; i < n; i++) {
if(msgArray[i].embeds[0]) {
id = msgArray[i].embeds[0].url.replace('https://www.youtube.com/watch?v=', '');
}
}
if(id !== last.id.replace('yt:video:', '')) {
channel.send(embed);
}
})
.catch(console.error)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment