major cleanup

This commit is contained in:
derpystuff 2023-11-04 17:29:06 +01:00
parent ddd918470e
commit a8cf49e31e
60 changed files with 537 additions and 619 deletions

View file

@ -1,4 +1,4 @@
const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed')
const { createEmbed, formatPaginationEmbeds, page } = require('../../../labscore/utils/embed')
const { link, pill } = require('../../../labscore/utils/markdown')
const { editOrReply } = require('../../../labscore/utils/message')
const { STATICS } = require('../../../labscore/utils/statics')
@ -10,32 +10,29 @@ const { Components } = require('detritus-client/lib/utils');
const { Permissions } = require("detritus-client/lib/constants");
function createQuoraAnswerPage(context, question, answer){
function createQuoraAnswerPage(context, question, answer) {
let tags = question.tags.map((t) => {
return pill(t)
})
let res = {
"embeds": [
createEmbed("default", context, {
title: question.title,
url: answer.url,
description: `${tags.splice(0, 3).join(' ')}\n\n${answer.content.substr(0,2000)}`,
footer: {
iconUrl: STATICS.quora,
text: `Quora • ${context.application.name}`
}
})
]
}
let res = page(
createEmbed("default", context, {
title: question.title,
url: answer.url,
description: `${tags.splice(0, 3).join(' ')}\n\n${answer.content.substr(0, 2000)}`,
footer: {
iconUrl: STATICS.quora,
text: `Quora • ${context.application.name}`
}
}))
if(answer.content.length >= 2000){
if(res.embeds[0].description.endsWith(' ')) res.embeds[0].description = res.embeds[0].description.substr(0,res.embeds[0].description.length - 1)
if (answer.content.length >= 2000) {
if (res.embeds[0].description.endsWith(' ')) res.embeds[0].description = res.embeds[0].description.substr(0, res.embeds[0].description.length - 1)
res.embeds[0].description += link(answer.url, '...', 'Click to read the entire answer.')
}
if(answer.author){
if (answer.author) {
res.embeds[0].author = {
name: answer.author.name.substr(0,1000),
name: answer.author.name.substr(0, 1000),
iconUrl: answer.author.icon,
url: answer.author.url
}
@ -43,7 +40,7 @@ function createQuoraAnswerPage(context, question, answer){
return res;
}
async function quoraPaginator(context, pages, refMappings, currentRef){
async function quoraPaginator(context, pages, refMappings, currentRef) {
const paging = await paginator.createPaginator({
context,
pages,
@ -55,7 +52,7 @@ async function quoraPaginator(context, pages, refMappings, currentRef){
});
paging.on("interaction", async ({ context: ctx, listener }) => {
if(ctx.customId == "search"){
if (ctx.customId == "search") {
// Kill the original paginator and replace it with a select
listener.stopWithoutUpdate()
@ -74,26 +71,26 @@ async function quoraPaginator(context, pages, refMappings, currentRef){
// Get the page reference and fetch the results
let ref = refMappings.filter((r) => r.ref == sctx.data.values[0])
ref = ref[0]
try{
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)]})
if (search.status == 2) return editOrReply(context, createEmbed("error", context, search.message))
let nextPages = []
// Create the initial page
for(const answer of search.answers){
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){
} catch (e) {
console.log(e)
return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform quora search.`)]})
return editOrReply(context, createEmbed("error", context, `Unable to perform quora search.`))
}
},
});
@ -112,7 +109,7 @@ async function quoraPaginator(context, pages, refMappings, currentRef){
options: selectOptions
})
await ctx.editOrRespond({content: `<@${context.userId}> Select a question.`, components})
await ctx.editOrRespond({ content: `<@${context.userId}> Select a question.`, components })
}
})
}
@ -131,29 +128,29 @@ module.exports = {
permissionsClient: [Permissions.EMBED_LINKS, Permissions.SEND_MESSAGES, Permissions.USE_EXTERNAL_EMOJIS, Permissions.READ_MESSAGE_HISTORY],
run: async (context, args) => {
context.triggerTyping();
if(!args.query) return editOrReply(context, {embeds:[createEmbed("warning", context, `Missing Parameter (query).`)]})
try{
if (!args.query) return editOrReply(context, createEmbed("warning", context, `Missing Parameter (query).`))
try {
let search = await quora(context, args.query)
search = search.response.body
if(search.status == 2) return editOrReply(context, {embeds:[createEmbed("error", context, search.message)]})
if (search.status == 2) return editOrReply(context, createEmbed("error", context, search.message))
let pages = []
// Create the initial page
for(const answer of search.answers){
for (const answer of search.answers) {
pages.push(createQuoraAnswerPage(context, search.question, answer))
}
pages = formatPaginationEmbeds(pages)
const refMappings = search.results
await quoraPaginator(context, pages, refMappings, refMappings[0].ref)
}catch(e){
} catch (e) {
console.log(e)
return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform quora search.`)]})
return editOrReply(context, createEmbed("error", context, `Unable to perform quora search.`))
}
},
};