代码之家  ›  专栏  ›  技术社区  ›  Justin Harris

问题:当一个命令连续运行多次时,DiscordJS应用程序崩溃

  •  0
  • Justin Harris  · 技术社区  · 2 年前

    1)启动时没有问题。控制台中加载的所有确认消息和命令都已注册

    Discord Error Message

    4)当第三次运行/ping命令时,我得到了“Pong!”的成功响应但是机器人崩溃了,并显示以下错误消息

    C:\Users\XXXX\Documents\bot\discordBot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:94
        if (this.deferred || this.replied) throw new Error('INTERACTION_ALREADY_REPLIED');
                                                 ^
    
    Error [INTERACTION_ALREADY_REPLIED]: The reply to this interaction has already been sent or deferred.
        at CommandInteraction.reply (C:\Users\XXXX\Documents\bot\discordBot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:94:46)
        at Client.<anonymous> (C:\Users\XXXX\Documents\bot\discordBot\events\interactionCreate.js:17:23)
        at processTicksAndRejections (node:internal/process/task_queues:96:5) {
      [Symbol(code)]: 'INTERACTION_ALREADY_REPLIED'
    }
    

    我的Github上有一个完整的代码示例供查看: https://github.com/DBAggie/discordBot/tree/dev

    1 回复  |  直到 2 年前
        1
  •  0
  •   Chang Alex    2 年前

    好的,经过几次测试,我发现问题出在哪里。 interactionCreate 解雇。

    事件

    module.exports = {
        name: 'interactionCreate',
        execute(interaction, client) {
            client.on('interactionCreate', async interaction => {
                console.log(`hit the interaction create script`)
                if (!interaction.isCommand()) return console.log('Not a valid command');
    
                const command = client.commands.get(interaction.commandName);
    
                if (!command) return;
    
                try {
                    console.log('Trying to execute')
                    await command.execute(interaction);
                } catch (error) {
                    await interaction.reply({
                        content: 'There was an error while executing this command!',
                        ephemeral: true
                    });
                }
                console.log('Below the catch');
            });
            //console.log('Below the below catch message');
        }
    };
    

    之后

    module.exports = {
        name: "interactionCreate",
        async execute(interaction, client) {
            console.log(`hit the interaction create script`);
            if (!interaction.isCommand()) return console.log("Not a valid command");
    
            const command = client.commands.get(interaction.commandName);
    
            if (!command) return;
    
            try {
                console.log("Trying to execute");
                await command.execute(interaction);
            } catch (error) {
                await interaction.reply({
                    content: "There was an error while executing this command!",
                    ephemeral: true,
                });
            }
            console.log("Below the catch");
            //console.log('Below the below catch message');
        },
    };
    

    看到这里了吗?每次互动时,你都会听到一个新的事件。

    execute(interaction, client) {
        client.on('interactionCreate', async interaction => {