toowake

chatgpt.js

Aug 7th, 2023
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 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. const openai = new OpenAIApi(configuration);
  8.  
  9. module.exports = {
  10. data: new SlashCommandBuilder()
  11. .setName("chatgpt")
  12. .setDescription("Ask chatgpt a question")
  13. .addStringOption(option => option.setName("question").setDescription("This is going to be the question for chatgpt").setRequired(true))
  14. .setDMPermission(false),
  15. async execute(interaction) {
  16.  
  17.  
  18.  
  19. await interaction.deferReply();
  20.  
  21. const question = interaction.options.getString("question");
  22.  
  23. try {
  24. const res = await openai.createCompletion({
  25. model: 'text-davinci-003',
  26. max_tokens: 2048,
  27. temperature: 0.5,
  28. prompt: question
  29. })
  30.  
  31. const embed = new EmbedBuilder()
  32. .setColor("Blue")
  33. .setTitle(`Prompt: ${question}`)
  34. .setDescription(`\`\`\`${res.data.choices[0].text}\`\`\``)
  35.  
  36. await interaction.editReply({ embeds: [embed] });
  37.  
  38. } catch(e) {
  39. return await interaction.editReply({ content: `Request failed with status code **${e.response.status}**`, ephemeral: true })
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment