mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-13 16:43:05 -04:00
user command support
This commit is contained in:
parent
af5a7f4252
commit
86936dc100
14 changed files with 710 additions and 11 deletions
100
commands/interaction/slash/search/google.js
Normal file
100
commands/interaction/slash/search/google.js
Normal file
|
@ -0,0 +1,100 @@
|
|||
const { createEmbed, formatPaginationEmbeds, page } = require('../../../../labscore/utils/embed')
|
||||
const { link, citation } = require('../../../../labscore/utils/markdown')
|
||||
const { editOrReply } = require('../../../../labscore/utils/message')
|
||||
const { STATICS } = require('../../../../labscore/utils/statics')
|
||||
|
||||
const { paginator } = require('../../../../labscore/client');
|
||||
const { google } = require('../../../../labscore/api');
|
||||
|
||||
const { ApplicationCommandOptionTypes, InteractionCallbackTypes } = require('detritus-client/lib/constants');
|
||||
|
||||
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 = {
|
||||
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
|
||||
}
|
||||
],
|
||||
run: async (context, args) => {
|
||||
await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE})
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
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.`))
|
||||
}
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue