【问题标题】:Discord.js: DiscordAPIError[40060]: Interaction has already been acknowledgedDiscord.js:DiscordAPIError [40060]:交互已被确认
【发布时间】:2023-01-31 15:25:00
【问题描述】:

我正在使用 Discord.js 的指南创建一个机器人,但是在 3 个或有时 3 个命令之后,机器人停止工作,我得到 discord message 我曾多次尝试重新启动它,但过了一段时间后它一次又一次地停止工作

const fs = require('node:fs');
const path = require('node:path')
const { Client, Events, GatewayIntentBits, Collection ,ActionRowBuilder,EmbedBuilder, StringSelectMenuBuilder } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });
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`);
    }
}


client.once(Events.ClientReady, () => {
    console.log('Ready!');
})

//menu
client.on(Events.InteractionCreate, async interaction => {
    if (!interaction.isChatInputCommand()) return;

    if (interaction.commandName === 'ping') {
        const row = new ActionRowBuilder()
            .addComponents(
                new StringSelectMenuBuilder()
                    .setCustomId('select')
                    .setPlaceholder('Nothing selected')
            );
            const embed = new EmbedBuilder()
            .setColor(0x0099FF)
            .setTitle('pong')
            .setDescription('Some description here')
            .setImage('https://media.istockphoto.com/id/1310339617/vector/ping-pong-balls-isolated-vector-illustration.jpg?s=612x612&w=0&k=20&c=sHlz5sbJrymDo7vfTQIuaj4lbmwlvAhVE7Uk_631ZA8=')

        await interaction.reply({ content: 'Pong!', ephemeral: true, embeds: [embed]});
    }
});
//======================================================================================================================

client.on(Events.InteractionCreate, async interaction => {
    if (!interaction.isChatInputCommand || 
        interaction.isButton() ||
        interaction.isModalSubmit()) return;

    const command = interaction.client.commands.get(interaction.commandName);

    if (!command) {
        console.error(`No command matching ${interaction.commandName} was found`)
        return;
    }
    try {
        await command.execute(interaction);
    }catch(error){
        console.error(error);
        await interaction.reply({content: 'There was an error while executing this command!', ephemeral: true});
    }
    console.log(interaction);
});
client.login(token);

Error i get in terminal

我希望这个机器人只要启动并运行就继续执行命令

【问题讨论】:

  • 我看到你正在使用 console.log() 你有程序跟踪的输出吗?该错误表明该请求已被确认。在您的错误消息中,它指出问题是这一行(第 65 行):await interaction.reply({content: 'There was an error while execution this command!', ephemeral: true});

标签: javascript discord.js


【解决方案1】:

这是 Discord.JS 中的一个常见错误,当您已经回复了交互并尝试再次回复时会发生这种错误。

来自 discord.js discord 服务器:

您已经回复了互动。

• 使用.followUp() 发送新消息

• 如果您推迟回复,最好使用 .editReply()

• 回应slash commands / buttons / select menus

要修复此错误,您可以使用.followUp() 向频道发送另一条消息或使用.editReply() 编辑回复,如上所示。

您可以查看有关命令交互的文档here

【讨论】:

    【解决方案2】:

    是的,问题是你不能在 discords api 中多次回复斜杠命令,所以你应该使用

    interaction.channel.send()
    

    或者

    interaction.editReply()
    

    【讨论】:

    • interaction.channel.send()interaction.followUp() 相同,如果这弥补了任何混淆
    • 现在可以正常使用了,非常感谢
    猜你喜欢
    • 2022-12-18
    • 2022-12-04
    • 2022-06-24
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多