misc.search: default results 2, --results=num (req by vin)

This commit is contained in:
Cynthia Foxwell 2023-01-17 17:38:17 -07:00
parent 477f49f473
commit 906baf31c2

View file

@ -552,23 +552,25 @@ search.helpText = "Search, powered by LibreX";
search.addAlias("g");
search.addAlias("google");
search.addAlias("ddg");
search.callback = async function (msg, line) {
search.callback = async function (msg, line, {results = 2}, [query]) {
if (!librex) return "LibreX instance not defined.";
if (!line || line == "") return "Search query required.";
if (!query || query == "") return "Search query required.";
const query = encodeURIComponent(line);
const encodedQuery = encodeURIComponent(line);
if (line.startsWith("!")) {
const url = `https://api.duckduckgo.com/?q=${query}&format=json`;
const url = `https://api.duckduckgo.com/?q=${encodedQuery}&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().then((arr) => arr.splice(0, 5));
const res = await fetch(`${librex}/api.php?q=${encodedQuery}&p=0&t=0`);
const searchResults = await res
.json()
.then((arr) => arr.splice(0, Number(results)));
let out = `__**Results for \`${safeString(line)}\`**__\n`;
for (const result of results) {
for (const result of searchResults) {
if (result.special_response) {
out += "> " + result.special_response.response.split("\n").join("\n> ");
out += `\n<${result.special_response.source}>`;