var fs = require('fs-extra');

module.exports = (lib) => {
  lib.trigger.add({
    name: 'thumbnailer',
    call: /^\!thumb(.*\d+)?$/i,
    level: 100,
    active: 1,
    func: (e) => {
      var id;
      if(id = e.message.split(' ')[1]) {
        if(Number.isInteger(parseInt(id)))
          fs.unlink('./t/'+id+'.png', () => lib.generateThumbs() );
      }
      /*else {
        deleteFolderRecursive('./t');
        lib.generateThumbs();
      }*/
    },
    desc: 'generate thumbnail'
  });
};

var deleteFolderRecursive = (path) => {
  if(fs.existsSync(path))
    fs.readdirSync(path).forEach((file,index) => (fs.lstatSync(path+"/"+file).isDirectory())?deleteFolderRecursive(path+"/"+file):fs.unlinkSync(path+"/"+file) );
};
