diff --git a/commands/message/search/google.js b/commands/message/search/google.js index ca13633..7ff2079 100644 --- a/commands/message/search/google.js +++ b/commands/message/search/google.js @@ -1,5 +1,5 @@ -const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed') -const { link } = require('../../../labscore/utils/markdown') +const { createEmbed, formatPaginationEmbeds, page } = require('../../../labscore/utils/embed') +const { link, pill, citation } = require('../../../labscore/utils/markdown') const { editOrReply } = require('../../../labscore/utils/message') const { STATICS } = require('../../../labscore/utils/statics') @@ -7,19 +7,38 @@ const { paginator } = require('../../../labscore/client'); const { google } = require('../../../labscore/api'); function createSearchResultPage(context, result){ - let res = { - "embeds": [ - createEmbed("default", context, { - description: `**${link(result.url, result.title)}**\n${result.content}`, - footer: { - iconUrl: STATICS.google, - text: `Google • ${context.application.name}` - } - }) - ] + let res; + switch(result.type){ + case 1: // Search Result Entry + res = page(createEmbed("default", context, { + description: `**${link(result.url, result.title)}**\n${result.content}`, + footer: { + iconUrl: STATICS.google, + text: `Google • ${context.application.name}` + } + })) + if(result.thumbnail) res.embeds[0].thumbnail = { url: result.thumbnail }; + return res; + 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*${result.card.description}*\n\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.content) res.embeds[0].description += result.card.content.replace(/\n/g, '') + citation(1, result.card.url, "Source") + return res; + break; + default: + res = page(createEmbed("error", context, "Unknown GoogleResult Type: " + result.type)) + return res; + break; } - if(result.thumbnail) res.embeds[0].thumbnail = { url: result.thumbnail }; - return res; } module.exports = { diff --git a/labscore/utils/markdown.js b/labscore/utils/markdown.js index 966a830..bc3accc 100644 --- a/labscore/utils/markdown.js +++ b/labscore/utils/markdown.js @@ -34,4 +34,15 @@ module.exports.smallPill = function(content){ module.exports.iconPill = function(icon, content){ if(!ICONS[icon]) icon = "question" return ICONS[icon] + " **` " + content + " `**" +} + +const SUPERSCRIPT_NUMBERS = ["⁰","¹","²","³","⁴","⁵","⁶","⁷","⁸","⁹"] +module.exports.citation = function(number = 1, url, tooltip = ""){ + let formatted = ""; + for(const n of number.toString().split('')) formatted += SUPERSCRIPT_NUMBERS[parseInt(n)] + if(url){ + if(tooltip.length) tooltip = ` '${tooltip}'` + return `[⁽${formatted}⁾](${url}${tooltip})` + } + return `⁽${formatted}⁾` } \ No newline at end of file