Added pagination and proper HTML entity decoding to youtube
This commit is contained in:
parent
4b6f2155a9
commit
97f2f38d47
3 changed files with 31 additions and 11 deletions
|
@ -1,17 +1,23 @@
|
|||
const fetch = require("node-fetch");
|
||||
const { decodeEntities } = require("../utils/misc.js");
|
||||
const paginator = require("../utils/pagination/pagination.js");
|
||||
|
||||
exports.run = async (message, args) => {
|
||||
if (args.length === 0) return `${message.author.mention}, you need to provide something to search for!`;
|
||||
message.channel.sendTyping();
|
||||
const request = await fetch(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${encodeURIComponent(args.join(" "))}&key=${process.env.GOOGLE}`);
|
||||
const messages = [];
|
||||
const request = await fetch(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${encodeURIComponent(args.join(" "))}&key=${process.env.GOOGLE}&maxResults=50`);
|
||||
const result = await request.json();
|
||||
if (result.items[0].id.kind === "youtube#channel") {
|
||||
return `<:youtube:637020823005167626> **${result.items[0].snippet.title.replace("*", "\\*")}**\nhttps://youtube.com/channel/${result.items[0].id.channelId}`;
|
||||
} else if (result.items[0].id.kind === "youtube#playlist") {
|
||||
return `<:youtube:637020823005167626> **${result.items[0].snippet.title.replace("*", "\\*")}**\nCreated by **${result.items[0].snippet.channelTitle.replace("*", "\\*")}**\nhttps://youtube.com/playlist?list=${result.items[0].id.playlistId}`;
|
||||
} else {
|
||||
return `<:youtube:637020823005167626> **${result.items[0].snippet.title.replace("*", "\\*")}**\nUploaded by **${result.items[0].snippet.channelTitle.replace("*", "\\*")}** on **${result.items[0].snippet.publishedAt.split("T")[0]}**\nhttps://youtube.com/watch?v=${result.items[0].id.videoId}`;
|
||||
for (const [i, value] of result.items.entries()) {
|
||||
if (value.id.kind === "youtube#channel") {
|
||||
messages.push(`Page ${i + 1} of ${result.items.length}\n<:youtube:637020823005167626> **${decodeEntities(value.snippet.title).replace("*", "\\*")}**\nhttps://youtube.com/channel/${value.id.channelId}`);
|
||||
} else if (value.id.kind === "youtube#playlist") {
|
||||
messages.push(`Page ${i + 1} of ${result.items.length}\n<:youtube:637020823005167626> **${decodeEntities(value.snippet.title).replace("*", "\\*")}**\nCreated by **${decodeEntities(value.snippet.channelTitle).replace("*", "\\*")}**\nhttps://youtube.com/playlist?list=${value.id.playlistId}`);
|
||||
} else {
|
||||
messages.push(`Page ${i + 1} of ${result.items.length}\n<:youtube:637020823005167626> **${decodeEntities(value.snippet.title).replace("*", "\\*")}**\nUploaded by **${decodeEntities(value.snippet.channelTitle).replace("*", "\\*")}** on **${value.snippet.publishedAt.split("T")[0]}**\nhttps://youtube.com/watch?v=${value.id.videoId}`);
|
||||
}
|
||||
}
|
||||
return paginator(message, messages);
|
||||
};
|
||||
|
||||
exports.aliases = ["yt", "video", "ytsearch"];
|
||||
|
|
|
@ -63,6 +63,24 @@ exports.regexEscape = (string) => {
|
|||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
||||
};
|
||||
|
||||
// decodeEntities(string)
|
||||
exports.decodeEntities = (string) => {
|
||||
var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
|
||||
var translate = {
|
||||
"nbsp": " ",
|
||||
"amp": "&",
|
||||
"quot": "\"",
|
||||
"lt": "<",
|
||||
"gt": ">"
|
||||
};
|
||||
return string.replace(translate_re, function(match, entity) {
|
||||
return translate[entity];
|
||||
}).replace(/&#(\d+);/gi, function(match, numStr) {
|
||||
var num = parseInt(numStr, 10);
|
||||
return String.fromCharCode(num);
|
||||
});
|
||||
};
|
||||
|
||||
// define defaults for prefixes and tags
|
||||
exports.defaults = {
|
||||
prefix: "&"
|
||||
|
|
|
@ -6,7 +6,6 @@ const paginationEmbed = async (message, pages, timeout = 120000) => {
|
|||
const manageMessages = message.channel.guild.members.get(client.user.id).permission.has("manageMessages") || message.channel.permissionsOf(client.user.id).has("manageMessages") ? true : false;
|
||||
let page = 0;
|
||||
let deleted = false;
|
||||
pages[page].embed.footer.text = `Page ${page + 1} of ${pages.length}`;
|
||||
const currentPage = await message.channel.createMessage(pages[page]);
|
||||
const emojiList = ["◀", "🔢", "▶", "🗑"];
|
||||
for (const emoji of emojiList) {
|
||||
|
@ -18,7 +17,6 @@ const paginationEmbed = async (message, pages, timeout = 120000) => {
|
|||
switch (reaction.name) {
|
||||
case "◀":
|
||||
page = page > 0 ? --page : pages.length - 1;
|
||||
pages[page].embed.footer.text = `Page ${page + 1} of ${pages.length}`;
|
||||
currentPage.edit(pages[page]);
|
||||
if (manageMessages) msg.removeReaction("◀", userID);
|
||||
break;
|
||||
|
@ -31,7 +29,6 @@ const paginationEmbed = async (message, pages, timeout = 120000) => {
|
|||
return messageCollector.on("message", response => {
|
||||
askMessage.delete();
|
||||
page = Number(response.content) - 1;
|
||||
pages[page].embed.footer.text = `Page ${page + 1} of ${pages.length}`;
|
||||
currentPage.edit(pages[page]);
|
||||
if (manageMessages) msg.removeReaction("🔢", userID);
|
||||
});
|
||||
|
@ -41,7 +38,6 @@ const paginationEmbed = async (message, pages, timeout = 120000) => {
|
|||
break;
|
||||
case "▶":
|
||||
page = page + 1 < pages.length ? ++page : 0;
|
||||
pages[page].embed.footer.text = `Page ${page + 1} of ${pages.length}`;
|
||||
currentPage.edit(pages[page]);
|
||||
if (manageMessages) msg.removeReaction("▶", userID);
|
||||
break;
|
||||
|
|
Loading…
Reference in a new issue