Welcome to the yt-api a light weight and easy to use api.
Installing is as simple as using
npm i yt-api
Setup
To use the api you need a api key from google and you can get that here
Once you have created the api key you need to enable the Youtube Data API v3
by clicking on the libraries tab and clicking on the Youtube Data API v3
then click on enable and your done!
Using the api
const yt-api = require("yt-api");
const API = new yt-api("PASTE_API_KEY_HERE");
Getting video info
API.getVideoInfo("YOUTUBE_VIDEO_ID" , info => {
console.log("This video's title is "+info.title+" with a id of "+info.videoID+".");
});
videoID:
url:
title:
channel: {
id:
user:
url:
},
livestream:
category:
thumbnail:
description:
publishDate:
duration: {
days:
hours:
minutes:
seconds:
},
statistics: {
views:
likes:
dislikes:
comments:
favorites:
}
Searching youtube
API.searchYouTube("Alan walker faded" , info => {
console.log(`The video's title is ${info.title} and its id is ${info.videoID}`);
});
videoID:
url:
title:
channel: {
id:
user:
url:
},
thumbnail:
Getting more than 1 video result aka the searchVideos function
API.searchVideos("Marshmello alone", 5 , videos => {
let sidenum=0;
console.log(videos.map(v=>`the video's title is ${v.title} and the id is ${v.videoID}`).join("\n"));
let video = videos[];
console.log(`This videos title is ${video.title} and the id is ${video.videoID}`);
});
videoID:
url:
title:
channel: {
id:
user:
url:
},
thumbnail:
Discord music bot example.
Ever wanted to make a music bot with a song selection well now you can using this code and this is the full code no cuts no unpasted code its all the same code minus the np and queue command wich i hope you can figure out your self's.
const { Client } = require("discord.js");
const bot = new Client();
const ytdl = require("ytdl-core");
let YouTube = require("./index.js");
let yt = new YouTube("API_KEY_HERE");
let guilds = {};
bot.login("BOT_TOKEN_HERE");
bot.on("ready", () => console.log("REEADY!"));
bot.on("message", msg => {
let prefix = ';';
let args = msg.content.slice(prefix.length).trim().split(" ");
let cmd = args.shift().toLowerCase();
let guild = guilds[msg.guild.id];
if(cmd === "play") {
if(!guild) guild = guilds[msg.guild.id] = {
queue: [],
isPlaying: false
};
if(!msg.member.voiceChannel) return msg.channel.send("You must be in a voice channel first!");
let song = args.join(" ");
if(!song) return msg.channel.send("You need to provide a searchterm!");
if(guild.isPlaying) {
yt.searchVideos(song, 5, async videos => {
try {
let num=0;
let m = await msg.channel.send(`
**Song selection:**
${videos.map(v=>`**${++num}:** ${v.title}`).join("\n")}
Please provide a value between 1 and 5.
`);
let collector = await msg.channel.awaitMessages(m=>m.content > 0 && m.content < 6 && m.author.id === msg.author.id && m.channel.id === msg.channel.id, {
maxMatches: 1,
time: 10000,
errors: ['time']
});
let selection = parseInt(collector.first().content);
let vid = videos[selection - 1];
m.delete(5000);
yt.getVideoInfo(vid.videoID, i => {
guild.queue.push({
title: i.title,
id: i.videoID,
url: i.url,
duration: {
days: i.duration.days,
hours: i.duration.hours,
minutes: i.duration.minutes,
seconds: i.duration.seconds
}
});
return msg.channel.send(`Added **${i.title}** to the queue!`);
});
} catch (error) {
return msg.channel.send("No value was entered. Canceling video selection!");
}
});
} else {
guild.isPlaying = true;
yt.searchVideos(song, 5, async videos => {
try {
let num=0;
let m = await msg.channel.send(`
**Song selection:**
${videos.map(v=>`**${++num}:** ${v.title}`).join("\n")}
Please provide a value between 1 and 5.
`);
let collector = await msg.channel.awaitMessages(m=>m.content > 0 && m.content < 6 && m.author.id === msg.author.id && m.channel.id === msg.channel.id, {
maxMatches: 1,
time: 10000,
errors: ['time']
});
let selection = parseInt(collector.first().content);
let vid = videos[selection - 1];
m.delete(5000);
yt.getVideoInfo(vid.videoID, i => {
guild.queue.push({
title: i.title,
id: i.videoID,
url: i.url,
duration: {
days: i.duration.days,
hours: i.duration.hours,
minutes: i.duration.minutes,
seconds: i.duration.seconds
}
});
return playSong(i.url, guild, msg);
});
} catch (error) {
return msg.channel.send("No value was entered. Canceling video selection!");
}
});
}
}
});
function playSong(url, guild, msg) {
let vc = msg.member.voiceChannel;
vc.join().then(con => {
let dispatcher = con.playStream(ytdl(url, { filter: "audioonly" }));
dispatcher.on("end", () => {
guild.queue.shift();
if(guild.queue.length === 0) {
vc.leave();
delete guilds[msg.guild.id];
} else {
playSong(guild.queue[0].url, guild, msg);
}
});
});
msg.channel.send(`Now playing: **${guild.queue[0].title}** (${guild.queue[0].duration.hours}${guild.queue[0].duration.minutes}${guild.queue[0].duration.seconds})`);
}