Home > database >  DiscordAPIError 40060: "Interaction has already been acknowledged"
DiscordAPIError 40060: "Interaction has already been acknowledged"

Time:12-06

I am in a problem where I am experiencing this error and I tried my best to fix this API error.

index.js:

const fs = require('node:fs');
const path = require('node:path');
const { Events, Collection, ActivityType } = require('discord.js');

const client = require('./client.js');
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);
    if ('data' in command && 'execute' in command) {
        client.commands.set(command.data.name, command);
    } else {
        console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
    }
}
client.on(Events.InteractionCreate, async interaction => {
    if (!interaction.isModalSubmit()) return;
    if (interaction.customId === 'myModal') {
        await interaction.reply({ content: 'Your submission was received successfully!' });
    const good = interaction.fields.getTextInputValue('good');
    const bot = interaction.fields.getTextInputValue('bot');
    client.users.send('821682594830614578', `good: "${good}"
bot: "${bot}" from ${interaction.user.username}`);
    }
});
client.on(Events.InteractionCreate, interaction => {
  if (!interaction.isChatInputCommand()) return;const command = interaction.client.commands.get(interaction.commandName);
    if (!command) {
        console.error(`No command matching ${interaction.commandName} was found.`);
        return;
    }

    try {
        command.execute(interaction);
    } catch (error) {
        console.error(error);
        interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});
client.once(Events.ClientReady, c => {
  client.user.setActivity('you or i have no food', { type: ActivityType.Watching });
    console.log(`Logged in as ${c.user.tag}`);
});
client.login(process.env.token);

client.js:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
module.exports = client;

I placed the client variable to client.js because some commands need the client variable thus placing it in a different file. I followed the discord.js guide, so you may see some code from there.

CodePudding user response:

You are using two times the InteractionCreate Event you should combine the two as followed:

client.on(Events.InteractionCreate, async (interaction) => {
    if (interaction.isModalSubmit()) {
        if (interaction.customId === "myModal") {
            await interaction.reply({
                content: "Your submission was received successfully!",
            });
            const good = interaction.fields.getTextInputValue("good");
            const bot = interaction.fields.getTextInputValue("bot");
            client.users.send(
                "821682594830614578",
                `good: "${good}"
bot: "${bot}" from ${interaction.user.username}`
            );
        }
    } else if (interaction.isChatInputCommand()) {
        const command = interaction.client.commands.get(interaction.commandName);
        if (!command) {
            console.error(
                `No command matching ${interaction.commandName} was found.`
            );
            return;
        }

        try {
            command.execute(interaction);
        } catch (error) {
            console.error(error);
            interaction.reply({
                content: "There was an error while executing this command!",
                ephemeral: true,
            });
        }
    } else {
        interaction.reply({
            content: "Interaction was no ModalSubmit or ChatInputCommand",
            ephemeral: true,
        });
    }
});

For your problem with the double interaction reply. It would be helpful to include the full error message and your js file from the command as well. Because with the propesed solution there only can be one interaction.reply in index.js so there is no problem

  • Related