我有我的代码,我做到了,所以每隔5秒钟它就会发送一条消息(Interval
),现在我希望它在键入@bclear或@bfound后重置间隔上的计时器。 (@)是前缀。这是我的代码
client.on('message', message => {
if (message.content.startsWith(`${prefix}start`)) {
message.delete()
setInterval(function() {
lastbuffer++;
client.channels.cache.get("700489735352746045").send('<@&700343773342990337>').then(msg => {
msg.delete();
})
client.channels.cache.get("700489735352746045").send(Buffer).then(msg => {
msg.delete({
timeout: 4000
})
});
}, 5000)
}
if (message.content.startsWith(`${prefix}bclear`)) {
message.delete()
setInterval(5000);
client.channels.cache.get("700489735352746045").send(BufferClear);
lastbuffer = 0;
}
if (message.content.startsWith(`${prefix}bfound`)) {
message.delete()
const BufferNClear = new Discord.MessageEmbed()
.setColor('#FF0000')
.setTitle('Buffers are NOT clear!')
.setDescription('Enemies are building a cannon on us!')
.addField("Triggered By: ", message.author.toString(), true)
.addField("Time triggered: ", datetime, true)
.setTimestamp()
.setFooter('we got splitL')
client.channels.cache.get("700489735352746045").send("<@&700343773342990337> Stay alert! Enemies on our walls!")
client.channels.cache.get("700489735352746045").send("<@&700343773342990337> Stay alert! Enemies on our walls!")
client.channels.cache.get("700489735352746045").send("<@&700343773342990337> Stay alert! Enemies on our walls!")
message.delete();
client.channels.cache.get("700489735352746045").send(BufferNClear)
lastbuffer = 0;
}
})
setInterval()
返回应该存储在变量中的intervalID。稍后,当您要停止间隔时,必须致电clearInterval(intervalID)
。MDN web docs中的更多详细信息>
//just make sure it has access to the params it needs to work
function sendMessage() {
lastbuffer++;
client.channels.cache.get("700489735352746045").send('<@&700343773342990337>').then(msg => {
msg.delete();
})
client.channels.cache.get("700489735352746045").send(Buffer).then(msg => {
msg.delete({
timeout: 4000
})
});
}
let intervalID;
const intervalSeconds = 5000;
client.on('message', message => {
if (message.content.startsWith(`${prefix}start`)) {
message.delete()
intervalID = setInterval(sendMessage, intervalSeconds)
}
if (message.content.startsWith(`${prefix}bclear`)) {
message.delete()
clearInterval(intervalID);
client.channels.cache.get("700489735352746045").send(BufferClear);
lastbuffer = 0;
intervalID = setInterval(sendMessage, intervalSeconds)
}
if (message.content.startsWith(`${prefix}bfound`)) {
message.delete()
clearInterval(intervalID);
const BufferNClear = new Discord.MessageEmbed()
.setColor('#FF0000')
.setTitle('Buffers are NOT clear!')
.setDescription('Enemies are building a cannon on us!')
.addField("Triggered By: ", message.author.toString(), true)
.addField("Time triggered: ", datetime, true)
.setTimestamp()
.setFooter('we got splitL')
client.channels.cache.get("700489735352746045").send("<@&700343773342990337> Stay alert! Enemies on our walls!")
client.channels.cache.get("700489735352746045").send("<@&700343773342990337> Stay alert! Enemies on our walls!")
client.channels.cache.get("700489735352746045").send("<@&700343773342990337> Stay alert! Enemies on our walls!")
message.delete();
client.channels.cache.get("700489735352746045").send(BufferNClear)
lastbuffer = 0;
intervalID = setInterval(sendMessage, intervalSeconds)
}
})