javascript - 如何阻止函数执行?
问题描述
所以我基本上想为我的不和谐票务机器人创建一个倒计时。如果有人输入 -close[...] 频道将在 10 秒后被删除。但是,如果执行此命令的人在频道中键入内容,倒计时将停止并且频道不会被删除。
到目前为止效果很好。但是,如果我中止发送到频道的所有其他消息的倒计时,则嵌入将发送到显示“倒计时停止”的位置,如果我再次键入 -close [...],则会弹出此消息,但频道仍将是10秒后删除。
function closeTicket (_ticketid, channel, deleter, reason) {
var timer = setTimeout(function() {
channel.delete();
}, 10000);
channel.send({embed: {
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: ``,
description: "This ticket will close in 10 seconds. If this was a mistake type anything to stop the timer.",
fields: [{
name: "Thank you!",
value: "Thank you for using our ticket system! Good luck and have fun playing on our servers."
},
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "Support Ticket System © H4rry#6701"
}
}
});
logTicketClosed(_ticketid, deleter, reason);
client.on('message', message => {
if(message.channel === channel && message.author === deleter && timer != null) {
clearTimeout(timer);
timer = null;
message.channel.send({embed: {
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: `Timer Stopped`,
description: "The timer has been stopped, the ticket will remain open.",
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "Support Ticket System © H4rry#6701"
}
}});
}
});
return 0;
}
解决方案
我现在开始工作了!我定义了一个名为的新变量timer_running
,该变量将设置为true
计时器启动和false
停止的时间。这样我现在就可以工作了。
function closeTicket (_ticketid, channel, deleter, reason) {
var timer_running = false;
var timer = setTimeout(function() {
channel.delete();
}, 10000);
timer_running = true;
channel.send({embed: {
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: ``,
description: "This ticket will close in 10 seconds. If this was a mistake type anything to stop the timer.",
fields: [{
name: "Thank you!",
value: "Thank you for using our ticket system! Good luck and have fun playing on our servers."
},
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "Support Ticket System © H4rry#6701"
}
}
});
logTicketClosed(_ticketid, deleter, reason);
client.on('message', message => {
if(message.channel === channel && message.author === deleter && timer_running === true) {
clearTimeout(timer);
timer_running = false;
message.channel.send({embed: {
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: `Timer Stopped`,
description: "The timer has been stopped, the ticket will remain open.",
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "Support Ticket System © H4rry#6701"
}
}});
}
});
}
推荐阅读
- javascript - 如何使用反应在 Ui 上显示嵌套的 json
- java - 同一对字符串可能有不同的最长公共子序列吗?
- javascript - 我在对象上使用“in”关键字时收到 TypeError?
- javascript - 在外部解决承诺的 JavaScript“惯用”方式是什么?
- android - 将摄像头和音频的权限授予 WebView?
- java - Springboot 2.3.1 多租户环境下动态更新Jdbc模板的schema
- python - 我们可以使用 --user / -u 在 docker 容器中运行 jupyter lab
- rust - 预计算术溢出但未发生
- python - Python 中 Plotly Express 的等值线图可以接受 CSS 格式的颜色吗?
- mysql - 使用 SaltStack 设置 mariadb 根密码