forked from Sakuya-Izayoi/sbot
-
Notifications
You must be signed in to change notification settings - Fork 4
/
secondary.js
388 lines (360 loc) · 10.1 KB
/
secondary.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import {client} from "./bot"
import got from "got"
import jimp from "jimp"
import skmeans from "skmeans"
// general methods
export function sentLog(msg, text, options) {
console.log(`${(msg.channel.type != "dm") ? msg.channel.id : "by direct messages"} | ${(new Date).toLocaleString("en-US", options)} <${msg.author.tag}> ${text}`)
}
export function getRandomElem(arr) {
return arr[Math.floor(arr.length*Math.random())]
}
export function getSimpleString(str) {
return str.replace(/\s+/g, " ").toLowerCase().replace(/ё/g, "е")
}
export function pluralize(n, arr) {
// by PLAYER_CHAR
let k = n % 10
return arr[(n - k) / 10 % 10 != 1 ? (k != 1 ? ([2, 3, 4].includes(k) ? 1 : 2) : 0) : 2]
}
export function envelope(msg) {
// функция для реакции конвертом
if (msg.channel.type != "dm") {
msg.react("✉")
}
}
export function deleteUserMessage(msg, time) {
if (time === undefined) {
time = 10000
}
if (msg.channel.type != "dm" && msg.guild.member(client.user.id).hasPermission('MANAGE_MESSAGES')) {
msg.delete(time)
.then(() => {})
.catch(error => console.log(error))
return true
} else {
return false
}
}
export function removeElementsByValue(arr) {
let what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
export function indexOfClosestValueInArray(num, arr) {
let curr = 0
for (let i = 0; i < arr.length; i++) {
if (Math.abs(num - arr[i]) < Math.abs(num - arr[curr])) {
curr = i
}
}
return curr
}
export function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
// emoji methods
export function getGuild(guildName, emojiName) {
let guildId
if (!guildName) {
guildId = null
} else if (guildName.match(/^\d+$/g) && client.guilds.get(guildName)) {
guildId = guildName
} else {
let guildIdFound = false
client.guilds.forEach(key => {
if (guildName == getSimpleString(key.name)) {
guildId = key.id
guildIdFound = true
} else if (!guildIdFound && getSimpleString(key.name).startsWith(guildName)) {
if (emojiName) {
let currentGuildId = key.id
client.guilds.get(key.id).emojis.forEach(key => {
if (key.name.toLowerCase().startsWith(emojiName)) {
guildId = currentGuildId
}
})
} else {
guildId = key.id
}
}
})
}
return client.guilds.get(guildId)
}
export function findEmoji(emojiName, guildName) {
let emoji
let emojiFull
if (emojiName.match(/^\d+$/g)) {
let emojiGotById = client.emojis.get(emojiName)
if (emojiGotById) {
return emojiGotById
}
}
let storage = getGuild(guildName, emojiName)
if (!storage) {
storage = client
}
storage.emojis.forEach(key => {
if (emojiName == key.name.toLowerCase()) {
emojiFull = key
} else if (key.name.toLowerCase().startsWith(emojiName)) {
emoji = key
}
})
return (emojiFull) ? emojiFull : emoji
}
export function getEmojiName(emojiText) {
let emojiRaw
if (emojiRaw = emojiText.match(/(?:<:[^:]+:(\d+)>)/)) {
return emojiRaw[1]
} else {
return emojiText
}
}
// special methods
export async function sendAttachmentToImgur(attURL) {
const IMGUR_ID = process.env.IMGUR_ID
if (!IMGUR_ID) {
console.log("Error! No IMGUR_ID here!")
}
let response = await got.post("https://api.imgur.com/3/image", {
headers: {
"authorization": `Client-ID ${IMGUR_ID}`
},
body: {type: "URL", image: attURL},
json: true
})
let imgurData
if (response) {
imgurData = response.body.data
}
if (imgurData && !imgurData.error) {
return [imgurData.link, imgurData.id]
} else {
return false
}
}
export function rgb2hsv([r,g,b]) {
var computedH = 0;
var computedS = 0;
var computedV = 0;
r=r/255; g=g/255; b=b/255;
var minRGB = Math.min(r,Math.min(g,b));
var maxRGB = Math.max(r,Math.max(g,b));
// Black-gray-white
if (minRGB==maxRGB) {
computedV = minRGB;
return [0,0,computedV];
}
// Colors other than black-gray-white:
var d = (r==minRGB) ? g-b : ((b==minRGB) ? r-g : b-r);
var h = (r==minRGB) ? 3 : ((b==minRGB) ? 1 : 5);
computedH = 60*(h - d/(maxRGB - minRGB));
computedS = (maxRGB - minRGB)/maxRGB;
computedV = maxRGB;
return [computedH,computedS,computedV];
}
export function hsv2rgb([h, s, v]) {
h = h / 360
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return [
Math.round(r * 255),
Math.round(g * 255),
Math.round(b * 255)
];
}
export function trimPunc(str) {
return str.match(/^[\s'`"]*([^]+?)[\s'`",.(\)]*$/)[1]
}
export function getRGBFromHex(hex) {
let Red = Math.floor(hex / 256 / 256) % 256
let Green = Math.floor(hex / 256) % 256
let Blue = hex % 256
return [Red, Green, Blue]
}
export function grayFromRGBAHex(hex) {
let colors = getRGBFromHex(hex)
let GrayHue = Math.round(colors[0] * 0.2126 + colors[1] * 0.7152 + colors[2] * 0.0722)
let GrayHex = GrayHue*256*256*256 + GrayHue*256*256 + GrayHue*256 + 255
return GrayHex
}
export async function getMainColorFromImage(link, callback, cnum) {
let dataset = []
try {
jimp.read(link)
.then(image => {
if (image.bitmap.width > 160) {
image.resize(160,jimp.AUTO)
}
if (!cnum) {
cnum = 10
}
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function(x, y, idx) {
if (this.bitmap.data[idx + 3] > 64) {
dataset.push([this.bitmap.data[idx + 0], this.bitmap.data[idx + 1], this.bitmap.data[idx + 2]])
}
if (x == image.bitmap.width - 1 && y == image.bitmap.height - 1) {
let color
let palette = []
if (dataset.length) {
let centroids = skmeans(dataset, cnum, "kmpp", 100).centroids
let hsvColors = []
for (let i = 0; i < centroids.length; i++) {
hsvColors.push(rgb2hsv(centroids[i]))
}
hsvColors.sort((a, b) => {
return (b[1]+0.01)*b[2]*b[2] - (a[1]+0.01)*a[2]*a[2]
})
for (let i = 0; i < hsvColors.length; i++) {
let colorRGB = hsv2rgb(hsvColors[i])
palette.push(colorRGB[0]*256*256 + colorRGB[1]*256 + colorRGB[2])
}
color = palette[0]
} else {
color = undefined
}
if (callback) {
callback(color, palette)
}
}
})
})
.catch(err => {
console.log(err)
})
} catch (err) {
console.log(err)
}
}
export async function recolorByPalette(link, pal, callback) {
let palnum = (pal.length > 20) ? pal.length : 20
await getMainColorFromImage(link, (color, paletteFromImage) => {
paletteFromImage.sort((a,b) => grayFromRGBAHex(b) - grayFromRGBAHex(a))
let paletteFromUser = pal
let paletteReady = []
for (let i = 0; i < paletteFromImage.length; i++) {
let bestSimilarity = Infinity
let colorHex
let cI = getRGBFromHex(paletteFromImage[i])
for (let j = 0; j < paletteFromUser.length; j++) {
let cU = getRGBFromHex(paletteFromUser[j])
let similarity = Math.sqrt((cU[0] - cI[0])**2 + (cU[1] - cI[1])**2 + (cU[2] - cI[2])**2)
if (similarity < bestSimilarity) {
bestSimilarity = similarity
colorHex = paletteFromUser[j]
}
}
paletteReady.push(colorHex)
}
try {
jimp.read(link)
.then(image => {
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function(x, y, idx) {
if (this.bitmap.data[idx + 3] > 64) {
let bestSimilarity = Infinity
let colorHex
let cI = getRGBFromHex(image.getPixelColor(x, y))
for (let j = 0; j < paletteReady.length; j++) {
let cU = getRGBFromHex(paletteReady[j])
let similarity = Math.sqrt((cU[0] - cI[0])**2 + (cU[1] - cI[1])**2 + (cU[2] - cI[2])**2)
if (similarity < bestSimilarity) {
bestSimilarity = similarity
colorHex = paletteReady[j]
}
}
image.setPixelColor(colorHex, x, y)
}
if (x == image.bitmap.width - 1 && y == image.bitmap.height - 1 && callback) {
image.getBuffer("image/png", (err, buf) => {
callback(buf)
})
}
})
})
.catch(err => {
console.log(err)
})
} catch (err) {
console.log(err)
}
}, palnum)
}
export function sftime(arg) {
let d = new Date(1420070400000 + Number(arg) / 4194304)
if (!d.toJSON) {
d = new Date(d)
}
return d
}
export function timeToString(d) {
d.setHours(d.getHours() + 3)
return `\`${d.toJSON().replace(/T/, ' ').replace(/Z/, '')} МСК\``
}
export async function findUser(args) {
let username = getSimpleString(args.join(" "))
let result
let usernameId
// проверка на айди
if (usernameId = username.match(/(?:<@\!?)?(\d{17,20})>?/)) {
await client.fetchUser(usernameId[1]).then(user => {
result = user
})
// проверка на тег
} else if (username.split("#")[1]) {
client.users.forEach(u => {
if (username == u.tag.toLowerCase()) {
result = u
}
})
} else {
let isDisplayNameSuitable = false
let isDisplayNameCanBeSuitable = false
client.guilds.forEach(guild => {
guild.members.forEach(member => {
if (username == getSimpleString(member.displayName)) {
result = member.user
isDisplayNameSuitable = true
} else if (getSimpleString(member.displayName).startsWith(username)) {
if (!isDisplayNameSuitable) {
result = member.user
isDisplayNameCanBeSuitable = true
}
} else if (member.nickname) {
if (username == getSimpleString(member.user.username)) {
if (!isDisplayNameSuitable && !isDisplayNameCanBeSuitable) {
result = member.user
}
} else if (getSimpleString(member.user.username).startsWith(username)) {
if (!result && !isDisplayNameSuitable && !isDisplayNameCanBeSuitable) {
result = member.user
}
}
}
})
})
}
return result
}