From 5e57461505982a060b6a70fd739bf08db176f2ae Mon Sep 17 00:00:00 2001 From: Atridad Lahiji <88056492+atridadl@users.noreply.github.com> Date: Thu, 15 Jun 2023 23:14:26 -0600 Subject: [PATCH] Img --- src/commands/pic.ts | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/commands/pic.ts diff --git a/src/commands/pic.ts b/src/commands/pic.ts new file mode 100644 index 0000000..3aa997c --- /dev/null +++ b/src/commands/pic.ts @@ -0,0 +1,60 @@ +import { ApplyOptions } from '@sapphire/decorators'; +import { Args, Command } from '@sapphire/framework'; +import { Message } from 'discord.js'; +import { Configuration, OpenAIApi } from 'openai'; + +const configuration = new Configuration({ + apiKey: process.env.OPENAI_API_KEY +}); +const openai = new OpenAIApi(configuration); + +@ApplyOptions({ + description: 'Make a picture!', + options: ['prompt'] +}) +export class UserCommand extends Command { + // Register Chat Input and Context Menu command + public override registerApplicationCommands(registry: Command.Registry) { + registry.registerChatInputCommand((builder) => + builder // + .setName(this.name) + .setDescription(this.description) + .addStringOption((option) => + option.setName('prompt').setDescription('You can ACTUALLY ask Himbot something! So cool!').setRequired(true) + ) + ); + } + + // Message command + public async messageRun(message: Message, args: Args) { + return this.pic(message, args.getOption('prompt') || message.content.split('!wryna ')[1]); + } + + // Chat Input (slash) command + public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { + return this.pic(interaction, interaction.options.getString('prompt') || 'NOTHING'); + } + + private async pic(interactionOrMessage: Message | Command.ChatInputCommandInteraction | Command.ContextMenuCommandInteraction, prompt: string) { + const askMessage = + interactionOrMessage instanceof Message + ? await interactionOrMessage.channel.send({ content: '🤔 Thinking... 🤔' }) + : await interactionOrMessage.reply({ content: '🤔 Thinking... 🤔', fetchReply: true }); + + const imageResponse = await openai.createImage({ + prompt, + n: 1, + size: '256x256' + }); + + const content = imageResponse.data.data[0].url || 'ERROR!'; + + if (interactionOrMessage instanceof Message) { + return askMessage.edit({ content }); + } + + return interactionOrMessage.editReply({ + content: content + }); + } +}