|
| 1 | +import { Interaction } from 'discord.js' |
| 2 | +import { isNil } from 'ramda' |
| 3 | +import { Bot } from '../bot' |
| 4 | +import { BotEvent, QueueItem } from '../types' |
| 5 | +import { getRandomInt } from '../utils' |
| 6 | +import { v4 as uuidv4 } from 'uuid' |
| 7 | +import addedToQueue from '../embeds/addedToQueue' |
| 8 | +import addedToQueueNoWait from '../embeds/addedToQueueNoWait' |
| 9 | + |
| 10 | +const botEvent: BotEvent = { |
| 11 | + name: 'Command Handler - Ping', |
| 12 | + event: 'interactionCreate', |
| 13 | + once: false, |
| 14 | + async execute(bot: Bot, interaction: Interaction) { |
| 15 | + if (!interaction.isButton()) return |
| 16 | + if (interaction.customId !== 'button-imagine-result-regenerate') return |
| 17 | + |
| 18 | + const referenceQueueItem = bot.findQueueItemReferenceByMessageID(interaction.message.id) |
| 19 | + if (isNil(referenceQueueItem)) { |
| 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 | + }).then(message => { |
| 24 | + // TODO: Remove message after 5 seconds |
| 25 | + // setTimeout(async () => { |
| 26 | + // if (!message.interaction.isRepliable) return |
| 27 | + // const msg = await message.awaitMessageComponent() |
| 28 | + // await msg.deleteReply() |
| 29 | + // }, 5000) |
| 30 | + }) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + const queueItem: QueueItem = { |
| 35 | + ...referenceQueueItem, |
| 36 | + discordCaller: interaction.user.id.toString(), |
| 37 | + seed: getRandomInt(1, 99999999), |
| 38 | + uuid: uuidv4() |
| 39 | + } |
| 40 | + |
| 41 | + bot.log.debug(`isProcessing: ${bot.stableDiffusion.isProcessing()} hasQueue: ${bot.hasQueue()} queueLength: ${bot.queue.length}`) |
| 42 | + |
| 43 | + // Remove old attachment |
| 44 | + await interaction.message.removeAttachments() |
| 45 | + |
| 46 | + await interaction.reply({ |
| 47 | + ephemeral: true, |
| 48 | + content: 'Your images are being regenerated. The original message will be updated once complete.' |
| 49 | + }).then(message => { |
| 50 | + // TODO: Remove message after 5 seconds |
| 51 | + // setTimeout(async () => { |
| 52 | + // if (!message.interaction.isRepliable) return |
| 53 | + // const msg = await message.awaitMessageComponent() |
| 54 | + // await msg.deleteReply() |
| 55 | + // }, 5000) |
| 56 | + }) |
| 57 | + |
| 58 | + |
| 59 | + if (bot.stableDiffusion.isProcessing() || bot.hasQueue()) { |
| 60 | + const queuePos = bot.addQueue(queueItem) |
| 61 | + |
| 62 | + await referenceQueueItem.interaction.editReply({ |
| 63 | + embeds: addedToQueue(queuePos, queueItem).embeds |
| 64 | + }) |
| 65 | + } else { |
| 66 | + bot.addQueue(queueItem) |
| 67 | + |
| 68 | + await referenceQueueItem.interaction.editReply({ |
| 69 | + embeds: addedToQueueNoWait(queueItem).embeds |
| 70 | + }) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export default botEvent |
0 commit comments