From 63ce6d377f6f6252ce8f15f653b1ba4adc23d8a3 Mon Sep 17 00:00:00 2001 From: bignutty <3515180-bignutty@users.noreply.gitlab.com> Date: Wed, 28 May 2025 15:10:29 +0200 Subject: [PATCH] wikipedia slash --- .../interaction/slash/search/wikipedia.js | 87 +++++++++++++++++++ commands/message/search/wikipedia.js | 5 +- 2 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 commands/interaction/slash/search/wikipedia.js diff --git a/commands/interaction/slash/search/wikipedia.js b/commands/interaction/slash/search/wikipedia.js new file mode 100644 index 0000000..05fa1ad --- /dev/null +++ b/commands/interaction/slash/search/wikipedia.js @@ -0,0 +1,87 @@ +const {createDynamicCardStack} = require("#cardstack/index"); +const { PERMISSION_GROUPS } = require('#constants'); + +const { createEmbed, page } = require('#utils/embed'); +const { acknowledge } = require('#utils/interactions'); +const { editOrReply } = require('#utils/message') +const { STATICS } = require('#utils/statics') + +const superagent = require('superagent'); + +const { + InteractionContextTypes, + ApplicationIntegrationTypes, + ApplicationCommandOptionTypes +} = require("detritus-client/lib/constants"); + +module.exports = { + name: 'wikipedia', + description: 'Search on Wikipedia.', + contexts: [ + InteractionContextTypes.GUILD, + InteractionContextTypes.PRIVATE_CHANNEL, + InteractionContextTypes.BOT_DM + ], + integrationTypes: [ + ApplicationIntegrationTypes.USER_INSTALL + ], + options: [ + { + name: 'query', + description: 'Search query.', + type: ApplicationCommandOptionTypes.STRING, + required: true + }, + { + name: 'incognito', + description: 'Makes the response only visible to you.', + type: ApplicationCommandOptionTypes.BOOLEAN, + required: false, + default: false + } + ], + run: async (context, args) => { + await acknowledge(context, args.incognito, [...PERMISSION_GROUPS.baseline_slash]); + + try{ + let search = await superagent.get(`https://api.wikimedia.org/core/v1/wikipedia/en/search/page`) + .query({ + q: args.query, + limit: 100, + language: 'en' + }) + + let pages = [] + + if(!search.body.pages.length) return editOrReply(context, createEmbed("error", context, `No results found.`)) + + for(const res of Object.values(search.body.pages)){ + let p = createEmbed("default", context, { + author: { + name: res.title, + url: `https://en.wikipedia.org/wiki/${res.key}` + }, + footer: { + iconUrl: STATICS.wikipedia, + text: `Wikipedia • ${context.application.name}` + } + }) + + if(res.thumbnail && res.thumbnail.url) p.thumbnail = { + url: 'https:' + res.thumbnail.url.replace(/d3\/.*?\/[0-9]*px-/, '/d3/').replace('/thumb/d/', '/d') + } + + if(res.excerpt) p.description = res.excerpt.replace(/<.*?>/g, '') + + pages.push(page(p)) + } + + return await createDynamicCardStack(context, { + cards: pages + }); + }catch(e){ + console.log(e) + return editOrReply(context, createEmbed("error", context, `Unable to perform wikipedia search.`)) + } + }, +}; \ No newline at end of file diff --git a/commands/message/search/wikipedia.js b/commands/message/search/wikipedia.js index 685284c..221386c 100644 --- a/commands/message/search/wikipedia.js +++ b/commands/message/search/wikipedia.js @@ -17,7 +17,8 @@ module.exports = { description_short: 'Search on Wikipedia', examples: ['wiki otters'], category: 'search', - usage: 'wikipedia ' + usage: 'wikipedia ', + slashCommand: "wikipedia" }, permissionsClient: [...PERMISSION_GROUPS.baseline], run: async (context, args) => { @@ -51,7 +52,7 @@ module.exports = { url: 'https:' + res.thumbnail.url.replace(/d3\/.*?\/[0-9]*px-/, '/d3/').replace('/thumb/d/', '/d') } - if(res.excerpt) p.description = res.excerpt.replace(/\<.*?\>/g, '') + if(res.excerpt) p.description = res.excerpt.replace(/<.*?>/g, '') pages.push(page(p)) }