mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-17 18:37:07 -04:00
167 lines
No EOL
5.3 KiB
JavaScript
167 lines
No EOL
5.3 KiB
JavaScript
const { google } = require('#api');
|
|
const { paginator } = require('#client');
|
|
|
|
const { createEmbed, formatPaginationEmbeds, page } = require('#utils/embed');
|
|
const { acknowledge } = require('#utils/interactions');
|
|
const { link, citation, icon } = require('#utils/markdown')
|
|
const { editOrReply } = require('#utils/message')
|
|
const { STATICS } = require('#utils/statics')
|
|
|
|
const { ApplicationCommandOptionTypes } = require('detritus-client/lib/constants');
|
|
|
|
function renderFooter(context, doodle){
|
|
if(doodle.label) return {
|
|
iconUrl: doodle.super_g,
|
|
text: `${doodle.label} • Google`
|
|
}
|
|
|
|
return {
|
|
iconUrl: STATICS.google,
|
|
text: `Google • ${context.application.name}`
|
|
}
|
|
}
|
|
|
|
// These have to be synced with the backend (search_service/endpoints/google).
|
|
const SEARCH_CARD_TYPES = {
|
|
UNKNONW: 0,
|
|
SEARCH_RESULT: 1,
|
|
KNOWLEDGE_GRAPH: 2,
|
|
DOODLE: 3,
|
|
ENTITY: 4
|
|
}
|
|
|
|
// TODO: create a favicon() util
|
|
function createSearchResultPage(context, result, doodle){
|
|
let res;
|
|
switch(result.type){
|
|
case SEARCH_CARD_TYPES.SEARCH_RESULT:
|
|
res = 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: renderFooter(context, doodle)
|
|
})
|
|
|
|
if(result.thumbnail) res.thumbnail = { url: result.thumbnail };
|
|
|
|
break;
|
|
case SEARCH_CARD_TYPES.KNOWLEDGE_GRAPH:
|
|
let header = result.card.title;
|
|
if(result.card.url) header = link(result.card.url, result.card.title)
|
|
res = createEmbed("default", context, {
|
|
description: `### ${header}\n`,
|
|
footer: renderFooter(context, doodle)
|
|
})
|
|
|
|
if(result.card.image) res.thumbnail = { url: result.card.image };
|
|
if(result.card.description) res.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.description += "\n" + cnt + citation(1, result.card.url, "Source")
|
|
}
|
|
break;
|
|
case SEARCH_CARD_TYPES.DOODLE:
|
|
res = createEmbed("default", context, {
|
|
description: `### ${result.card.title}\n${result.card.description}\n\n${link(result.card.learn_more, `Learn More ${icon("link_open_external")}`, "Learn more about this Doodle")}`,
|
|
thumbnail: {
|
|
url: result.card.images.thumbnail
|
|
},
|
|
image: {
|
|
url: result.card.images.image
|
|
},
|
|
footer: renderFooter(context, doodle)
|
|
})
|
|
break;
|
|
case SEARCH_CARD_TYPES.ENTITY:
|
|
res = createEmbed("default", context, {
|
|
author: {
|
|
name: result.card.title
|
|
},
|
|
thumbnail: {
|
|
url: result.card.images.thumbnail
|
|
},
|
|
description: `-# ${result.card.type}`,
|
|
footer: renderFooter(context, doodle)
|
|
})
|
|
|
|
if(!result.card.images.preview && result.card.link == ""){
|
|
delete res.author
|
|
res.description = `### ${result.card.title}\n-# ${result.card.type}`
|
|
}
|
|
|
|
if(result.card.color !== "") res.color = parseInt(result.card.color.substring(1,10), 16)
|
|
|
|
if(result.card.description !== ""){
|
|
res.description += "\n\n" + result.card.description
|
|
if(result.card.source) res.description += citation(1, result.card.source, "Source") + "\n\n" + link(result.card.source, `Learn More ${icon("link_open_external")}`, "Learn more about this topic");
|
|
else if(result.card.link !== "") res.description += "\n\n" + link(result.card.link, `Learn More ${icon("link_open_external")}`, "Learn more about this topic");
|
|
}
|
|
|
|
if(result.card.images.preview) res.author.iconUrl = result.card.images.preview;
|
|
|
|
if(result.card.link) res.author.url = result.card.link
|
|
|
|
break;
|
|
default:
|
|
res = createEmbed("error", context, "Unknown GoogleResult Type: " + result.type)
|
|
break;
|
|
}
|
|
return page(res);
|
|
}
|
|
|
|
module.exports = {
|
|
name: 'google',
|
|
description: 'Search Google for websites.',
|
|
contexts: [
|
|
0,
|
|
1,
|
|
2
|
|
],
|
|
integrationTypes: [
|
|
1
|
|
],
|
|
options: [
|
|
{
|
|
name: 'query',
|
|
description: 'Google search query.',
|
|
type: ApplicationCommandOptionTypes.TEXT,
|
|
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);
|
|
|
|
try{
|
|
let search = await google(context, args.query, false) // safe search is always on
|
|
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, search.body.doodle))
|
|
}
|
|
|
|
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.`))
|
|
}
|
|
},
|
|
}; |