From 97cb96d93c6628b31b95d81230d3f10db11dd1ec Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 17 Jan 2023 17:29:36 -0700 Subject: [PATCH] misc: readd search, using librex --- src/modules/misc.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/modules/misc.js b/src/modules/misc.js index 9b1cb52..bf2958c 100644 --- a/src/modules/misc.js +++ b/src/modules/misc.js @@ -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);