mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-16 01:53:08 -04:00
user command support
This commit is contained in:
parent
af5a7f4252
commit
86936dc100
14 changed files with 710 additions and 11 deletions
74
commands/interaction/slash/search/google-images.js
Normal file
74
commands/interaction/slash/search/google-images.js
Normal file
|
@ -0,0 +1,74 @@
|
|||
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 { googleImages } = require('../../../../labscore/api');
|
||||
|
||||
const { ApplicationCommandOptionTypes, InteractionCallbackTypes } = require('detritus-client/lib/constants');
|
||||
|
||||
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 = {
|
||||
name: 'image',
|
||||
label: 'query',
|
||||
aliases: ['i', 'img'],
|
||||
description: 'Search the web for images on Google.',
|
||||
contexts: [
|
||||
0,
|
||||
1,
|
||||
2
|
||||
],
|
||||
integrationTypes: [
|
||||
1
|
||||
],
|
||||
options: [
|
||||
{
|
||||
name: 'query',
|
||||
description: 'Image 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 googleImages(context, args.query, false) //safesearch 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(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.`))
|
||||
}
|
||||
},
|
||||
};
|
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.`))
|
||||
}
|
||||
},
|
||||
};
|
102
commands/interaction/slash/search/lyrics.js
Normal file
102
commands/interaction/slash/search/lyrics.js
Normal file
|
@ -0,0 +1,102 @@
|
|||
const { createEmbed, formatPaginationEmbeds, page } = require('../../../../labscore/utils/embed')
|
||||
const { editOrReply } = require('../../../../labscore/utils/message')
|
||||
const { STATICS } = require('../../../../labscore/utils/statics')
|
||||
|
||||
const { lyrics } = require('../../../../labscore/api');
|
||||
const { paginator } = require('../../../../labscore/client');
|
||||
|
||||
const { ApplicationCommandOptionTypes, InteractionCallbackTypes } = require('detritus-client/lib/constants');
|
||||
const { smallIconPill } = require('../../../../labscore/utils/markdown');
|
||||
|
||||
const META_FIELDS = {
|
||||
"Album": "stat_videos",
|
||||
"Released": "note"
|
||||
}
|
||||
|
||||
function renderMetadata(metadata){
|
||||
const pills = []
|
||||
for(const m of metadata){
|
||||
if(!META_FIELDS[m.id]) continue;
|
||||
pills.push(smallIconPill(META_FIELDS[m.id], `${m.id}: ${m.value}`))
|
||||
}
|
||||
return pills.join(' ')
|
||||
}
|
||||
|
||||
function createLyricsPage(context, search, fields){
|
||||
let em = createEmbed("default", context, {
|
||||
author: {
|
||||
iconUrl: search.body.track.artist_cover,
|
||||
name: `${search.body.track.title} by ${search.body.track.artist}`
|
||||
},
|
||||
fields: fields,
|
||||
footer: {
|
||||
iconUrl: STATICS.musixmatch,
|
||||
text: `Musixmatch • ${context.application.name}`
|
||||
}
|
||||
})
|
||||
if(search.body.track.cover) em.thumbnail = { url: search.body.track.cover }
|
||||
if(search.body.track.metadata.length) em.description = renderMetadata(search.body.track.metadata)
|
||||
return em;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
name: 'lyrics',
|
||||
description: 'Search for song lyrics.',
|
||||
contexts: [
|
||||
0,
|
||||
1,
|
||||
2
|
||||
],
|
||||
integrationTypes: [
|
||||
1
|
||||
],
|
||||
options: [
|
||||
{
|
||||
name: 'query',
|
||||
description: 'Song name or partial lyrics.',
|
||||
type: ApplicationCommandOptionTypes.TEXT,
|
||||
required: true
|
||||
}
|
||||
],
|
||||
run: async (context, args) => {
|
||||
await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE})
|
||||
|
||||
try{
|
||||
let search = await lyrics(context, args.query)
|
||||
search = search.response
|
||||
|
||||
if(search.body.status == 2) return editOrReply(context, createEmbed("error", context, search.body.message))
|
||||
let fields = [];
|
||||
|
||||
for(const f of search.body.lyrics.split('\n\n')){
|
||||
fields.push({
|
||||
name: '',
|
||||
value: f,
|
||||
inline: false
|
||||
})
|
||||
}
|
||||
|
||||
let pages = []
|
||||
while(fields.length) {
|
||||
let pageFields = fields.splice(0,3)
|
||||
|
||||
// Display less fields if they take up too much vertical space
|
||||
while(pageFields.map((f)=>f.value).join('\n').split('\n').length >= 30 && pageFields[1]){
|
||||
fields.unshift(pageFields[pageFields.length - 1])
|
||||
pageFields = pageFields.splice(0, pageFields.length - 1)
|
||||
}
|
||||
|
||||
pages.push(page(createLyricsPage(context, search, pageFields)))
|
||||
}
|
||||
|
||||
await paginator.createPaginator({
|
||||
context,
|
||||
pages: formatPaginationEmbeds(pages)
|
||||
});
|
||||
}catch(e){
|
||||
if(e.response?.body?.status && e.response.body.status == 2 && e.response.body.message) return editOrReply(context, createEmbed("error", context, e.response.body.message))
|
||||
console.log(JSON.stringify(e.raw))
|
||||
return editOrReply(context, createEmbed("error", context, `Something went wrong.`))
|
||||
}
|
||||
},
|
||||
};
|
84
commands/interaction/slash/search/wolfram-alpha.js
Normal file
84
commands/interaction/slash/search/wolfram-alpha.js
Normal file
|
@ -0,0 +1,84 @@
|
|||
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 { ApplicationCommandOptionTypes, 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 = {
|
||||
name: 'wolframalpha',
|
||||
description: 'Compute a query via Wolfram|Alpha.',
|
||||
contexts: [
|
||||
0,
|
||||
1,
|
||||
2
|
||||
],
|
||||
integrationTypes: [
|
||||
1
|
||||
],
|
||||
options: [
|
||||
{
|
||||
name: 'query',
|
||||
description: 'Computational 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 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.`))
|
||||
}
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue