show different drafts on bard

This commit is contained in:
derpystuff 2023-12-02 01:49:44 +01:00
parent b19f753ae0
commit a0a0e2d492
8 changed files with 103 additions and 12 deletions

View file

@ -8,6 +8,7 @@ const superagent = require('superagent')
const { iconPill, stringwrap } = require('../../../labscore/utils/markdown')
const { Permissions } = require("detritus-client/lib/constants");
const { Components } = require('detritus-client/lib/utils');
module.exports = {
name: 'bard',
@ -36,6 +37,9 @@ module.exports = {
.set({
Authorization: process.env.AI_SERVER_KEY
})
.query({
with_drafts: true
})
.send({
input
})
@ -53,7 +57,7 @@ module.exports = {
})
}
return editOrReply(context, {
if(!res.body.drafts || res.body.drafts?.length <= 1) return editOrReply(context, {
embeds:[createEmbed("defaultNoFooter", context, {
author: {
name: stringwrap(args.text, 50),
@ -66,6 +70,92 @@ module.exports = {
})],
files
})
// Draft support
else {
let currentView;
const components = new Components({
timeout: 100000,
run: async (ctx) => {
if (ctx.userId !== context.userId) return await ctx.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE);
// this sucks but works, ensures the newly selected option stays selected
for (let i = 0; i < components.components[0].components[0].options.length; i++) {
components.components[0].components[0].options[i].default = (components.components[0].components[0].options[i].value == ctx.data.values[0])
}
draft = res.body.drafts[parseInt(ctx.data.values[0].replace('draft-', ''))]
description = []
files = [];
if(draft.length <= 4000) description.push(draft)
else {
files.push({
filename: `chat.${Date.now().toString(36)}.txt`,
value: Buffer.from(draft)
})
}
currentView = createEmbed("defaultNoFooter", context, {
author: {
name: stringwrap(args.text, 50, false),
iconUrl: STATIC_ICONS.ai_bard_idle
},
description: description.join('\n'),
footer: {
text: `Bard • This information may be inaccurate or biased`
}
})
await ctx.editOrRespond({
embeds:[currentView],
files,
components
})
}
})
let draftOptions = [];
for (let i = 1; i < res.body.drafts.length; i++) {
draftOptions.push({
label: `Draft ${i}: ${stringwrap(res.body.drafts[i], 50, false)}`,
value: "draft-" + (i - 1),
default: false
})
}
components.addSelectMenu({
placeholder: "View other drafts",
customId: "bard-drafts",
options: draftOptions
})
setTimeout(()=>{
editOrReply(context, {
embeds:[currentView],
components:[]
})
}, 100000)
currentView = createEmbed("defaultNoFooter", context, {
author: {
name: stringwrap(args.text, 50, false),
iconUrl: STATIC_ICONS.ai_bard_idle
},
description: description.join('\n'),
footer: {
text: `Bard • This information may be inaccurate or biased`
}
})
return editOrReply(context, {
embeds:[currentView],
files,
components
})
}
}catch(e){
if(e.response?.body?.message) return editOrReply(context, createEmbed("warning", context, e.response.body.message))