|
| 1 | +import { Interaction } from "discord.js" |
| 2 | +import { isEmpty, isNil } from "ramda" |
| 3 | +import { Bot } from '../bot' |
| 4 | +import { BotEvent, QueueItem, QueueItemType } from '../types' |
| 5 | +import imageSelectPrompt from '../embeds/imageSelectPrompt' |
| 6 | +import { v4 as uuidv4 } from 'uuid' |
| 7 | +import addedToQueue, { QueueType } from "../embeds/addedToQueue" |
| 8 | + |
| 9 | +const botEvent: BotEvent = { |
| 10 | + name: 'Button Handler - Imagine Variant Of', |
| 11 | + event: 'interactionCreate', |
| 12 | + once: false, |
| 13 | + async execute(bot: Bot, interaction: Interaction) { |
| 14 | + if (!interaction.isButton() && !interaction.isSelectMenu()) return |
| 15 | + if (interaction.customId === 'image-select-prompt-variant' && interaction.isSelectMenu()) { |
| 16 | + // TODO: Lock this action to user who triggered prompt? |
| 17 | + |
| 18 | + const referenceQueueItem = bot.findLatestQueueItemReferenceByMessageID(interaction.message.id) |
| 19 | + if (isNil(referenceQueueItem) || isNil(referenceQueueItem.imageData) || isEmpty(referenceQueueItem.imageData)) { |
| 20 | + await interaction.reply({ |
| 21 | + ephemeral: true, |
| 22 | + content: 'There is no reference of this image generation. Most likely, this generation is too old to modify.' |
| 23 | + }) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + // Ensure PNG |
| 28 | + const selectedImageB64 = `data:image/png;base64,${referenceQueueItem.imageData[0].toString('base64')}` |
| 29 | + const queueItem: QueueItem = { |
| 30 | + ...referenceQueueItem, |
| 31 | + type: QueueItemType.Variant, |
| 32 | + prediction: { |
| 33 | + prompt: referenceQueueItem.prediction.prompt, |
| 34 | + promptStrength: 0.8, |
| 35 | + initImage: selectedImageB64, |
| 36 | + numOutputs: 4, |
| 37 | + height: referenceQueueItem.prediction.height, |
| 38 | + width: referenceQueueItem.prediction.width, |
| 39 | + guidanceScale: referenceQueueItem.prediction.guidanceScale, |
| 40 | + mask: referenceQueueItem.prediction.mask, |
| 41 | + numInferenceSteps: referenceQueueItem.prediction.numInferenceSteps |
| 42 | + }, |
| 43 | + discordCaller: interaction.user.id.toString(), |
| 44 | + uuid: uuidv4() |
| 45 | + } |
| 46 | + |
| 47 | + // Remove old attachment |
| 48 | + await interaction.message.removeAttachments() |
| 49 | + |
| 50 | + await interaction.reply({ |
| 51 | + ephemeral: true, |
| 52 | + content: 'Your image variants are being generated. The original message will be updated once complete.' |
| 53 | + }) |
| 54 | + |
| 55 | + if (bot.stableDiffusion.isProcessing() || bot.hasQueue()) { |
| 56 | + const queuePos = bot.addQueue(queueItem) |
| 57 | + await referenceQueueItem.interaction.editReply({ |
| 58 | + embeds: addedToQueue(QueueType.Queued, queueItem, queuePos).embeds, |
| 59 | + components: [] |
| 60 | + }) |
| 61 | + } else { |
| 62 | + bot.addQueue(queueItem) |
| 63 | + await referenceQueueItem.interaction.editReply({ |
| 64 | + embeds: addedToQueue(QueueType.Instant, queueItem).embeds, |
| 65 | + components: [] |
| 66 | + }) |
| 67 | + } |
| 68 | + } else if (interaction.customId === 'button-imagine-result-variant-of' && interaction.isButton()) { |
| 69 | + const referenceQueueItem = bot.findLatestQueueItemReferenceByMessageID(interaction.message.id) |
| 70 | + if (isNil(referenceQueueItem)) { |
| 71 | + await interaction.reply({ |
| 72 | + ephemeral: true, |
| 73 | + content: 'There is no reference of this image generation. Most likely, this generation is too old to modify.' |
| 74 | + }) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + const imageSelectPromptEmbed = imageSelectPrompt({...referenceQueueItem, type: QueueItemType.Variant}) |
| 79 | + |
| 80 | + await referenceQueueItem.interaction.editReply({ |
| 81 | + embeds: imageSelectPromptEmbed.embeds, |
| 82 | + components: imageSelectPromptEmbed.components |
| 83 | + }) |
| 84 | + } |
| 85 | + return |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export default botEvent |
0 commit comments