81 lines
3.3 KiB
JavaScript
81 lines
3.3 KiB
JavaScript
const { submitImagePromptTransaction, DALLE_COLOR } = require("../controllers/openAiController");
|
|
const { SlashCommandBuilder } = require('discord.js');
|
|
const { DebugBuilder } = require("../utilities/debugBuilder");
|
|
const log = new DebugBuilder("server", "imagine");
|
|
const { EmmeliaEmbedBuilder } = require('../libUtils');
|
|
|
|
const COST_OF_COMMAND = 800;
|
|
|
|
module.exports = {
|
|
data: new SlashCommandBuilder()
|
|
.setName('imagine')
|
|
.setDescription(`Submit an image generation prompt to DALL-E`)
|
|
.addStringOption(option =>
|
|
option.setName('prompt')
|
|
.setDescription('The prompt to be sent to DALL-E')
|
|
.setRequired(true))
|
|
.addBooleanOption(option =>
|
|
option.setName('public')
|
|
.setDescription("Set this to false if you would like the message to only be visible to you. *defaults to public*")
|
|
.setRequired(false))
|
|
.addNumberOption(option =>
|
|
option.setName('images')
|
|
.setDescription('The number of images you wish to generate [1 - 10] *(defaults to 1)*')
|
|
.setRequired(false))
|
|
.addStringOption(option =>
|
|
option.setName('size')
|
|
.setDescription('The size of the images to be generated *defaults to 256px*')
|
|
.addChoices(
|
|
{ name: '1024px - 1000 tokens', value: '1024x1024' },
|
|
{ name: '512px - 900 tokens', value: '512x512' },
|
|
{ name: '256px - 800 tokens', value: '256x256' },
|
|
)
|
|
.setRequired(false)),
|
|
example: "imagine [the sinking of the titanic on acid] [4] [", // Need to figure out the tokens
|
|
isPrivileged: false,
|
|
requiresTokens: true,
|
|
defaultTokenUsage: COST_OF_COMMAND,
|
|
deferInitialReply: true,
|
|
async execute(interaction) {
|
|
const promptText = interaction.options.getString('prompt');
|
|
const images = interaction.options.getNumber('images') ?? undefined;
|
|
const size = interaction.options.getString('size') ?? undefined;
|
|
const discordAccountId = interaction.member.id;
|
|
try {
|
|
submitImagePromptTransaction(promptText, discordAccountId, images, size, interaction, this, async (err, imageResults) => {
|
|
if (err) throw err;
|
|
|
|
var dalleEmbeds = [];
|
|
log.DEBUG("Image Results: ", imageResults)
|
|
// Add the information post
|
|
dalleEmbeds.push(new EmmeliaEmbedBuilder()
|
|
.setColor(DALLE_COLOR)
|
|
.setTitle(`New Image Result`)
|
|
.setDescription(`${interaction.member.user} sent the prompt: '${promptText}'`)
|
|
);
|
|
// Add the images to the result
|
|
const imagesInResult = Array(imageResults.results).length
|
|
log.DEBUG("Images in the result: ", imagesInResult);
|
|
if (imagesInResult >= 1) {
|
|
for (const imageData of imageResults.results.data){
|
|
const imageUrl = imageData.url;
|
|
dalleEmbeds.push(new EmmeliaEmbedBuilder().setURL(imageUrl).setImage(imageUrl).setColor(DALLE_COLOR));
|
|
}
|
|
}
|
|
// Add the information post
|
|
dalleEmbeds.push(new EmmeliaEmbedBuilder()
|
|
.setColor(DALLE_COLOR)
|
|
.addFields({ name: 'Tokens Used', value: `${imageResults.totalTokens}`, inline: true })
|
|
.addFields({ name: 'Images Generated', value: `${imagesInResult}`, inline: true })
|
|
.addFields({ name: 'Image Size Requested', value: `${imagesInResult}`, inline: true })
|
|
);
|
|
await interaction.editReply({ embeds: dalleEmbeds, ephemeral: false });
|
|
});
|
|
|
|
// Needs reply code to reply to the generation
|
|
}catch(err){
|
|
log.ERROR(err)
|
|
//await interaction.reply(err.toString());
|
|
}
|
|
}
|
|
}; |