【问题标题】:DiscordAPIError 40060: "Interaction has already been acknowledged"DiscordAPIError 40060:“交互已被确认”
【发布时间】:2022-12-18 16:06:24
【问题描述】:

我遇到了这个错误,我尽力修复了这个 API 错误。

索引.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);

平.js:

const { SlashCommandBuilder } = require('discord.js');
const client = require('./../client.js');
module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Ping! Pong!'),
    async execute(interaction) {
        await interaction.reply(`Websocket heartbeat: ${client.ws.ping} ms. 
Roundtrip latency: NaN ms.`);
    },
};

我将客户端变量放置到client.js,因为一些命令需要client变量,因此将其放置在不同的文件中。我遵循了 discord.js 指南,因此您可能会从那里看到一些代码。

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您使用了两次 InteractionCreate 事件,您应该将两者结合起来,如下所示:

    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,
            });
        }
    });
    

    针对你的双交互问题回复。从命令中包含完整的错误消息和您的 js 文件会很有帮助。因为在 propesed 解决方案中只能有一个 interaction.reply in index.js 所以没有问题

    【讨论】:

    • 有效,但是当我重新运行我的 ping 命令时,它返回错误
    • 您能否也分享一下您的 ping 命令?我看不到命令​​中的交互是如何处理的。
    • 完毕。我编辑了消息
    • 为清楚起见:它第一次工作,然后,当我再次运行它时,它只返回错误
    猜你喜欢
    • 2022-12-04
    • 2023-01-31
    • 2022-06-24
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多