Search code examples
javascriptnode.jstwitter

twitterbot is repeating itself


For a bit of fun I decided to put a twitter bot together using a youtube based tutorial, and while this was relatively easy to execute I am having an issue with the bot repeating the same tweet a few times before it tweets something new.

At this stage the bot is only set up to post tweets that are put together at random from three seperate arrays

Array.prototype.pick = function() {
  return this[Math.floor(Math.random()*this.length)];
};

console.log("The bot is starting");

var Twit = require('twit');
var T = new Twit(require('./config.js'));
var partOne = require('./content/partone.js');
var partTwo = require('./content/parttwo.js');
var emoji = require('./content/emojis.js');
var statusOne = partOne.pick();
var statusEmoji = emoji.pick();
var statusTwo = partTwo.pick();


function tweetIt() {

    var tweet = {
      status: statusOne + statusTwo + ' ' + statusEmoji
    };

    T.post('statuses/update', tweet, tweeted);

    function tweeted(err, data, response) {
      if (err) {
        console.log("Something went wwrong!");
      } else {
        console.log("It worked!");
      }
    }
}

//tweetIt();

setInterval(tweetIt, 60 * 1000);

I have looked over this but cannot seem to figure out what would cause the bot to repeat itself a few times instead of creating a brand new tweet,

Any help with this would be much appreciated


Solution

  • You're picking your tweet parts outside of your interval. Try putting this:

    var statusOne = partOne.pick();
    var statusEmoji = emoji.pick();
    var statusTwo = partTwo.pick();
    

    inside your tweetIt function.