fucking request

This commit is contained in:
Flummi 2016-08-14 00:16:56 +02:00
parent 1e4a6af1b5
commit c9ec376a88

View File

@ -1,4 +1,5 @@
var cfg = require('../../cfg.json');
var fs = require('fs');
var request = require('request');
var mysql = require('mysql');
var bot = require('coffea')();
@ -31,18 +32,52 @@ bot.on('message', (e) => {
if(e.channel.getName().toString() == "#f0ck") {
/*var u = "f0ck";
bot.whois(u, e.network, (m)=>{ });
setTimeout(() => {
var user = bot.getUser(u, e.network);
e.reply(user);
}, 2000);*/
if(orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi)) {
var tmp = orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi);
tmp.forEach((entry,i,a) => {
dl(entry, "./b/test.png", (cb) => {
e.reply(cb);
});
});
}
else if(orig.match(/^\.test/)) {
var tmp = getUser(e.user.getNick(), e.network);
setTimeout(()=>{
e.reply(tmp);
}, 2000);
}
}
});
var getUser = (u, n) => {
bot.whois(u, n, (fn)=>{ });
bot.getUser(u, n);
var start = Date.now();
while (Date.now() < start + 3000) {}
return bot.getUser(u, n);
};
var dl = (url, dest, cb) => {
var file = fs.createWriteStream(dest);
var sendReq = request.get(url);
sendReq.on('response', (response) => {
if(response.statusCode !== 200) return cb('Response status was ' + response.statusCode);
});
sendReq.on('error', (err) => {
fs.unlink(dest);
if(cb) return cb(err.message);
});
sendReq.pipe(file);
file.on('finish', () => {
file.close(cb);
});
file.on('error', (err) => {
fs.unlink(dest);
if(cb) return cb(err.message);
});
};