-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
97 lines (82 loc) · 2.55 KB
/
utils.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
const fs = require("fs");
const request = require("request");
class Utils {
constructor(){};
log( content, color ) {
const col = {
red : "\x1b[31m%s\x1b[0m",
green : "\x1b[32m%s\x1b[0m",
yellow : "\x1b[33m%s\x1b[0m",
blue : "\x1b[34m%s\x1b[0m",
magenta : "\x1b[35m%s\x1b[0m",
cyan : "\x1b[36m%s\x1b[0m"
}
let output = `[${ new Date().toLocaleString() }] ${ content }`;
switch( color ) {
case 'red':
console.log( `${ col.red }`, `${ output }` );
break;
case 'green':
console.log( `${ col.green }`, `${ output }` );
break;
case 'yellow':
console.log( `${ col.yellow }`, `${ output }` );
break;
case 'blue':
console.log( `${ col.blue }`, `${ output }` );
break;
case 'magenta':
console.log( `${ col.magenta }`, `${ output }` );
break;
case 'cyan':
console.log( `${ col.cyan }`, `${ output }` );
break;
default:
console.log( `${ col.output }` );
break;
}
}
sanitize( string ) {
const illegalChars = [ '\\', '/', ':', '*', '?', '"', '<', '>', '|' ];
for( let i = 0; i < string.length; i++ ) {
if( illegalChars.includes( string.charAt( i ) ) )
string = string.replace( string.charAt( i ), '-' );
}
return string;
}
download( uri, filename, callback ) {
request.head( uri, function( err, res, body ){
request( uri ).pipe( fs.createWriteStream( filename ) ).on( 'close', callback );
});
return;
};
dump( client, inputID, _callback ) {
const root = `./dumps`;
let time = Date.now();
let guild = client.guilds.find( guild => guild.id === inputID );
let status = 0;
if(!guild){
this.log( "[:/] Something went wrong! Please try again...", "green" );
return _callback();
}
let emojis = guild.emojis.array();
if( !fs.existsSync( root ) ) fs.mkdirSync( root );
this.log( "[!] Creating dump directory. . .", "green" );
fs.mkdirSync( `${ root }/${ this.sanitize( guild.name ) }_dump_${ time }` );
this.log( `[+] Downloading ${ emojis.length } emojis. . .`, "green" );
for( let i = 0; i < emojis.length; i++ ) {
let name = emojis[ i ].identifier.split( ":" )[ 0 ];
let ext = emojis[ i ].animated ? "gif" : "png";
this.download( emojis[ i ].url, `${`${ root }/${ this.sanitize( guild.name ) }_dump_${ time }`}/${ name }.${ ext }`, () => {
this.log( `[+] Downloaded emoji ${ name } (saved as .${ ext })!`, "green" );
++status;
if( status >= emojis.length ) {
status = 0;
this.log( `[!] Finished dumping guild->${ inputID }!\n\n`, "green" );
_callback();
}
} );
}
}
}
module.exports = Utils;