Select 一个随机文件并随 node.js 一起移动

Select a random file and move with node.js

我正在尝试修复我的推特机器人的一个错误,基本上,有一个包含文件夹所有文件名的数组,然后随机选择一个并发布它,但有时会再次发布相同的图像,怎么可能我修好了?

这是代码

var fs = require('fs'),
    path = require('path'),
    Twit = require('twit'),
    set = require(path.join(__dirname, 'set.js'));
    //array of files
    files_memes = require(path.join(__dirname, 'files.js'))

var currentdate = new Date();
var upl = "Subido: "
          + currentdate.getHours() + ":"
          + currentdate.getMinutes()+ " hs.";

var setMin = 10;
var T = new Twit(set);

function random_file(){
  var allFiles = (files_memes)//array
  return allFiles[Math.floor(Math.random() * allFiles.length)];
}

var filename = (random_file());
var imgPATH = path.join(__dirname, '/memestorage/queue/' + filename);


//image selection and upload
function upload_random_image(){
  console.log('Opening file...');
  var image_path = imgPATH,
      b64content = fs.readFileSync(image_path, { encoding: 'base64' });

  console.log('Uploading file...');
  T.post('media/upload', { media_data: b64content }, function (err, data, response) {
    if (err){
      console.log('ERROR');
      console.log(err);
    }
    else{
      console.log('File loaded!');

      T.post('statuses/update', {
        media_ids: new Array(data.media_id_string)
      },
        function(err, data, response) {
          if (err){
            console.log('Error!');
            console.log(err);
          }
          else{
            console.log('Tweeted!');
            console.log(upl);
            console.log('Next tweet in ' + setMin + ' min.');
          }
        }
      );
    }
  });
}

//timer

setTimeout(
      upload_random_image,
        0
    );
setInterval(
      upload_random_image,
      1000*10
    );

我试过

...
var filename = (random_file());
var pfile = ("posted"+random_file());
var imgPATH = path.join(__dirname, '/memestorage/queue/' + filename);
var postedFile = path.join(__dirname, '/memestorage/posted/' + pfile);
fs.rename(imgPATH, postedFile, function(err) {
  if ( err ) console.log('ERROR: ' + err);
});

//image selection and upload
function upload_random_image(){
  console.log('Opening file...');
  var image_path = imgPATH,
      b64content = fs.readFileSync(imgPATH, { encoding: 'base64' });
...

但是一遍又一遍地发布相同的图片,或者有时会给出这样的错误信息:

fs.js:640
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open 'D:\memesbot\memestorage\queue5 (2).jpg'
    at Error (native)
    at Object.fs.openSync (fs.js:640:18)
    at Object.fs.readFileSync (fs.js:508:33)
    at Timeout.upload_random_image [as _onTimeout] (D:\memesbot\memes.js:29:23)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5)

希望有人能帮助我,谢谢。

代码似乎在开始时生成随机文件 (var filename = (random_file());),但在 upload_random_image() 的 运行 处没有。

因此,文件被随机选择一个,upload_random_image 被调用多次 setInterval

解决方案:

在方法内移到下一行upload_random_image

var filename = (random_file());
var imgPATH = path.join(__dirname, '/memestorage/queue/' + filename);