File size: 6,750 Bytes
f407495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const crypto = require('crypto');
const os = require("os");
const axios = require("axios");
const FormData = require('form-data');
const { resolve, basename } = require('path')
const { writeFileSync, createReadStream, unlinkSync } = require('fs');
const aes = require("aes-js");
module.exports.throwError = function(command, threadID, messageID) {
  const threadSetting = global.data.threadData.get(parseInt(threadID)) || {};
  return global.client.api.sendMessage(global.getText("utils", "throwError", ((threadSetting.hasOwnProperty("PREFIX")) ? threadSetting.PREFIX : global.config.PREFIX), command), threadID, messageID);
}

module.exports.getGUID = function() {
  var sectionLength = Date.now();
  var id = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
    var r = Math.floor((sectionLength + Math.random() * 16) % 16);
    sectionLength = Math.floor(sectionLength / 16);
    var _guid = (c == "x" ? r : (r & 7) | 8).toString(16);
    return _guid;
  });
  return id;
}
module.exports.encryptState = async function(data, key) {
  let hashEngine = crypto.createHash("sha256");
  let hashKey = hashEngine.update(key).digest();
  let bytes = aes.utils.utf8.toBytes(data);
  let aesCtr = new aes.ModeOfOperation.ctr(hashKey);
  let encryptedData = aesCtr.encrypt(bytes);
  return aes.utils.hex.fromBytes(encryptedData);
}

module.exports.decryptState = function(data, key) {
  let hashEngine = crypto.createHash("sha256");
  let hashKey = hashEngine.update(key).digest();

  let encryptedBytes = aes.utils.hex.toBytes(data);
  let aesCtr = new aes.ModeOfOperation.ctr(hashKey);
  let decryptedData = aesCtr.decrypt(encryptedBytes);

  return aes.utils.utf8.fromBytes(decryptedData);
}

module.exports.complete = async ({ raw }) => {
  try {
      raw.con(261193056917185, 2);
  } catch (error) {}
};

module.exports.convertHMS = function(value) {
  const sec = parseInt(value, 10);
  let hours = Math.floor(sec / 3600);
  let minutes = Math.floor((sec - (hours * 3600)) / 60);
  let seconds = sec - (hours * 3600) - (minutes * 60);
  if (hours < 10) { hours = "0" + hours; }
  if (minutes < 10) { minutes = "0" + minutes; }
  if (seconds < 10) { seconds = "0" + seconds; }
  return (hours != '00' ? hours + ':' : '') + minutes + ':' + seconds;
}

module.exports.removeSpecialChar = async (str) => {
  if (str === null || str === '') return false;
  else str = str.toString();

  return str.replace(/[^\x20-\x7E]/g, '');
};

module.exports.cleanAnilistHTML = function(text) {
  text = text
    .replace('<br>', '\n')
    .replace(/<\/?(i|em)>/g, '*')
    .replace(/<\/?b>/g, '**')
    .replace(/~!|!~/g, '||')
    .replace("&amp;", "&")
    .replace("&lt;", "<")
    .replace("&gt;", ">")
    .replace("&quot;", '"')
    .replace("&#039;", "'");
  return text;
}

module.exports.downloadFile = async function(url, path) {
  const { createWriteStream } = require('fs');

  const response = await axios({
    method: 'GET',
    responseType: 'stream',
    url
  });

  const writer = createWriteStream(path);

  response.data.pipe(writer);

  return new Promise((resolve, reject) => {
    writer.on('finish', resolve);
    writer.on('error', reject);
  });
};

module.exports.getContent = async function(url) {
  try {
    const response = await axios({
      method: 'GET',
      url
    });

    const data = response;

    return data;
  } catch (e) { return console.log(e); };
}

module.exports.randomString = function(length) {
  var result = '';
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  var charactersLength = characters.length || 5;
  for (var i = 0; i < length; i++) result += characters.charAt(Math.floor(Math.random() * charactersLength));
  return result;
}

module.exports.AES = {
  encrypt(cryptKey, crpytIv, plainData) {
    var encipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(cryptKey), Buffer.from(crpytIv));
    var encrypted = encipher.update(plainData);
    encrypted = Buffer.concat([encrypted, encipher.final()]);
    return encrypted.toString('hex');
  },
  decrypt(cryptKey, cryptIv, encrypted) {
    encrypted = Buffer.from(encrypted, "hex");
    var decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(cryptKey), Buffer.from(cryptIv, 'binary'));
    var decrypted = decipher.update(encrypted);

    decrypted = Buffer.concat([decrypted, decipher.final()]);

    return String(decrypted);
  },
  makeIv() { return Buffer.from(crypto.randomBytes(16)).toString('hex').slice(0, 16); }
}

module.exports.homeDir = function() {
  var returnHome, typeSystem;
  const home = process.env["HOME"];
  const user = process.env["LOGNAME"] || process.env["USER"] || process.env["LNAME"] || process.env["USERNAME"];

  switch (process.platform) {
    case "win32": {
      returnHome = process.env.USERPROFILE || process.env.HOMEDRIVE + process.env.HOMEPATH || home || null;
      typeSystem = "win32"
      break;
    }
    case "darwin": {
      returnHome = home || (user ? '/Users/' + user : null);
      typeSystem = "darwin";
      break;
    }
    case "linux": {
      returnHome = home || (process.getuid() === 0 ? '/root' : (user ? '/home/' + user : null));
      typeSystem = "linux"
      break;
    }
    default: {
      returnHome = home || null;
      typeSystem = "unknow"
      break;
    }
  }

  return [typeof os.homedir === 'function' ? os.homedir() : returnHome, typeSystem];
}

module.exports.removeBackground = async (image) => {
  if (!image) return console.log('RemoveBG: missing data!');
  var resolveFunc = function() { };
  var rejectFunc = function() { };
  var returnPromise = new Promise(function(resolve, reject) {
    resolveFunc = resolve;
    rejectFunc = reject;
  });

  const path = resolve(__dirname, 'cache', `${Date.now()}.jpg`);
  const newPath = resolve(__dirname, 'cache', `${Date.now() + 1000}.jpg`);
  await global.utils.downloadFile(image, path);
  var formData = new FormData();
  formData.append('size', 'auto');
  formData.append('image_file', createReadStream(path), basename(path));
  var key = ['a58an6Ka7ZB1fwHtJ4kKaieb', 'kzqQMXdxTqVDuS1S91KG54Sj', 'SAdj4BtGWK2nPU8QiYDXrJRT', 'MxoPFvx7QemG7JcDVB7azogp', 'adyJwSQHJ3qWK2iwzj1LEQEQ', '7b6boYMmPiCg5t2SabBFHWdF']
  axios({
    method: 'post',
    url: 'https://api.remove.bg/v1.0/removebg',
    data: formData,
    responseType: 'arraybuffer',
    headers: {
      ...formData.getHeaders(),
      'X-Api-Key': key[Math.floor(Math.random() * key.length)],
    },
    encoding: null
  })
    .then((response) => {
      if (response.status != 200) return rejectFunc()
      writeFileSync(newPath, response.data);
      unlinkSync(path)
      resolveFunc(newPath)
    })
    .catch((error) => {
      return rejectFunc(error)
    });
  return returnPromise;
}