如何跟踪以前的回复?

How can I track previous responses?

我正在为 D&D 游戏开发一个 discord 机器人。我希望机器人能够识别出有人最近使用了命令。这是我目前所拥有的;

    const barid = require("./tavernarray.json")
    var tavernarray = barid.guests;
    
    message.channel.send('Tavern Keeper Emotes and replies;')

    if (message.member.roles.cache.has('770678762944725003')){
        if (tavernarray.includes(message.author.id)){
            message.channel.send("Weren't you just here?");
        } else;
            tavernarray.push(message.author.id);
            message.channel.send(` Welcome to the Tavern Guild Master ${message.author}`);
            setInterval (() => {
                tavernarray.pop(message.author.id)
            }, 30000)

    } else {
         message.channel.send("Error no role");
    }

据我所知,代码在第一个命令中起作用,我们收到了预期的欢迎消息,并将用户 ID 添加到数组中。但是在第二个命令上,有一个短暂的延迟,然后我们收到了两条消息。我应该使用 setTimeout 而不是 setInterval 吗?还是使用 .json 数组有问题?我尝试将数组保留在程序中,但每次我 运行 它都会不断重置。

是的,您应该使用 setTimeout()。您可能遇到问题的原因是因为代码试图每 30 秒从 JSON 数组中删除变量,这将导致两个问题:更高的内存使用率和潜在的错误。 setInterval()setTimeout() 之间的区别是 timeout 执行函数 once,而另一个不断循环直到它被告知中断。除此之外,您使用 else 的方式也是问题所在。当您使用 else;(注意分号)时,您是在告诉代码如果 ID 不存在,它不应该执行任何代码,因为分号表示行尾的代码。下面显示了一个片段

if (tavernarray.includes(message.author.id)){
    message.channel.send("Weren't you just here?");
} else { // Instead of ; we'll use {}
    tavernarray.push(message.author.id);
    message.channel.send(` Welcome to the Tavern Guild Master ${message.author}`);
    setInterval (() => {
        tavernarray.pop(message.author.id)
    }, 30000)
}