diff --git a/src/trigger/clean.js b/src/trigger/clean.js new file mode 100644 index 0000000..028522d --- /dev/null +++ b/src/trigger/clean.js @@ -0,0 +1,45 @@ +var fs = require('fs-extra'); + +module.exports = (bot, trigger, lib) => { + trigger.add({ + name: 'clean', + call: /^\!clean/i, + level: 100, + active: 1, + func: (e) => { + var dir_b = fs.readdirSync('./b/'); // Items + var dir_t = fs.readdirSync('./t/'); // Thumbnails + var items_id = []; + var items_dest = []; + + var failed_ids = []; + var failed_dests = []; + + lib.sql.query("select `id`, `dest` from `f0ck`.`items`", (err, rows, fields) => { + rows.forEach((e,i,a) => { + items_id.push(e.id); + items_dest.push(e.dest.replace('./b/', '')); + }); + + dir_b.forEach((e,i,a) => { + if(!(e in items_dest)) { + failed_dests.push(e); + //fs.unlink('./b/'+e); + } + }); + dir_t.forEach((e,i,a) => { + if(!(e.replace('.png', '') in items_id)) { + failed_ids.push(e); + //fs.unlink('./t/'+e); + } + }); + + e.reply('unused thumbnails: ' + failed_ids.length + ' ; unused dests: ' + failed_dests.length); + //console.log(failed_dests); + }).on('error', (err) => { + e.reply(err); + }); + }, + desc: 'testtrigger' + }); +}; \ No newline at end of file