2017-02-24 78 views
0

我正在为Sinusbot(一个Bot for TeamSpeak)编写脚本,并且想编写一个脚本来检查用户是否已加入该频道。JavaScript setTimeout Sinusbot

问题在这里:我希望脚本在用户之后执行某些操作是在该通道中10秒钟!

我试过setTimeout但它没有奏效。

我做错了什么?

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); 
    } 

Sinusbot API:https://www.sinusbot.com/scripts/scripting3.html

编辑:

var timeout; 

sinusbot.on('clientMove', function(ev) { 
    if (ev.newChannel == channel_10m) { 
     timeout = setTimeout(() => { 
      sinusbot.chatPrivate(ev.clientId, msg1); 
     }, 10000); 
    } 
} 

sinusbot.on('clientMove', function(ev) { 
    if (timeout) { 
     clearTimeout(timeout); 
     sinusbot.chatPrivate(ev.clientId, msg2); 
    } 
} 

EDIT2:

我懂了:

 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); 
    } 
+0

你的代码看起来不错,setTimeout应该工作。看看javascript控制台的错误。使用javascript调试器查看程序中是否达到了setTimeout。 –

+0

没有“错误”。我只是需要一些思考来检查用户是否仍然在该频道中10秒后。 – ZarneXxX

+0

你确定你的程序正在进入if块吗? 'ev.newChannel == channel_10m'是真的吗? – josemigallas

回答

1

如果你的问题是要检查,如果用户仍conected你可以尝试这样的:

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); 
    } 
} 
+0

我做到了,我只是把它贴在我的帖子:) – ZarneXxX

+0

@ZarneXxX是否解决了您的问题?您应该将其标记为已解决,或者您应该添加自己并回答并标记它,以便大家看到问题已关闭:) – josemigallas