如何跟踪以前的回复



我正在为D&D游戏。我希望机器人能够识别某人最近使用的命令。这就是我迄今为止所拥有的;

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执行函数一次,而其他函数则连续循环,直到被告知中断。除此之外,您使用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)
}

最新更新