mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-08 22:23:03 -04:00
update quora command to allow for more questions
This commit is contained in:
parent
94bee05892
commit
2b618e536e
5 changed files with 100 additions and 7 deletions
|
@ -4,7 +4,9 @@ const { editOrReply } = require('../../../labscore/utils/message')
|
||||||
const { STATICS } = require('../../../labscore/utils/statics')
|
const { STATICS } = require('../../../labscore/utils/statics')
|
||||||
|
|
||||||
const { paginator } = require('../../../labscore/client');
|
const { paginator } = require('../../../labscore/client');
|
||||||
const { quora } = require('../../../labscore/api');
|
const { quora, quoraResult } = require('../../../labscore/api');
|
||||||
|
const { InteractionCallbackTypes } = require('detritus-client/lib/constants');
|
||||||
|
const { Components } = require('detritus-client/lib/utils');
|
||||||
|
|
||||||
function createQuoraAnswerPage(context, question, answer){
|
function createQuoraAnswerPage(context, question, answer){
|
||||||
let tags = question.tags.map((t) => {
|
let tags = question.tags.map((t) => {
|
||||||
|
@ -39,6 +41,80 @@ function createQuoraAnswerPage(context, question, answer){
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function quoraPaginator(context, pages, refMappings, currentRef){
|
||||||
|
const paging = await paginator.createPaginator({
|
||||||
|
context,
|
||||||
|
pages,
|
||||||
|
buttons: [
|
||||||
|
"previous",
|
||||||
|
"next",
|
||||||
|
"search"
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
paging.on("interaction", async ({ context: ctx, listener }) => {
|
||||||
|
if(ctx.customId == "search"){
|
||||||
|
// Kill the original paginator and replace it with a select
|
||||||
|
listener.stopWithoutUpdate()
|
||||||
|
|
||||||
|
const components = new Components({
|
||||||
|
timeout: 10000,
|
||||||
|
run: async (sctx) => {
|
||||||
|
if (sctx.userId !== context.userId || !context.values) {
|
||||||
|
return await sctx.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE);
|
||||||
|
}
|
||||||
|
await sctx.editOrRespond({
|
||||||
|
embeds: [
|
||||||
|
createEmbed("loading", context, "Loading quora result...")
|
||||||
|
],
|
||||||
|
components: []
|
||||||
|
})
|
||||||
|
// Get the page reference and fetch the results
|
||||||
|
let ref = refMappings.filter((r) => r.ref == sctx.data.values[0])
|
||||||
|
ref = ref[0]
|
||||||
|
try{
|
||||||
|
let search = await quoraResult(context, ref.link)
|
||||||
|
search = search.response.body
|
||||||
|
|
||||||
|
if(search.status == 2) return editOrReply(context, {embeds:[createEmbed("error", context, search.message)]})
|
||||||
|
|
||||||
|
let nextPages = []
|
||||||
|
// Create the initial page
|
||||||
|
|
||||||
|
for(const answer of search.answers){
|
||||||
|
nextPages.push(createQuoraAnswerPage(context, search.question, answer))
|
||||||
|
}
|
||||||
|
|
||||||
|
nextPages = formatPaginationEmbeds(nextPages)
|
||||||
|
|
||||||
|
await quoraPaginator(context, nextPages, refMappings, sctx.data.values[0])
|
||||||
|
|
||||||
|
}catch(e){
|
||||||
|
console.log(e)
|
||||||
|
return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform quora search.`)]})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
let selectOptions = refMappings.map((r) => {
|
||||||
|
return {
|
||||||
|
label: r.title,
|
||||||
|
value: r.ref,
|
||||||
|
default: (r.ref == currentRef)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
components.addSelectMenu({
|
||||||
|
placeholder: "Select a question.",
|
||||||
|
customId: "quora-select",
|
||||||
|
options: selectOptions
|
||||||
|
})
|
||||||
|
|
||||||
|
await ctx.editOrRespond({components})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'quora',
|
name: 'quora',
|
||||||
label: 'query',
|
label: 'query',
|
||||||
|
@ -67,10 +143,11 @@ module.exports = {
|
||||||
}
|
}
|
||||||
|
|
||||||
pages = formatPaginationEmbeds(pages)
|
pages = formatPaginationEmbeds(pages)
|
||||||
const paging = await paginator.createPaginator({
|
|
||||||
context,
|
const refMappings = search.results
|
||||||
pages
|
|
||||||
});
|
await quoraPaginator(context, pages, refMappings, refMappings[0].ref)
|
||||||
|
|
||||||
}catch(e){
|
}catch(e){
|
||||||
console.log(e)
|
console.log(e)
|
||||||
return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform quora search.`)]})
|
return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform quora search.`)]})
|
||||||
|
|
|
@ -32,6 +32,7 @@ const Api = Object.freeze({
|
||||||
SEARCH_GOOGLE_IMAGES: '/search/google-images',
|
SEARCH_GOOGLE_IMAGES: '/search/google-images',
|
||||||
SEARCH_LYRICS: '/search/lyrics',
|
SEARCH_LYRICS: '/search/lyrics',
|
||||||
SEARCH_QUORA: '/search/quora',
|
SEARCH_QUORA: '/search/quora',
|
||||||
|
SEARCH_QUORA_RESULT: '/search/quora-result',
|
||||||
SEARCH_REDDIT: '/search/reddit',
|
SEARCH_REDDIT: '/search/reddit',
|
||||||
SEARCH_REVERSE_IMAGE: '/search/reverse-image',
|
SEARCH_REVERSE_IMAGE: '/search/reverse-image',
|
||||||
SEARCH_RULE34: '/search/booru',
|
SEARCH_RULE34: '/search/booru',
|
||||||
|
|
|
@ -124,6 +124,12 @@ module.exports.quora = async function(context, query){
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.quoraResult = async function(context, reference){
|
||||||
|
return await request(Api.SEARCH_QUORA_RESULT, "GET", {}, {
|
||||||
|
ref: reference
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.reddit = async function(context, query, nsfw = false){
|
module.exports.reddit = async function(context, query, nsfw = false){
|
||||||
return await request(Api.SEARCH_REDDIT, "GET", {}, {
|
return await request(Api.SEARCH_REDDIT, "GET", {}, {
|
||||||
q: query,
|
q: query,
|
||||||
|
|
|
@ -133,13 +133,21 @@ module.exports = class BasePaginator extends EventEmitter {
|
||||||
this.pages.push(page)
|
this.pages.push(page)
|
||||||
}
|
}
|
||||||
|
|
||||||
stop(timeout = false) {
|
async stop(timeout = false) {
|
||||||
this.emit("stop", this, timeout);
|
this.emit("stop", this, timeout);
|
||||||
this.removeAllListeners();
|
this.removeAllListeners();
|
||||||
const targetIndex = this.client.activeListeners.findIndex(v => v.message.id === this.message.id);
|
const targetIndex = this.client.activeListeners.findIndex(v => v.message.id === this.message.id);
|
||||||
this.client.activeListeners.splice(targetIndex, 1);
|
this.client.activeListeners.splice(targetIndex, 1);
|
||||||
// Disable components
|
// Disable components
|
||||||
this.update({components:[]});
|
await this.update({components:[]});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
stopWithoutUpdate(timeout = false) {
|
||||||
|
this.emit("stop", this, timeout);
|
||||||
|
this.removeAllListeners();
|
||||||
|
const targetIndex = this.client.activeListeners.findIndex(v => v.message.id === this.message.id);
|
||||||
|
this.client.activeListeners.splice(targetIndex, 1);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -13,6 +13,7 @@ const ButtonEmoji = Object.freeze({
|
||||||
NEXT: '<:right:977871577758707782>',
|
NEXT: '<:right:977871577758707782>',
|
||||||
PREVIOUS: '<:left:977871577532211200>',
|
PREVIOUS: '<:left:977871577532211200>',
|
||||||
STOP: '<:ico_trash:929498022386221096>',
|
STOP: '<:ico_trash:929498022386221096>',
|
||||||
|
SEARCH: '<:search:1063080546365866056>',
|
||||||
UNKNOWN: '<:ico_question:949420315677691934>'
|
UNKNOWN: '<:ico_question:949420315677691934>'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue