From 6276e16739331048b8e21469592d7bac7b667339 Mon Sep 17 00:00:00 2001 From: dragonfire535 Date: Sat, 11 Mar 2017 15:49:32 -0500 Subject: [PATCH] YouTube Command Added --- commands/search/youtube.js | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 commands/search/youtube.js diff --git a/commands/search/youtube.js b/commands/search/youtube.js new file mode 100644 index 00000000..b414028f --- /dev/null +++ b/commands/search/youtube.js @@ -0,0 +1,56 @@ +const commando = require('discord.js-commando'); +const Discord = require('discord.js'); +const request = require('request-promise'); +const config = require('../../config.json'); + +class YouTubeCommand extends commando.Command { + constructor(Client){ + super(Client, { + name: 'youtube', + group: 'search', + memberName: 'youtube', + description: 'Searches YouTube for a video. (;youtube video)', + examples: [';youtube video'] + }); + } + + async run(message, args) { + if(message.channel.type !== 'dm') { + if(!message.channel.permissionsFor(this.client.user).hasPermission('SEND_MESSAGES')) return; + if(!message.channel.permissionsFor(this.client.user).hasPermission('READ_MESSAGES')) return; + } + console.log("[Command] " + message.content); + let videotosearch = message.content.split(" ").slice(1).join("-"); + const options = { + method: 'GET', + uri: 'https://www.googleapis.com/youtube/v3/search', + qs: { + part: 'snippet', + type: 'video', + maxResults: 1, + q: videotosearch, + order: 'viewCount', + key: config.youtubekey + }, + json: true + } + request(options).then(function (response) { + if(response.items[0].snippet === undefined) { + message.channel.sendMessage(':x: Error! No Video Found!'); + } else { + const embed = new Discord.RichEmbed() + .setColor(0xDD2825) + .setTitle(response.items[0].snippet.title) + .setDescription(response.items[0].snippet.description) + .setAuthor('YouTube - ' + response.items[0].snippet.channelTitle, 'https://cdn3.iconfinder.com/data/icons/social-icons-5/607/YouTube_Play.png') + .setURL('https://www.youtube.com/watch?v=' + response.items[0].id.videoId) + .setThumbnail(response.items[0].snippet.thumbnails.default.url); + message.channel.sendEmbed(embed).catch(console.error); + } + }).catch(function (err) { + message.channel.sendMessage(":x: Error! Daily Quota Reached! Try again tomorrow!"); + }); + } +} + +module.exports = YouTubeCommand; \ No newline at end of file