From 3f27da7269f42e62a4e4513f97ab122ed464204c Mon Sep 17 00:00:00 2001 From: GiovaniFZ Date: Sun, 6 Oct 2024 12:52:19 -0300 Subject: [PATCH] feat: /wiki command --- commands/wiki.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 commands/wiki.js diff --git a/commands/wiki.js b/commands/wiki.js new file mode 100644 index 0000000..543a23d --- /dev/null +++ b/commands/wiki.js @@ -0,0 +1,38 @@ +const axios = require("axios"); + +function capitalizeFirstLetter(string) { + return string.charAt(0).toUpperCase() + string.slice(1); +} + +function mediaWikiToMarkdown(input) { + input = input.replace(/===(.*?)===/g, '*$1*'); + input = input.replace(/==(.*?)==/g, '*$1*'); + input = input.replace(/=(.*?)=/g, '*$1*'); + input = input.replace(/'''(.*?)'''/g, '**$1**'); + input = input.replace(/''(.*?)''/g, '_$1_'); + input = input.replace(/^\*\s/gm, '- '); + input = input.replace(/^\#\s/gm, '1. '); + input = input.replace(/{{Quote(.*?)}}/g, "```\n$1```\n"); + input = input.replace(/\[\[(.*?)\|?(.*?)\]\]/g, (_, link, text) => { + const sanitizedLink = link.replace(/ /g, '_'); // Substituir espaços por underscores + return text ? `[${text}](${sanitizedLink})` : `[${sanitizedLink}](${sanitizedLink})`; + }); + input = input.replace(/\[\[File:(.*?)\|.*?\]\]/g, '![$1](https://en.wikipedia.org/wiki/File:$1)'); + + return input; +} + +module.exports = (bot) => { + bot.command("wiki", async (ctx) => { + const userInput = capitalizeFirstLetter(ctx.message.text.split(' ')[1]); + const apiUrl = `https://en.wikipedia.org/w/index.php?title=${userInput}&action=raw`; + const response = await axios(apiUrl, { headers: { 'Accept': "text/plain" } }); + + // Limpa a resposta e remove a infobox + const convertedResponse = response.data.replace(/<\/?div>/g, "").replace(/{{Infobox.*?}}/s, ""); + + const result = mediaWikiToMarkdown(convertedResponse).slice(0, 2048); + + ctx.reply(result, { parse_mode: 'Markdown', disable_web_page_preview: true, reply_to_message_id: ctx.message.message_id }); + }); +};