64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
|
const Command = require("../lib/command.js");
|
||
|
const CATEGORY = "misc";
|
||
|
|
||
|
const fetch = require("node-fetch");
|
||
|
const {safeString, parseHtmlEntities} = require("../lib/utils.js");
|
||
|
|
||
|
const yt = new Command("youtube");
|
||
|
yt.addAlias("yt");
|
||
|
yt.category = CATEGORY;
|
||
|
yt.helpText = "Search YouTube";
|
||
|
yt.usage = "[search term]";
|
||
|
yt.callback = async function (msg, line) {
|
||
|
if (!line) return "Arguments are required.";
|
||
|
|
||
|
const req = await fetch(
|
||
|
`https://www.googleapis.com/youtube/v3/search?key=${
|
||
|
hf.apikeys.google
|
||
|
}&maxResults=5&part=snippet&type=video&q=${encodeURIComponent(line)}`
|
||
|
).then((x) => x.json());
|
||
|
|
||
|
const topVid = req.items[0];
|
||
|
|
||
|
let out = `**${safeString(
|
||
|
parseHtmlEntities(topVid.snippet.title)
|
||
|
)}** | \`${safeString(
|
||
|
parseHtmlEntities(topVid.snippet.channelTitle)
|
||
|
)}\`\nhttps://youtu.be/${topVid.id.videoId}\n\n**__See Also:__**\n`;
|
||
|
|
||
|
for (let i = 1; i < req.items.length; i++) {
|
||
|
const vid = req.items[i];
|
||
|
out += `- **${safeString(
|
||
|
parseHtmlEntities(vid.snippet.title)
|
||
|
)}** | By: \`${safeString(
|
||
|
parseHtmlEntities(vid.snippet.channelTitle)
|
||
|
)}\` | <https://youtu.be/${vid.id.videoId}>\n`;
|
||
|
}
|
||
|
|
||
|
return out;
|
||
|
};
|
||
|
hf.registerCommand(yt);
|
||
|
|
||
|
const fyt = new Command("fyt");
|
||
|
fyt.category = CATEGORY;
|
||
|
fyt.helpText = "Search YouTube and take the first result.";
|
||
|
fyt.usage = "[search term]";
|
||
|
fyt.callback = async function (msg, line) {
|
||
|
if (!line) return "Arguments are required.";
|
||
|
|
||
|
const req = await fetch(
|
||
|
`https://www.googleapis.com/youtube/v3/search?key=${
|
||
|
hf.apikeys.google
|
||
|
}&maxResults=2&part=snippet&type=video&q=${encodeURIComponent(line)}`
|
||
|
).then((x) => x.json());
|
||
|
|
||
|
const vid = req.items[0];
|
||
|
|
||
|
return `**${safeString(
|
||
|
parseHtmlEntities(vid.snippet.title)
|
||
|
)}** | \`${safeString(
|
||
|
parseHtmlEntities(vid.snippet.channelTitle)
|
||
|
)}\`\nhttps://youtu.be/${vid.id.videoId}`;
|
||
|
};
|
||
|
hf.registerCommand(fyt);
|