misc: readd search, using librex

This commit is contained in:
Cynthia Foxwell 2023-01-17 17:29:36 -07:00
parent 90b9a869e4
commit 97cb96d93c

View file

@ -1,6 +1,7 @@
const Command = require("../lib/command.js");
const CATEGORY = "misc";
const {librex} = require("../../config.json");
const {
hastebin,
safeString,
@ -179,7 +180,9 @@ gimg.callback = async function (msg, line) {
url: image.url,
},
footer: {
text: `Image ${Number(index) + 1}/${images.length}. Rerun to get a different image.`,
text: `Image ${Number(index) + 1}/${
images.length
}. Rerun to get a different image.`,
},
},
],
@ -542,3 +545,39 @@ generate.callback = async function (msg, line) {
return out;
};
hf.registerCommand(generate);
const search = new Command("search");
search.category = CATEGORY;
search.helpText = "Search, powered by LibreX";
search.addAlias("g");
search.addAlias("google");
search.addAlias("ddg");
search.callback = async function (msg, line) {
if (!librex) return "LibreX instance not defined.";
const query = encodeURIComponent(line);
if (line.startsWith("!")) {
const url = `https://api.duckduckgo.com/?q=${query}&format=json`;
const res = await fetch(url);
if (res.url != url) return res.url;
}
const res = await fetch(librex + "/api.php?q=${query}&p=0&t=0");
const results = await res.json().splice(0, 5);
let out = `__Results for \`${safeString(line)}\`__\n`;
for (const result of results) {
if (result.special_response) {
out += "> " + result.special_response.response.split("\n").join("\n> ");
out += `\n<${result.special_response.source}>`;
} else {
out += `**${result.title}** - <${result.url}>`;
out += `\n> ${result.description}`;
}
out += "\n\n";
}
return out.trim();
};
hf.registerCommand(search);