-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
99 lines (93 loc) · 2.46 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const Discord = require('discord.js');
const client = new Discord.Client();
var fs = require('fs'),
request = require('request');
var images = require("images");
var token = require('./ressources/token.js').token;
var download = function(uri, filename, callback, message){
request.head(uri, function(err, res, body){
if(!res)
{
message.channel.send(message.author + " Is this a real file? :thonking:");
return;
}
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
overlay = 'ressources/IntoTheBreachOverlay.png'
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
var num = 0;
client.on('message', msg => {
if(!msg.author.bot)
{
if (msg.channel.name === 'strategy' || msg.channel.name.indexOf('spam')!=-1)
{
url = '';
text = msg.content;
if(text.startsWith("overlay"))
{
array = msg.attachments.array()
if(array.length != 0)
{
url = array[0].url;
content = msg.content.substring(8).trim();
}
else
{
sub = text.substring(8).trim();
i = sub.indexOf("http://");
if(i==-1)
{
i = sub.indexOf("https://")
}
if(i==-1)
{
msg.channel.send(msg.author + " This isn't a valid url!");
return;
}
url = sub.substring(i).trim();
content = sub.substring(0, i).trim();
}
var to = 'ressources/tomerge_' + num;
var saveto = 'ressources/output_' + num + ".jpg"
num++
download(url, to, function()
{
try
{
image_to = images(to);
}
catch (err)
{
msg.channel.send(msg.author + " I'm pretty sure this isn't an image! You sneaky fox.");
return;
}
image_overlay = images(overlay).resize(image_to.width())
diff = (image_to.height() - image_overlay.height())/2
if(diff>0)
{
images(to).draw(image_overlay, 0, diff).save(saveto)
}
else
{
image_overlay = images(image_overlay, 0, -diff, image_to.width(), image_to.height())
images(to).draw(image_overlay, 0, 0).save(saveto)
}
opts = {};
opts.file = saveto;
msg.channel.send(msg.author + (content.length != 0 ? ': ' + content : ''), opts).then( reply =>
{
fs.unlinkSync(to);
fs.unlinkSync(saveto);
msg.delete();
})
}, msg);
}
}
}
});
client.login(token);