diff --git a/commands/interaction/slash/info/avatar.js b/commands/interaction/slash/info/avatar.js new file mode 100644 index 0000000..07bd696 --- /dev/null +++ b/commands/interaction/slash/info/avatar.js @@ -0,0 +1,34 @@ +const { Constants } = require('detritus-client'); +const { InteractionCallbackTypes, ApplicationCommandOptionTypes } = Constants; + +const { createEmbed } = require('../../../../labscore/utils/embed'); + +const { editOrReply } = require('../../../../labscore/utils/message'); + +module.exports = { + description: 'Get someones avatar.', + name: 'avatar', + options: [ + { + name: 'user', + description: 'User to get the avatar from.', + type: ApplicationCommandOptionTypes.USER, + required: false + } + ], + run: async (context, args) => { + await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE}) + + if(!args.user) return editOrReply(context, createEmbed("default", context, { + image: { + url: context.user.avatarUrl + '?size=4096' + } + })) + + return editOrReply(context, createEmbed("default", context, { + image: { + url: args.user.avatarUrl + '?size=4096' + } + })) + }, +}; \ No newline at end of file diff --git a/commands/interaction/slash/search/bing.js b/commands/interaction/slash/search/bing.js new file mode 100644 index 0000000..32b634c --- /dev/null +++ b/commands/interaction/slash/search/bing.js @@ -0,0 +1,139 @@ +const { Constants } = require('detritus-client'); +const { InteractionCallbackTypes, ApplicationCommandOptionTypes } = Constants; + +const { createEmbed, page, formatPaginationEmbeds } = require('../../../../labscore/utils/embed'); + +const { paginator } = require('../../../../labscore/client'); +const { STATICS } = require('../../../../labscore/utils/statics'); +const { bing } = require('../../../../labscore/api'); +const { link, citation, codeblock } = require('../../../../labscore/utils/markdown'); +const { editOrReply } = require('../../../../labscore/utils/message'); + + + +function createSearchResultPage(context, entry){ + let res; + switch(entry.type){ + case 1: // WebPage + res = page(createEmbed("default", context, { + author: { + iconUrl: `https://www.google.com/s2/favicons?domain=${encodeURIComponent(entry.result.url)}&sz=256`, + name: entry.result.title, + url: entry.result.url + }, + fields: [], + description: `${entry.result.snippet}`, + footer: { + iconUrl: STATICS.bing, + text: `Microsoft Bing • ${context.application.name}` + } + })) + if(entry.result.image) res.embeds[0].thumbnail = { url: entry.result.image } + if(entry.result.deepLinks) { + let fl = entry.result.deepLinks; + while(fl.length >= 1){ + fields = fl.splice(0, 4) + fields = fields.map((f)=>link(f.url, f.title)) + res.embeds[0].fields.push({ + name: "​", + value: fields.join('\n'), + inline: true + }) + } + } + break; + case 2: // Entity + res = page(createEmbed("default", context, { + author: { + name: entry.result.title, + url: entry.result.url + }, + description: `${entry.result.description}`, + fields: [], + footer: { + iconUrl: STATICS.bing, + text: `Microsoft Bing • ${context.application.name}` + } + })) + if(entry.result.sources.description) res.embeds[0].description += citation(1, entry.result.sources.description.url, `Source: ${entry.result.sources.description.title}`) + if(entry.result.image) res.embeds[0].thumbnail = { url: entry.result.image } + if(entry.result.fields){ + // only up to 6 fields + for(const f of entry.result.fields.splice(0, 6)){ + if(f.url){ + res.embeds[0].fields.push({ + name: f.title, + value: f.value, + inline: true + }) + continue; + } + res.embeds[0].fields.push({ + name: f.title, + value: f.value, + inline: true + }) + } + } + break; + case 6: // Computation + res = page(createEmbed("default", context, { + author: { + iconUrl: `https://www.google.com/s2/favicons?domain=${encodeURIComponent("bing.com")}&sz=256`, + name: entry.result.expression + }, + description: `${codeblock("python", [entry.result.value])}`, + fields: [], + footer: { + iconUrl: STATICS.bing, + text: `Microsoft Bing • ${context.application.name}` + } + })) + break; + default: + break; + } + + return res; +} + + + +module.exports = { + description: 'Search on Bing.', + name: 'bing', + options: [ + { + name: 'query', + description: 'Search query', + type: ApplicationCommandOptionTypes.STRING, + required: true + } + ], + run: async (context, args) => { + await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE}) + + try{ + let search = await bing(context, args.query, context.channel.nsfw) + search = search.response + + if(search.body.status == 2) return editOrReply(context, createEmbed("error", context, search.body.message)) + + let pages = [] + for(const res of search.body.results){ + let sp = createSearchResultPage(context, res) + if(sp) pages.push(sp) + } + + if(!pages.length) return editOrReply(context, createEmbed("warning", context, `No results found.`)) + + await paginator.createPaginator({ + context, + pages: formatPaginationEmbeds(pages) + }); + }catch(e){ + console.log(e) + return editOrReply(context, createEmbed("error", context, `Unable to perform bing search.`)) + } + }, +}; \ No newline at end of file diff --git a/commands/interaction/slash/search/google-images.js b/commands/interaction/slash/search/google-images.js new file mode 100644 index 0000000..0f80c1c --- /dev/null +++ b/commands/interaction/slash/search/google-images.js @@ -0,0 +1,66 @@ +const { Constants } = require('detritus-client'); +const { InteractionCallbackTypes, ApplicationCommandOptionTypes } = Constants; + +const { createEmbed, page, formatPaginationEmbeds } = require('../../../../labscore/utils/embed'); + +const { paginator } = require('../../../../labscore/client'); +const { STATICS } = require('../../../../labscore/utils/statics'); +const { google, googleImages } = require('../../../../labscore/api'); +const { editOrReply } = require('../../../../labscore/utils/message'); + +function createImageResultPage(context, result) { + let res = page(createEmbed("default", context, { + author: { + iconUrl: `https://www.google.com/s2/favicons?domain=${encodeURIComponent(result.url)}&sz=256`, + name: result.title, + url: result.url + }, + image: { + url: result.image + }, + footer: { + iconUrl: STATICS.google, + text: `Google Images • ${context.application.name}` + } + })) + if (result.thumbnail) res.embeds[0].thumbnail = { url: result.thumbnail }; + return res; +} + +module.exports = { + description: 'Search on Google Images.', + name: 'google-images', + options: [ + { + name: 'query', + description: 'Search query', + type: ApplicationCommandOptionTypes.STRING, + required: true + } + ], + run: async (context, args) => { + await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE}) + + try { + let search = await googleImages(context, args.query, context.channel.nsfw) + search = search.response + + if (search.body.status == 2) return editOrReply(context, createEmbed("error", context, search.body.message)) + + let pages = [] + for (const res of search.body.results) { + pages.push(createImageResultPage(context, res)) + } + + if (!pages.length) return editOrReply(context, createEmbed("warning", context, `No results found.`)) + + await paginator.createPaginator({ + context, + pages: formatPaginationEmbeds(pages) + }); + } catch (e) { + console.log(e) + return editOrReply(context, createEmbed("error", context, `Unable to perform google search.`)) + } + } +}; \ No newline at end of file diff --git a/commands/interaction/slash/search/google.js b/commands/interaction/slash/search/google.js new file mode 100644 index 0000000..8e40375 --- /dev/null +++ b/commands/interaction/slash/search/google.js @@ -0,0 +1,96 @@ +const { Constants } = require('detritus-client'); +const { InteractionCallbackTypes, ApplicationCommandOptionTypes } = Constants; + +const { createEmbed, page, formatPaginationEmbeds } = require('../../../../labscore/utils/embed'); + +const { paginator } = require('../../../../labscore/client'); +const { STATICS } = require('../../../../labscore/utils/statics'); +const { google } = require('../../../../labscore/api'); +const { link, citation } = require('../../../../labscore/utils/markdown'); +const { editOrReply } = require('../../../../labscore/utils/message'); + + +function createSearchResultPage(context, result){ + let res; + switch(result.type){ + case 1: // Search Result Entry + res = page(createEmbed("default", context, { + author: { + iconUrl: `https://www.google.com/s2/favicons?domain=${encodeURIComponent(result.url)}&sz=256`, + name: result.title, + url: result.url + }, + description: result.content, + footer: { + iconUrl: STATICS.google, + text: `Google • ${context.application.name}` + } + })) + + if(result.thumbnail) res.embeds[0].thumbnail = { url: result.thumbnail }; + break; + case 2: // Knowledge Graph Entry + let header = result.card.title; + if(result.card.url) header = link(result.card.url, result.card.title) + res = page(createEmbed("default", context, { + description: `**${header}**\n`, + footer: { + iconUrl: STATICS.google, + text: `Google Knowledge Graph • ${context.application.name}` + } + })) + + if(result.card.image) res.embeds[0].thumbnail = { url: result.card.image }; + if(result.card.description) res.embeds[0].description += `*${result.card.description}*\n` + if(result.card.content){ + let cnt = result.card.content.replace(/\n/g, '') + if(cnt.endsWith(" ")) cnt = cnt.substr(0,cnt.length - 1) + res.embeds[0].description += "\n" + cnt + citation(1, result.card.url, "Source") + } + break; + default: + res = page(createEmbed("error", context, "Unknown GoogleResult Type: " + result.type)) + break; + } + return res; +} + + +module.exports = { + description: 'Search on Google.', + name: 'google', + options: [ + { + name: 'query', + description: 'Search query', + type: ApplicationCommandOptionTypes.STRING, + required: true + } + ], + run: async (context, args) => { + await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE}) + + if(!args.query) return editOrReply(context, createEmbed("warning", context, `Missing Parameter (query).`)) + try{ + let search = await google(context, args.query, context.channel.nsfw) + search = search.response + + if(search.body.status == 2) return editOrReply(context, createEmbed("error", context, search.body.message)) + + let pages = [] + for(const res of search.body.results){ + pages.push(createSearchResultPage(context, res)) + } + + if(!pages.length) return editOrReply(context, createEmbed("warning", context, `No results found.`)) + + await paginator.createPaginator({ + context, + pages: formatPaginationEmbeds(pages) + }); + }catch(e){ + console.log(e) + return editOrReply(context, createEmbed("error", context, `Unable to perform google search.`)) + } + }, +}; \ No newline at end of file diff --git a/commands/interaction/slash/search/wolfram-alpha.js b/commands/interaction/slash/search/wolfram-alpha.js new file mode 100644 index 0000000..369ea0f --- /dev/null +++ b/commands/interaction/slash/search/wolfram-alpha.js @@ -0,0 +1,76 @@ +const { createEmbed, formatPaginationEmbeds, page } = require('../../../../labscore/utils/embed') +const { editOrReply } = require('../../../../labscore/utils/message') +const { STATICS } = require('../../../../labscore/utils/statics') + +const { paginator } = require('../../../../labscore/client'); +const { wolframAlpha } = require('../../../../labscore/api'); +const { citation } = require('../../../../labscore/utils/markdown'); +const { ApplicationCommandTypes, InteractionCallbackTypes } = require('detritus-client/lib/constants'); + +function createWolframPage(context, pod, query, sources) { + let res = page(createEmbed("default", context, { + author: { + name: pod.title, + url: `https://www.wolframalpha.com/input?i=${encodeURIComponent(query)}` + }, + description: undefined, + footer: { + iconUrl: STATICS.wolframalpha, + text: `Wolfram|Alpha • ${context.application.name}` + } + })) + if (pod.icon) res.embeds[0].author.iconUrl = pod.icon + if (pod.value) res.embeds[0].description = pod.value.substr(0, 1000) + if (pod.value && pod.refs) { + for (const r of pod.refs) { + let src = Object.values(sources).filter((s) => s.ref == r)[0] + if (!src) continue; + + // Only add a direct source if one is available + if (src.collections) { + res.embeds[0].description += citation(r, src.url, src.title + ' | ' + src.collections[0].text) + continue; + } + if (src.url) res.embeds[0].description += citation(r, src.url, src.title) + } + } + if (pod.image) res.embeds[0].image = { url: pod.image }; + return res; +} + +module.exports = { + description: 'Compute with Wolfram|Alpha.', + name: 'wolfram-alpha', + options: [ + { + name: 'query', + description: 'Computational query', + type: ApplicationCommandTypes.STRING, + required: true + } + ], + run: async (context, args) => { + await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE}) + + if (!args.query) return editOrReply(context, createEmbed("warning", context, `Missing Parameter (query).`)) + try { + let search = await wolframAlpha(context, args.query) + search = search.response + + if (search.body.status == 1) return editOrReply(context, createEmbed("warning", context, search.body.message)) + + let pages = [] + for (const res of search.body.data) { + pages.push(createWolframPage(context, res, args.query, search.body.sources)) + } + + await paginator.createPaginator({ + context, + pages: formatPaginationEmbeds(pages) + }); + } catch (e) { + console.log(e) + return editOrReply(context, createEmbed("error", context, `Unable to perform Wolfram|Alpha search.`)) + } + }, +}; \ No newline at end of file