endcancer

This commit is contained in:
Flummi 2016-08-14 02:01:16 +02:00
parent 959ba2a84d
commit 039ca5dd80

View File

@ -1,5 +1,6 @@
var cfg = require('../../cfg.json');
var fs = require('fs');
var http = require("http");
var https = require("https");
var mysql = require('mysql');
var bot = require('coffea')();
@ -28,8 +29,8 @@ bot.on('motd', (e) => {
bot.on('message', (e) => {
var orig = e.message;
if(e.channel.getName().toString() == "#f0ck") { // (debug) only in channel #f0ck
if(orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi)) { // shitpostcatcher
var tmp = orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links
if(orig.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi)) { // shitpostcatcher
var tmp = orig.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links
tmp.forEach((entry,i,a) => {
dl(entry, "./b/"+path.parse(entry).base, (cb) => { // download item
e.reply(cb);
@ -53,16 +54,42 @@ var getUser = (u, n) => {
var dl = (url, dest, cb) => {
var file = fs.createWriteStream(dest);
if(url.match(/https/)) {
var request = https.get(url, (response) => {
try {
response.pipe(file);
file.on('finish', () => {
file.close();
cb('downloaded '+dest);
cb('(https) downloaded '+dest);
});
file.on('error', (err) => {
fs.unlink(dest);
file.close();
cb(err.message);
});
}
catch(ex) {
e.reply(ex);
}
});
}
else {
var request = http.get(url, (response) => {
try {
response.pipe(file);
file.on('finish', () => {
file.close();
cb('(http) downloaded '+dest);
});
file.on('error', (err) => {
fs.unlink(dest);
file.close();
cb(err.message);
});
}
catch(ex) {
e.reply(ex);
}
});
}
}