diff --git a/commands/message/image/stablediffusion.js b/commands/message/image/stablediffusion.js new file mode 100644 index 0000000..27574d5 --- /dev/null +++ b/commands/message/image/stablediffusion.js @@ -0,0 +1,61 @@ +const { createEmbed } = require("../../../labscore/utils/embed"); +const { editOrReply } = require("../../../labscore/utils/message"); + +const superagent = require('superagent'); + +module.exports = { + name: 'stability', + label: 'query', + aliases: ['genimg'], + metadata: { + description: 'Uses Stable Diffusion to generate four images from a text prompt.', + description_short: 'Stable Diffusion image generation', + examples: ['genimg Otter, digital art'], + category: 'image', + usage: 'stability ' + }, + ratelimit: { + type: 'guild', + limit: 1, + duration: 5000 + }, + run: async (context, args) => { + let response = await editOrReply(context, { embeds: [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." + }; + response.edit({ embeds: [ emb ] }); + }, 45000) + + try{ + let t = Date.now(); + + let img = await superagent.get(`${process.env.STABLE_DIFFUSION_SERVER}/generate`) + .query({ + prompt: args.query + }) + + clearTimeout(noticeTimer) + + if(img.body.message) return await response.edit({embeds:[createEmbed("warning", context, img.body.message)]}) + + let embeds = []; + let files = []; + + for(let i = 0; i < 4; i++){ + embeds.push(createEmbed("default", context, {image: {url:`attachment://stability${i}.jpeg`}, url: `https://example.com`, footer: { iconUrl: `https://cdn.discordapp.com/avatars/${context.application.id}/${context.application.icon}.png?size=256`, text: `${context.application.name} • Took ${((Date.now() - t) / 1000).toFixed(2)}s` }})) + files.push({ + filename: `stability${i}.jpeg`, + value: Buffer.from(img.body.images[i].replace('data:image/jpeg;base64,',''), 'base64') + }) + } + + await response.edit({ embeds, files }) + }catch(e){ + await response.edit({embeds:[createEmbed("error", context, `Image generation failed.`)]}) + } + }, +}; \ No newline at end of file