2016-07-18 3 views
1
私は Picarto.tvのために友人のセットアップにボットを手助けしようとしている

、我々はボットLINKを持っており、デフォルトpluginは、メッセージを繰り返すためにそこにありませんので、私は非常に原油を作ってみました(真剣に、それはひどいです私は開発者ではありません)pluginのために、私はSetInterval/SetTimeoutを使用しようとしました、そして、私はそれらを使用すると、メッセージを設定された間隔で一度チャットに入れますインターバルの後に、メッセージを2回、3回などと言うでしょう。のNode.js - のsetInterval&setTimeoutをインクリメンタルに繰り返しコマンド

それは次のようになります。上の

Time 1: 
Testing... 

Time 2: 
Testing... 
Testing... 

そしてそう。ここでコードは、私が言ったように、それはひどく作られた、私のためにそれをあまりにも難しくしないでください。

var api; 
function handleChatMsg(data) { 
    var recursive = function() { 
     api.Messages.send("Testing Bot Repeat..."); 
     setTimeout(recursive,15000); 
    } 
    recursive(); 
} 



module.exports = { 
    meta_inf: { 
     name: "Repeat Message", 
     version: "1.0.0", 
     description: "Repeats a message every 5 minutes. Message and interval can be changed.", 
     author: "ZX6R" 
    }, 
    load: function (_api) { 
     api = _api; 
    }, 
    start: function() { 
     api.Events.on("userMsg", handleChatMsg); 
    } 
} 

メッセージを何度も増やしている理由を理解できますか?

EDIT:問題を修正し、新しいコードは

var api; 
// Function to call for the repeating 
function handleChatMsg() { 
// This sets the interval of 5 minutes, and calls the variable. Edit the numbers after the comma to change the interval. You MUST put it into milliseconds. 
setInterval(function(){xyz()}, 15000); 
// This sets the variable, edit the text in "api.Messages.send" to change what the bot repeats. 
var xyz = function() 
{ 
    api.Messages.send("Testing...") 
} 
} 


// defines some information about the plugin, and sets up stuff we need. 
module.exports = { 
    meta_inf: { 
     name: "Repeat Message", 
     version: "1.1.1", 
     description: "Repeats a message every 5 minutes. Message and interval can be changed.", 
     author: "ZX6R" 
    }, 
load: function (_api) { 
     api = _api; 
    }, 
    start: function() { 
     handleChatMsg(); 
    } 
} 

// The MIT License (MIT) 

// Copyright (c) 2016 RedFalconv2 - ZX6R - WalnutGaming 

//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 

// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

答えて

0

はのは、物事を複雑にし、それを簡単にはやめましょうです。

setInterval(function(){xyz()}, 300000); 

var xyz = function() 
{ 
    //Do what you want your function to do. 
} 

ここで、関数xyzは300,000ミリ秒ごとに呼び出されます。これは5分を意味します。

アプリケーション用に定期的に使用する場合は、Node Cronbtwをご覧ください。次のような

+0

これは私の以前のものより少し洗練されていますが、初めてテストの後にさらにテストを追加し続けます。私はそのスタート 'に関連していると思う:function(){ api.Events.on( "userMsg"、handleChatMsg); } } ' – Thunderwolf

+0

メッセージが送信されたことを検出している可能性がありますし、setinterval AGAINを実行しています。しかし、プラグインはこのapi.Events.onや関数の前にすべてのものをロードすることはありません。 – Thunderwolf

+0

私は正しく、最初に "handleChatMsg"関数を呼び出すように変更しました。これで正しく動作します!ちなみに、Node Cronの方が良いと思いますか、この方法ですか?私はちょうどボットを自分の走りのままにして、ストリームの間に置いておきたいと思っています。 – Thunderwolf

0

おそらく何か:

// define and export module 
var plugin = {}; 
module.exports = plugin; 

// plugin metadata 
plugin.meta_inf = { 
     name: "Repeat Message", 
     version: "1.0.0", 
     description: "Repeats a message every 5 minutes. Message and interval can be changed.", 
     author: "ZX6R" 
    }; 

/** 
* To be called on plugin load. 
* @param {Object} api - api instance. 
*/ 
plugin.load = function(api){ 
    plugin._api = api; 
}; 

/** 
* Called on plugin start. 
*/ 
plugin.start = function(){ 

    if(!plugin._api){ 
     // api instance should have been available 
     throw new Error("api not defined, is plugin load()'ed ?"); 
    } 

    // each user message event (re)configures the repeating timer 
    plugin._api.Events.on("userMsg",function(){ 
     plugin._configure(true, "Testing echo bot...",15000); 
    }); 
}; 

/** 
* Configure the repeating timer 
* @param {Boolean} enabled - true to enable timer, false to disable 
* @param {String} message - message to repeat, required if enabled 
* @param {Number} interval - milliseconds between repeats, required if enabled 
*/ 
plugin._configure = function(enabled, message, interval){ 

    if(plugin._timer){ 
     // always clear any old timer 
     clearInterval(plugin._timer); 
    } 

    if(enabled){ 
     if(!message || !interval){ 
     // message and interval are required 
     throw new Error("message and interval are required."); 
     } 

     // set a repeating timer 
     plugin._timer = setInterval(function(){ 
     plugin._api.Messages.send(message); 
     },interval); 
    } 

}; 

注:

  1. ユーザーのメッセージイベントは、タイマーに新しい設定を適用します。ボットリピートのユーザーメッセージ、または何かカスタムを持つことができます。
  2. または、タイマーは1回のみ設定できます。
+0

ありがとう、しかし、私は問題を解決した、私は開始メッセージのプラグイン変数を "setinterval"を呼び出すように設定していました。私はsetintervalを一度呼び出すように設定しましたが、今は動作しています! – Thunderwolf

関連する問題