diff options
author | V <vendicated@riseup.net> | 2023-09-24 16:02:18 +0200 |
---|---|---|
committer | V <vendicated@riseup.net> | 2023-09-24 16:02:18 +0200 |
commit | 30ac25607023752031aa98060cbf8a736109992d (patch) | |
tree | 79bb82b6634ef601db6c98e751275607ec54dbea /src/plugins/wikisearch | |
parent | d0e2a324717e600736a18b88fe89a21c640a406b (diff) | |
download | Vencord-30ac25607023752031aa98060cbf8a736109992d.tar.gz Vencord-30ac25607023752031aa98060cbf8a736109992d.tar.bz2 Vencord-30ac25607023752031aa98060cbf8a736109992d.zip |
migrate all plugins to folders
Diffstat (limited to 'src/plugins/wikisearch')
-rw-r--r-- | src/plugins/wikisearch/index.ts | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/plugins/wikisearch/index.ts b/src/plugins/wikisearch/index.ts new file mode 100644 index 0000000..81dc37a --- /dev/null +++ b/src/plugins/wikisearch/index.ts @@ -0,0 +1,110 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2023 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. +*/ + +import { ApplicationCommandInputType, ApplicationCommandOptionType, findOption, sendBotMessage } from "@api/Commands"; +import { Devs } from "@utils/constants"; +import definePlugin from "@utils/types"; + +export default definePlugin({ + name: "Wikisearch", + description: "Searches Wikipedia for your requested query. (/wikisearch)", + authors: [Devs.Samu], + dependencies: ["CommandsAPI"], + commands: [ + { + name: "wikisearch", + description: "Searches Wikipedia for your request.", + inputType: ApplicationCommandInputType.BUILT_IN, + options: [ + { + name: "search", + description: "Word to search for", + type: ApplicationCommandOptionType.STRING, + required: true + }, + ], + execute: async (_, ctx) => { + const word = findOption(_, "search", ""); + + if (!word) { + return sendBotMessage(ctx.channel.id, { + content: "No word was defined!" + }); + } + + const dataSearchParams = new URLSearchParams({ + action: "query", + format: "json", + list: "search", + formatversion: "2", + origin: "*", + srsearch: word + }); + + const data = await fetch("https://en.wikipedia.org/w/api.php?" + dataSearchParams).then(response => response.json()) + .catch(err => { + console.log(err); + sendBotMessage(ctx.channel.id, { content: "There was an error. Check the console for more info" }); + return null; + }); + + if (!data) return; + + if (!data.query?.search?.length) { + console.log(data); + return sendBotMessage(ctx.channel.id, { content: "No results given" }); + } + + const altData = await fetch(`https://en.wikipedia.org/w/api.php?action=query&format=json&prop=info%7Cdescription%7Cimages%7Cimageinfo%7Cpageimages&list=&meta=&indexpageids=1&pageids=${data.query.search[0].pageid}&formatversion=2&origin=*`) + .then(res => res.json()) + .then(data => data.query.pages[0]) + .catch(err => { + console.log(err); + sendBotMessage(ctx.channel.id, { content: "There was an error. Check the console for more info" }); + return null; + }); + + if (!altData) return; + + const thumbnailData = altData.thumbnail; + + const thumbnail = thumbnailData && { + url: thumbnailData.source.replace(/(50px-)/ig, "1000px-"), + height: thumbnailData.height * 100, + width: thumbnailData.width * 100 + }; + + sendBotMessage(ctx.channel.id, { + embeds: [ + { + type: "rich", + title: data.query.search[0].title, + url: `https://wikipedia.org/w/index.php?curid=${data.query.search[0].pageid}`, + color: "0x8663BE", + description: data.query.search[0].snippet.replace(/( |<([^>]+)>)/ig, "").replace(/(")/ig, "\"") + "...", + image: thumbnail, + footer: { + text: "Powered by the Wikimedia API", + }, + } + ] as any + }); + } + } + ] +}); |