0

It says [InteractionAlreadyReplied]: The reply to this interaction has already been sent or deferred. But inreality i am using editReply

I am having issue in logging error, it works fine with try and else but it shows [InteractionAlreadyReplied]: The reply to this interaction has already been sent or deferred in catch (error). I even tried using followUp but still doesnt work, it keeps giving the same error and shuts the whole bot down.

module.exports = {
  data: new SlashCommandBuilder()
  .setName("chat-gpt")
  .setDescription("chat-gpt-3")
  .addStringOption(option =>
    option.setName('prompt')
      .setDescription('Ask Anything')
      .setRequired(true)),

  async execute(interaction, client) {
    const prompt = interaction.options.getString('prompt');
    await interaction.deferReply();

    const configuration = new Configuration({
      apiKey: "nvm",
    });
    const openai = new OpenAIApi(configuration);
    
    const response = await openai.createCompletion({
      model: 'text-davinci-003',
      prompt: prompt,
      max_tokens: 2048,
      temperature: 0.7,
      top_p: 1,
      frequency_penalty: 0.0,
      presence_penalty: 0.0,
    });

    let responseMessage = response.data.choices[0].text;

    /* Exceed 2000 */

    try {
      let responseMessage = response.data.choices[0].text;
      if (responseMessage.length >= 2000) {
        const attachment = new AttachmentBuilder(Buffer.from(responseMessage, 'utf-8'), { name: 'chatgpt-response.txt' });
        const limitexceed = new EmbedBuilder()
        .setTitle(`Reached 2000 characters`)
        .setDescription(`Sorry, but you have already reached the limit of 2000 characters`)
        .setColor(`#FF6961`);
        await interaction.editReply({ embeds: [limitexceed], files: [attachment] });
        
      } else {
        const responded = new EmbedBuilder()
        .setTitle(`You said: ${prompt}`)
        .setDescription(`\`\`\`${response.data.choices[0].text}\`\`\``)
        .setColor(`#77DD77`);
  
        await interaction.editReply({ embeds: [responded] });   
      }

    } catch (error) {
      console.error(error);
      await interaction.followUp({ content: `error` });
    }

    return;
  },
};

i even tried using followUp or etc but the result is still same.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Darcel
  • 1
  • 1

1 Answers1

0

Your Problem

I believe the error occur in the catch() method (Line 55).

Please be reminded that the interaction has been deferred at Line 14:

await interaction.deferReply();

Explanation

Each interaction can only be replied and deferred once only. If you want to make changes on it, consider using the editReply() method.

Solution

Simply replace the code on Line 55 with editReply and this will solve the current error.

await interaction.editReply({ content: `error` });
Bobosky
  • 155
  • 1
  • 1
  • 15