Skip to content

Instantly share code, notes, and snippets.

@zekroTJA
Created December 7, 2017 15:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zekroTJA/0c80b34f6ede503deca32404800dded8 to your computer and use it in GitHub Desktop.
Save zekroTJA/0c80b34f6ede503deca32404800dded8 to your computer and use it in GitHub Desktop.
Little discord.js bot snipped for youtube subscriber goal message
const { Client, RichEmbed } = require('discord.js')
var afterLoad = require('after-load');
// <---[ SETTINGS ]--->
const token = ''
const channelid = 'zekrommaster110'
const goal = 1000
const end = '31.12.2017'
const timeout = 5 // minutes
// --------------------
const bot = new Client()
var message
var timer
bot.login(token)
function getTime() {
function btf(inp) {
if (inp < 10)
return "0" + inp;
return inp;
}
var date = new Date(),
y = date.getFullYear(),
m = btf(date.getMonth()),
d = btf(date.getDate()),
h = btf(date.getHours()),
min = btf(date.getMinutes()),
s = btf(date.getSeconds());
return `${d}.${m}.${y} - ${h}:${min}:${s}`;
}
function getSubs() {
var html = afterLoad('https://www.youtube.com/zekrommaster110')
var $ = afterLoad.$(html)
var subs = $('.yt-subscription-button-subscriber-count-branded-horizontal')['0'].attribs.title
return parseInt(subs.trim())
}
function createEmbed(current) {
return emb = new RichEmbed()
.setColor(0xf1c40f)
.setDescription(
`:trophy: **SUBSCRIBER GOAL 2017** :trophy: \n\n` +
`*Goal:* \`${goal} subscribers\`\n*End: \`${end}\`*\n\n` +
'**Current state:**\n\n' +
`**${current}** / ${goal} subscribers\n\n` +
`*${goal - current} until reaching goal*`
)
.setFooter(`${getTime()} - Refresh every 5 minutes`)
}
bot.on('message', (msg) => {
if (msg.author.id != '221905671296253953') return
if (msg.content.startsWith('--create')) {
msg.channel.send('', createEmbed(getSubs())).then(m => message = m)
timer = setInterval(() => {
message.edit('', createEmbed(getSubs()))
}, timeout * 60 * 1000)
}
if (msg.content.startsWith('--test')) {
var html = afterLoad('https://www.youtube.com/' + channelid)
var $ = afterLoad.$(html)
var subs = $('.yt-subscription-button-subscriber-count-branded-horizontal')['0'].attribs.title
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment