mrmBot-Matrix/commands/general/youtube.js

32 lines
2.1 KiB
JavaScript
Raw Normal View History

const fetch = require("node-fetch");
const { decodeEntities } = require("../../utils/misc.js");
const paginator = require("../../utils/pagination/pagination.js");
const Command = require("../../classes/command.js");
class YouTubeCommand extends Command {
async run() {
if (this.args.length === 0) return "You need to provide something to search for!";
this.client.sendChannelTyping(this.message.channel.id);
const messages = [];
const request = await fetch(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${encodeURIComponent(this.args.join(" "))}&key=${process.env.GOOGLE}&maxResults=50`);
const result = await request.json();
if (result.error && result.error.code === 403) return "I've exceeded my YouTube API search quota for the day. Check back later.";
for (const [i, value] of result.items.entries()) {
if (value.id.kind === "youtube#channel") {
2021-05-17 15:13:29 +00:00
messages.push({ content: `Page ${i + 1} of ${result.items.length}\n<:youtube:637020823005167626> **${decodeEntities(value.snippet.title).replaceAll("*", "\\*")}**\nhttps://youtube.com/channel/${value.id.channelId}` });
} else if (value.id.kind === "youtube#playlist") {
2021-05-17 15:13:29 +00:00
messages.push({ content: `Page ${i + 1} of ${result.items.length}\n<:youtube:637020823005167626> **${decodeEntities(value.snippet.title).replaceAll("*", "\\*")}**\nCreated by **${decodeEntities(value.snippet.channelTitle).replaceAll("*", "\\*")}**\nhttps://youtube.com/playlist?list=${value.id.playlistId}` });
} else {
2021-05-17 15:13:29 +00:00
messages.push({ content: `Page ${i + 1} of ${result.items.length}\n<:youtube:637020823005167626> **${decodeEntities(value.snippet.title).replaceAll("*", "\\*")}**\nUploaded by **${decodeEntities(value.snippet.channelTitle).replaceAll("*", "\\*")}** on **${value.snippet.publishedAt.split("T")[0]}**\nhttps://youtube.com/watch?v=${value.id.videoId}` });
}
}
return paginator(this.client, this.message, messages);
}
static description = "Searches YouTube";
static aliases = ["yt", "video", "ytsearch"];
static arguments = ["[query]"];
static requires = "google";
}
module.exports = YouTubeCommand;