pissbot-9000/commands/message/image/craiyon.js
2024-09-09 23:28:30 +02:00

72 lines
2.8 KiB
JavaScript

const { createEmbed } = require("#utils/embed");
const { editOrReply } = require("#utils/message");
const { STATICS } = require("#utils/statics");
// TODO: Turn this into a general purpose permissions constant
const { Permissions } = require("detritus-client/lib/constants");
const superagent = require('superagent');
const { link } = require("#utils/markdown");
module.exports = {
name: 'craiyon',
label: 'query',
metadata: {
description: 'Uses Craiyon to generate four images from a text prompt.',
description_short: 'Craiyon AI image generation',
examples: ['craiyon Otter'],
category: 'image',
usage: 'craiyon <text>'
},
ratelimit: {
type: 'guild',
limit: 1,
duration: 5000
},
permissionsClient: [Permissions.EMBED_LINKS, Permissions.SEND_MESSAGES, Permissions.USE_EXTERNAL_EMOJIS, Permissions.ATTACH_FILES, Permissions.READ_MESSAGE_HISTORY],
run: async (context, args) => {
return editOrReply(context, createEmbed("default", context, {
description: `Image manipulation commands are **no longer supported** in ${context.client.application.name}.\n\nIf you're looking for a suitable replacement, check out ${link("https://canary.discord.com/oauth2/authorize?client_id=571661221854707713", "Assyst", "Invite Assyst Bot")}.`,
image: {
url: "https://cdn.discordapp.com/attachments/1029331568315093043/1282814251005775902/attachment.gif?ex=66e0b95e&is=66df67de&hm=81087808fd9bcd31886050280cae535827c274efdf6d04a318a073c40dc5a554&"
}
}))
await editOrReply(context, createEmbed("loading", context, `Synthesizing images...`))
let noticeTimer = setTimeout(()=>{
let emb = createEmbed("loading", context, `Synthesizing images...`)
emb.footer = {
text: "This might take several minutes to complete."
};
editOrReply(context, { embeds: [ emb ] });
}, 30000)
try{
let t = Date.now();
let img = await superagent.post(`https://bf.dallemini.ai/generate`)
.send({
prompt: args.query
})
clearTimeout(noticeTimer)
let embeds = [];
let files = [];
for(let i = 0; i < 4; i++){
embeds.push(createEmbed("default", context, {image: {url:`attachment://dalle${i}.jpeg`}, url: `https://example.com`, footer: { iconUrl: STATICS.labscore, text: `${context.application.name} • Took ${((Date.now() - t) / 1000).toFixed(2)}s` }}))
files.push({
filename: `dalle${i}.jpeg`,
value: Buffer.from(img.body.images[i], 'base64')
})
}
await editOrReply(context, { embeds, files })
}catch(e){
if(e.response.status == 503) return await editOrReply(context, createEmbed("error", context, `DALL-E Mini server is busy, try again later.`))
await editOrReply(context, createEmbed("error", context, `Image generation failed.`))
}
},
};