Skip to content

Commit

Permalink
commands: add /image generate command to generate image with DALL-E (
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtraction authored Dec 2, 2023
2 parents fa80d6f + de490c3 commit cd71bd6
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/commands/image/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*!
* @author TRACTION (iamtraction)
* @copyright 2023
*/
import { ApplicationCommandOptionType, ChatInputCommandInteraction } from "discord.js";
import { Client, Command } from "@bastion/tesseract";
import OpenAI from "openai";

import Settings from "../../utils/settings.js";

class ImageGenerateCommand extends Command {
constructor() {
super({
name: "generate",
description: "Generate an image with DALL-E from OpenAI.",
owner: true,
options: [
{
type: ApplicationCommandOptionType.String,
name: "prompt",
description: "A description of the desired image.",
required: true,
},
{
type: ApplicationCommandOptionType.String,
name: "size",
description: "The size of the generated image.",
choices: [
{
name: "Square",
value: "1024x1024",
},
{
name: "Portrait",
value: "1024x1792",
},
{
name: "Landscape",
value: "1792x1024",
},
],
},
],
});
}

public async exec(interaction: ChatInputCommandInteraction<"cached">): Promise<void> {
await interaction.deferReply();

const prompt = interaction.options.getString("prompt");
const size = interaction.options.getString("size") as "1024x1024" | "1024x1792" | "1792x1024" || "1024x1024";

const openai = new OpenAI({
apiKey: ((interaction.client as Client).settings as Settings).get("openai").apiKey,
});

const response = await openai.images.generate({
model: "dall-e-3",
prompt: prompt,
response_format: "url",
size: size,
user: interaction.member.id,
});

await interaction.editReply({
content: response.data[0].revised_prompt,
files: [{
attachment: response.data[0].url,
}],
});
}
}

export { ImageGenerateCommand as Command };

0 comments on commit cd71bd6

Please sign in to comment.