From f4a22934c6bc10b14101c8cf25cdda788e42e085 Mon Sep 17 00:00:00 2001 From: derpystuff <3515180-derpystuff@users.noreply.gitlab.com> Date: Sun, 22 May 2022 12:49:08 +0200 Subject: [PATCH] [cmd] wolfram-alpha --- commands/message/search/wolfram-alpha.js | 65 ++++++++++++++++++++++++ labscore/api/endpoints.js | 1 + labscore/api/index.js | 6 +++ labscore/utils/statics.js | 7 ++- 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 commands/message/search/wolfram-alpha.js diff --git a/commands/message/search/wolfram-alpha.js b/commands/message/search/wolfram-alpha.js new file mode 100644 index 0000000..7540ba5 --- /dev/null +++ b/commands/message/search/wolfram-alpha.js @@ -0,0 +1,65 @@ +const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed') +const { link } = require('../../../labscore/utils/markdown') +const { editOrReply } = require('../../../labscore/utils/message') +const { STATICS } = require('../../../labscore/utils/statics') + +const { paginator } = require('../../../labscore/client'); +const { wolframAlpha } = require('../../../labscore/api'); + +function createWolframPage(context, pod){ + let res = { + "embeds": [ + createEmbed("default", context, { + description: `**${pod.title}**`, + footer: { + iconUrl: STATICS.wolframalpha, + text: `Wolfram|Alpha • ${context.application.name}` + } + }) + ] + } + if(pod.value) res.embeds[0].description = res.embeds[0].description + `\n${pod.value}` + if(pod.image) res.embeds[0].image = { url: pod.image }; + return res; +} + +const reactions = { + previousPage: "⬅️", + nextPage: "➡️" +}; + +module.exports = { + name: 'wolframalpha', + label: 'query', + aliases: ['wa', 'wolfram-alpha'], + metadata: { + description: 'wolfram alpha search', + examples: ['wa Gray'], + category: 'search', + usage: 'wolframalpha ' + }, + run: async (context, args) => { + context.triggerTyping(); + if(!args.query) return editOrReply(context, {embeds:[createEmbed("warning", context, `Missing Parameter (query).`)]}) + try{ + let search = await wolframAlpha(context, args.query) + search = search.response + + let pages = [] + for(const res of search.body.data){ + pages.push(createWolframPage(context, res)) + } + + pages = formatPaginationEmbeds(pages) + const message = context.message + const paging = await paginator.createReactionPaginator({ + message, + pages, + reactions + }); + }catch(e){ + console.log(e) + return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform Wolfram|Alpha search.`)]}) + } + }, +}; \ No newline at end of file diff --git a/labscore/api/endpoints.js b/labscore/api/endpoints.js index e7d99e7..08553d1 100644 --- a/labscore/api/endpoints.js +++ b/labscore/api/endpoints.js @@ -14,6 +14,7 @@ const Api = Object.freeze({ SEARCH_GOOGLE_IMAGES: '/search/google-images', SEARCH_BING: '/search/bing', SEARCH_BING_IMAGES: '/search/bing-images', + SEARCH_WOLFRAM_ALPHA: '/search/wolfram-alpha', PHOTOFUNIA_YACHT: '/photofunia/yacht' }) diff --git a/labscore/api/index.js b/labscore/api/index.js index f91a610..e77c9c9 100644 --- a/labscore/api/index.js +++ b/labscore/api/index.js @@ -70,6 +70,12 @@ module.exports.bingImages = async function(context, query){ }) } +module.exports.wolframAlpha = async function(context, query){ + return await request(Api.SEARCH_WOLFRAM_ALPHA, "GET", {}, { + q: query + }) +} + module.exports.yacht = async function(context, text){ return await request(Api.PHOTOFUNIA_YACHT, "GET", {}, { text: text diff --git a/labscore/utils/statics.js b/labscore/utils/statics.js index 00458a9..b6e17f7 100644 --- a/labscore/utils/statics.js +++ b/labscore/utils/statics.js @@ -17,6 +17,10 @@ const Statics = Object.freeze({ google: { file: "brands/google.png", revision: 0 + }, + wolframalpha: { + file: "brands/wolframalpha.png", + revision: 0 } } }) @@ -29,5 +33,6 @@ module.exports.STATICS = Object.freeze({ photofunia: staticAsset(Statics.brands.photofunia), genius: staticAsset(Statics.brands.genius), bing: staticAsset(Statics.brands.bing), - google: staticAsset(Statics.brands.google) + google: staticAsset(Statics.brands.google), + wolframalpha: staticAsset(Statics.brands.wolframalpha) }) \ No newline at end of file