toowake

image-generate.js

Aug 7th, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
  2. const { Configuration, OpenAIApi } = require('openai');
  3.  
  4. const configuration = new Configuration({
  5. apiKey: 'API_KEY'
  6. });
  7.  
  8. const openai = new OpenAIApi(configuration);
  9.  
  10. module.exports = {
  11. data: new SlashCommandBuilder()
  12. .setName('image-generate')
  13. .setDescription(`This generates an image using a prompt provided by you`)
  14. .addStringOption(option => option.setName('prompt').setDescription(`Describe what image you want to generate`).setRequired(true)),
  15. async execute (interaction) {
  16.  
  17. await interaction.deferReply();
  18.  
  19. const prompt = interaction.options.getString('prompt');
  20.  
  21. try {
  22.  
  23. const response = await openai.createImage({
  24. prompt: `${prompt}`,
  25. n: 1,
  26. size: `512x512`,
  27. });
  28. const image = response.data.data[0].url;
  29.  
  30. const embed = new EmbedBuilder()
  31. .setColor("Blue")
  32. .setTitle(`Heres your image of a \`\`\`${prompt}\`\`\``)
  33. .setImage(image)
  34. .setTimestamp()
  35. .setFooter({ text: `Image Generator`})
  36.  
  37. await interaction.editReply({ embeds: [embed] });
  38.  
  39. } catch(e) {
  40. if (e.response.status == 400) return await interaction.editReply({ content: `I cannot generate that image - status code **400**`});
  41. return await interaction.editReply({ content: `Request failed with status code **${e.response.status}**`});
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment