Files
xiao/commands/search/youtube.js
T
Daniel Odendahl Jr 8074fe51bd Move to snekfetch 🐍
2017-04-16 01:12:22 +00:00

48 lines
2.0 KiB
JavaScript

const { Command } = require('discord.js-commando');
const { RichEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
module.exports = class YouTubeCommand extends Command {
constructor(client) {
super(client, {
name: 'youtube',
aliases: [
'video'
],
group: 'search',
memberName: 'youtube',
description: 'Searches YouTube for a video. (;youtube video)',
examples: [';youtube video'],
args: [{
key: 'video',
prompt: 'What would you like to search for?',
type: 'string'
}]
});
}
async run(message, args) {
if (message.channel.type !== 'dm') {
if (!message.channel.permissionsFor(this.client.user).hasPermission(['SEND_MESSAGES', 'READ_MESSAGES'])) return;
if (!message.channel.permissionsFor(this.client.user).hasPermission('EMBED_LINKS')) return message.say(':x: Error! I don\'t have the Embed Links Permission!');
}
const video = encodeURIComponent(args.video);
try {
const response = await snekfetch
.get(`https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&maxResults=1&q=${video}&key=${process.env.GOOGLE_KEY}`);
const data = response.body.items[0];
const embed = new RichEmbed()
.setColor(0xDD2825)
.setTitle(data.snippet.title)
.setDescription(data.snippet.description)
.setAuthor(`YouTube - ${data.snippet.channelTitle}`, 'https://cdn3.iconfinder.com/data/icons/social-icons-5/607/YouTube_Play.png')
.setURL(`https://www.youtube.com/watch?v=${data.id.videoId}`)
.setThumbnail(data.snippet.thumbnails.default.url);
return message.embed(embed);
}
catch (err) {
return message.say(':x: Error! Something went wrong! Maybe no video was found?');
}
}
};