-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtools.js
More file actions
166 lines (156 loc) · 5.86 KB
/
tools.js
File metadata and controls
166 lines (156 loc) · 5.86 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
exports.Tools = {
getTimeAgo: function(time) {
time = ~~((Date.now() - time) / 1000);
let seconds = time % 60;
let times = [];
if (seconds) times.push(seconds + (seconds === 1 ? ' second' : ' seconds'));
if (time >= 60) {
time = ~~((time - seconds) / 60);
let minutes = time % 60;
if (minutes) times.unshift(minutes + (minutes === 1 ? ' minute' : ' minutes'));
if (time >= 60) {
time = ~~((time - minutes) / 60);
let hours = time % 24;
if (hours) times.unshift(hours + (hours === 1 ? ' hour' : ' hours'));
if (time >= 24) {
time = ~~((time - hours) / 24);
let days = time % 365;
if (days) times.unshift(days + (days === 1 ? ' day' : ' days'));
if (time >= 365) {
let years = ~~((time - days) / 365);
if (days) times.unshift(years + (years === 1 ? ' year' : ' years'));
}
}
}
}
if (!times.length) return '0 seconds';
return times.join(', ');
},
uncacheTree: function(root) {
let uncache = [require.resolve(root)];
do {
let newuncache = [];
for (let i = 0; i < uncache.length; ++i) {
if (require.cache[uncache[i]]) {
newuncache.push.apply(
newuncache,
require.cache[uncache[i]].children.map(module => module.filename)
);
delete require.cache[uncache[i]];
}
}
uncache = newuncache;
} while (uncache.length > 0);
},
reload: function() {
this.uncacheTree("./commands.js");
try {
Commands = require("./commands.js").commands;
log("ok", "Reloaded commands.js")
}
catch (e) {
log("error", "Unable to load commands.js");
return false;
}
let loaded = [];
let failed = [];
fs.readdirSync('./chat-plugins/').forEach(f => {
try {
this.uncacheTree("./chat-plugins/" + f);
Object.assign(Commands, require("./chat-plugins/" + f).commands);
loaded.push(f);
}
catch (e) {
failed.push(f)
}
});
if (loaded.length) {
log("info", "Loaded command files: " + loaded.join(", "));
}
if (failed.length) {
log("error", "Failed to load: " + failed.join(", "));
return false;
}
return true;
},
matchText: function(str1, str2) {
function matchStrings(first, second) {
// Calculates the similarity between two strings
// taken from: http://phpjs.org/functions/similar_text
if (first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined') {
return 0;
}
first += '';
second += '';
let pos1 = 0,
pos2 = 0,
max = 0,
firstLength = first.length,
secondLength = second.length,
p, q, l, sum;
max = 0;
for (p = 0; p < firstLength; p++) {
for (q = 0; q < secondLength; q++) {
for (l = 0;
(p + l < firstLength) && (q + l < secondLength) && (first.charAt(p + l) === second.charAt(q + l)); l++);
if (l > max) {
max = l;
pos1 = p;
pos2 = q;
}
}
}
sum = max;
if (sum) {
if (pos1 && pos2) {
sum += matchStrings(first.substr(0, pos2), second.substr(0, pos2));
}
if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) {
sum += matchStrings(first.substr(pos1 + max, firstLength - pos1 - max), second.substr(pos2 + max, secondLength - pos2 - max));
}
}
return sum;
}
if (!str1 || !str2) return 0;
let length = str1.length > str2.length ? str1.length : str2.length;
let match = matchStrings(str1.toLowerCase(), str2.toLowerCase()) * 100;
return match / length;
},
regexify: function(string) {
if (!string) return "";
return string.split("").map(l => (/[a-zA-Z0-9\s]/i.test(l) ? l : "\\" + l)).join("");
},
uploadToHastebin: function(toUpload, callback) {
if (typeof callback !== 'function') return false;
let reqOpts = {
hostname: 'hastebin.com',
method: 'POST',
path: '/documents'
};
let req = require('https').request(reqOpts, res => {
res.on('data', chunk => {
// CloudFlare can go to hell for sending the body in a header request like this
let filename;
try {
filename = JSON.parse(chunk).key;
}
catch (e) {
if (typeof chunk === 'string' && /^[^\<]*\<!DOCTYPE html\>/.test(chunk)) {
callback('Cloudflare-related error uploading to Hastebin: ' + e.message);
}
else {
callback('Unknown error uploading to Hastebin: ' + e.message);
}
}
callback('http://hastebin.com/raw/' + filename);
});
});
req.on('error', e => {
callback('Error uploading to Hastebin: ' + e.message);
//throw e;
});
req.write(toUpload);
req.end();
},
};