【问题标题】:Discord.JS Slash Command Invalid Webhook Token after 15 minsDiscord.JS 斜线命令 15 分钟后 Webhook 令牌无效
【发布时间】:2021-11-12 00:55:18
【问题描述】:

我最近在 node.js 中使用 Discord.JS 库制作了一个冷却不和谐机器人,因为我想使用新的 Discord.JS v13 来执行 Slash 命令。结果我的循环出错了

DiscordAPIError: Invalid Webhook Token

我在交互时收到了 editReply 错误。

这是完整的错误

C:\Users\Administrator\Desktop\Discord Lab Countdown brokers\node_modules\discord.js\src\rest\RequestHandler.js:298
  throw new DiscordAPIError(data, res.status, request);
        ^
DiscordAPIError: Invalid Webhook Token
at RequestHandler.execute (C:\Users\Administrator\Desktop\Discord Lab Countdown brokers\node_modules\discord.js\src\rest\RequestHandler.js:298:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\Administrator\Desktop\Discord Lab Countdown brokers\node_modules\discord.js\src\rest\RequestHandler.js:50:14)
at async InteractionWebhook.editMessage (C:\Users\Administrator\Desktop\Discord Lab Countdown brokers\node_modules\discord.js\src\structures\Webhook.js:268:15)
at async CommandInteraction.editReply (C:\Users\Administrator\Desktop\Discord Lab Countdown brokers\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:139:21)
at async Timeout._onTimeout (C:\Users\Administrator\Desktop\Discord Lab Countdown brokers\commands\command.js:45:21) {

该命令可以正常工作,但机器人会在 15 分钟后崩溃。所以我读到令牌会在 15 分钟后过期??但我不知道那是不是真的,如果是,如何在循环运行时重新生成它?

这里是命令代码:

client.on('interactionCreate', async interaction => {
    
    // ...
    
    if (interaction.commandName === 'command') {
        // ...
        try {
            // ...
            let time = 60;
            let current = 0;
            
            await interaction.reply({embeds: [myembed]});

            // ...

            let interval = setInterval(async () => {
                current++;
                await interaction.editReply({embeds: [newembed]});
                if(current == time)
                {
                    // ...
                    await interaction.editReply({embeds: [newembed], components: [button_row]});  <--- error points here
                    clearInterval(interval);
                }
            }, 60000);
        } catch(err) {
            await interaction.reply({embeds: [error]});
        }
                
    }
});

我真的需要这方面的帮助,因为我在互联网上找不到任何关于它的信息,

非常感谢:D

编辑:我在 discord.js 文档中注意到斜杠命令不会在 15 分钟后过期,所以我只是推迟了命令的回复并使用普通消息而不是 interaction.channel.send()

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    您需要使用MessageComponentInteraction#deferReply() 方法来回复等待承诺履行的交互,这是因为如果在3000 毫秒内没有任何操作,则任何交互都会无效,即3 秒,如果您遵循答案,您的代码将类似于以下内容:

     try {
                // ...
                let time = 60;
                let current = 0;
                await interaction.deferReply()
                await interaction.editReply({embeds: [myembed]});
    
                // ...
                let interval = setInterval(async () => {
                    current++;
                    await interaction.editReply({embeds: [newembed]});
                    if(current == time)
                    {
                        // ...
                        await interaction.editReply({embeds: [newembed], components: [button_row]});  <--- error points here
                        clearInterval(interval);
                    }
                }, 60000);
            } catch(err) {
                await interaction.reply({embeds: [error]});
            }
                    
        }
    

    【讨论】:

    • 你不能在回复后 deferReply,我后来注意到斜杠命令的有效期为 15 分钟,所以我只是推迟了交互的回复,并在我的循环中使用了普通消息,它可以工作。
    • 您可以在回复互动之前先推迟回复,然后对我的回答进行适当的更改。
    • 是的,但这并没有改变交互令牌在 15 分钟内有效的事实,所以我注意到您可以跟进更改令牌,但这也会发送一个新回复,所以新消息不是什么我想要,所以我只使用了带有interaction.channel.send 的普通消息。
    猜你喜欢
    • 2021-06-11
    • 2022-01-25
    • 2021-09-12
    • 2021-11-17
    • 2022-11-25
    • 2021-02-18
    • 2021-08-17
    • 2022-01-15
    • 2015-09-07
    相关资源
    最近更新 更多