mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
55 lines
2.1 KiB
JavaScript
55 lines
2.1 KiB
JavaScript
const { Command } = require('discord.js-commando');
|
|
const { RichEmbed } = require('discord.js');
|
|
const request = require('superagent');
|
|
|
|
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 videoToSearch = args.video;
|
|
try {
|
|
const response = await request
|
|
.get('https://www.googleapis.com/youtube/v3/search')
|
|
.query({
|
|
part: 'snippet',
|
|
type: 'video',
|
|
maxResults: 1,
|
|
q: videoToSearch,
|
|
key: process.env.YOUTUBE_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?');
|
|
}
|
|
}
|
|
};
|