JavaScript setbot
我正在为SinsusBot编写脚本,这是TeamSpeak的一个Bot,我想编写一个脚本,检查用户是否已加入频道 这里的问题是:我希望脚本在用户进入该频道10秒后执行操作 我用setTimeout试过了,但没用 我做错了什么JavaScript setbot,javascript,settimeout,Javascript,Settimeout,我正在为SinsusBot编写脚本,这是TeamSpeak的一个Bot,我想编写一个脚本,检查用户是否已加入频道 这里的问题是:我希望脚本在用户进入该频道10秒后执行操作 我用setTimeout试过了,但没用 我做错了什么 if (ev.newChannel == channel_10m){ //if someone joins channel_10m //wait 10 seconds setTimeout(function(){
if (ev.newChannel == channel_10m){
//if someone joins channel_10m
//wait 10 seconds
setTimeout(function(){
if (ev.newChannel == channel_10m){
//check if user is in channel_10m
//do somethink
}
}, 10000);
}
机器人应用程序接口:
编辑:
编辑2:
我明白了:
if (ev.newChannel == achannel_entrance){
setTimeout(function(){
if ((sinusbot.getChannel(1267)['clients'][0]['id'] && ev.newChannel) == (sinusbot.getChannel(1267)['clients'][0]['id'] && achannel_entrance)){
sinusbot.chatPrivate(ev.clientId, msg0);
sinusbot.move(ev.clientId, bchannel_support);
}
}, 300000);
}
如果您的问题是要检查用户是否仍然连接,您可以尝试以下方法:
var timeout;
sinusbot.on("connect", ev => {
if (ev.newChannel == channel_10m) {
timeout = setTimeout(() => {
doSomething();
}, 10000);
}
}
sinusbot.on("disconnect", ev => {
if (timeout) {
clearTimeout(timeout);
}
}
编辑:
我认为您现在正在做的是取消超时,无论客户机正在移入或移出。您应该可以跟踪不同的客户,让我们尝试以下方法:
// Dictionary for <clientId, timeout>
const timeouts = [];
// Event triggers when a client goes online or offline
// If client disconnects channel will be 0
sinusbot.on('clientMove', function(ev) {
const clientId = ev.clientId;
if (ev.newChannel == channel_10m) {
timeouts[clientId] = setTimeout(() => {
sinusbot.chatPrivate(clientId, msg1);
}, 10000);
} else if (ev.newChannel == 0 && timeouts[clientId]) {
clearTimeout(timeouts[clientId]);
sinusbot.chatPrivate(clientId, msg2);
}
}
您的代码看起来不错,setTimeout应该可以工作。查看javascript控制台中的错误。使用javascript调试器查看程序中是否达到setTimeout。没有错误。我只需要想一想,检查用户是否在10秒后仍在该频道。你确定你的程序正在进入if块吗?ev.newChannel==channel_10m是真的吗?正如我所说,你的设置是有效的,请参阅:原因是代码没有到达。@josemigallas两个都有效,但它同时检查两个。我做到了,我只是把它贴在我的帖子上:@ZarneXxX我的解决方案解决了你的问题了吗?您应该将其标记为已解决,或者您应该添加自己并回答并标记,以便每个人都看到问题已结束:
// Dictionary for <clientId, timeout>
const timeouts = [];
// Event triggers when a client goes online or offline
// If client disconnects channel will be 0
sinusbot.on('clientMove', function(ev) {
const clientId = ev.clientId;
if (ev.newChannel == channel_10m) {
timeouts[clientId] = setTimeout(() => {
sinusbot.chatPrivate(clientId, msg1);
}, 10000);
} else if (ev.newChannel == 0 && timeouts[clientId]) {
clearTimeout(timeouts[clientId]);
sinusbot.chatPrivate(clientId, msg2);
}
}