|
| 1 | +import { AttachmentBuilder, Interaction } from "discord.js" |
| 2 | +import { isEmpty, isNil } from "ramda" |
| 3 | +import { Bot } from '../bot' |
| 4 | +import { BotEvent, QueueItemType } from '../types' |
| 5 | + |
| 6 | +import imageResult from "../embeds/imageResult" |
| 7 | +import imageSelectPrompt from "../embeds/imageSelectPrompt" |
| 8 | + |
| 9 | +const botEvent: BotEvent = { |
| 10 | + name: 'Button Handler - Imagine Upscale', |
| 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-upscaled' && 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 | + // We can cheat here, we don't really need to generate anything. We just take the image out of the collage. |
| 28 | + // Maybe we add ai upscaling here in the future? |
| 29 | + const [selectedImage] = interaction.values |
| 30 | + const imageResultEmbed = imageResult({...referenceQueueItem, type: QueueItemType.Upscaled}) |
| 31 | + |
| 32 | + const file = new AttachmentBuilder(referenceQueueItem.imageData[+selectedImage], { |
| 33 | + name: `stable-confusion_${referenceQueueItem.uuid}_upscaled.jpeg`, |
| 34 | + description: referenceQueueItem.prediction.prompt |
| 35 | + }) |
| 36 | + |
| 37 | + await referenceQueueItem.interaction.editReply({ |
| 38 | + embeds: imageResultEmbed.embeds, |
| 39 | + components: imageResultEmbed.components, |
| 40 | + files: [file] |
| 41 | + }) |
| 42 | + } else if (interaction.customId === 'button-imagine-result-upscale' && interaction.isButton()) { |
| 43 | + const referenceQueueItem = bot.findLatestQueueItemReferenceByMessageID(interaction.message.id) |
| 44 | + if (isNil(referenceQueueItem)) { |
| 45 | + await interaction.reply({ |
| 46 | + ephemeral: true, |
| 47 | + content: 'There is no reference of this image generation. Most likely, this generation is too old to modify.' |
| 48 | + }) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + const imageSelectPromptEmbed = imageSelectPrompt({...referenceQueueItem, type: QueueItemType.Upscaled}) |
| 53 | + |
| 54 | + await referenceQueueItem.interaction.editReply({ |
| 55 | + embeds: imageSelectPromptEmbed.embeds, |
| 56 | + components: imageSelectPromptEmbed.components |
| 57 | + }) |
| 58 | + } |
| 59 | + return |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export default botEvent |
0 commit comments