Home > OS >  I am using discord.js v14 and I made button. I Get Error Interaction has already been acknowledged
I am using discord.js v14 and I made button. I Get Error Interaction has already been acknowledged

Time:08-21

When I Click On It Once It Replies, But Second Time It Replies But Crashes Saying Interaction has already been acknowledged.

I dont wanna Make A Event Handler I am New So Can Someone Help Me With Example Code?

Here's My Code

    import { Client, GatewayIntentBits, Partials, ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";

const client = new Client({
  'intents': [
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildBans,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
  'partials': [Partials.Channel]
});

client.once('ready', () =>{
    console.log(`${client.user.username} Is Online!`);
    client.user.setActivity(`>>rank`, { type: "WATCHING" });
});

client.on("messageCreate", (message) => {
  const btn1 = new ButtonBuilder()
    .setCustomId('btn1')
    .setLabel('Click Me!')
    .setStyle('Primary')

    if (message.content === 'hi'){
      return message.channel.send({
        content: 'HI' , components:[new ActionRowBuilder().addComponents(btn1)]
      })
    }

    client.on('interactionCreate', async interaction => {
      if(interaction.isButton){
        await interaction.deferUpdate();
        if(interaction.customId === 'btn1'){
          await message.channel.send('Um Hello');
        }
      }
    });

});

client.login('TOKEN');

CodePudding user response:

As @Zsolt Meszaros Said I Removed The Interaction Handler From Message Handler And Placed It Down It So It Fixed The Problem

    import { Client, GatewayIntentBits, Partials, ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";

const client = new Client({
  'intents': [
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildBans,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
  'partials': [Partials.Channel]
});

client.once('ready', () =>{
    console.log(`${client.user.username} Is Online!`);
    client.user.setActivity(`>>rank`, { type: "WATCHING" });
});

client.on("messageCreate", (message) => {
  const btn1 = new ButtonBuilder()
    .setCustomId('btn1')
    .setLabel('Click Me!')
    .setStyle('Primary')

    if (message.content === 'hi'){
      return message.channel.send({
        content: 'HI' , components:[new ActionRowBuilder().addComponents(btn1)]
      })
    }
});

client.on('interactionCreate', async interaction => {
  if(interaction.isButton){
    await interaction.deferUpdate();
    if(interaction.customId === 'btn1'){
      await interaction.channel.send('Um Hello');
    }
  }
});


client.login('SUPER SECRET TOKEN');
  • Related