-
Notifications
You must be signed in to change notification settings - Fork 4
/
secondary.js
386 lines (354 loc) · 11.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
import * as c from "./commands";
import {client, botID} from "./bot";
let timeoutForAutoReact;
let whoNeedsToReactToSomething = {};
let whichGuildThisUserMeans = {};
export function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
export function getRandomElem(arr) {
return arr[Math.floor(arr.length*Math.random())];
}
export function getSimpleString(str) {
return str.replace(/\n/g, " ").replace(/ +/g, " ").toLowerCase().replace(/ё/g, "е").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 == "text") {
msg.react("✉");
}
}
export function getGuild(guildName) {
if (!guildName) {
return null;
}
if (guildName.match(/^\d+$/g)) {
if (client.guilds.get(guildName)) {
return guildName;
}
}
let guildId;
let guildIdFull;
client.guilds.forEach(key => {
if (guildName == getSimpleString(key.name)) {
guildIdFull = key.id;
} else if (getSimpleString(key.name).match(new RegExp("^(" + escapeRegExp(guildName) + ")"))) {
guildId = key.id;
}
});
if (!(guildId || guildIdFull)) {
return;
}
return (guildIdFull) ? guildIdFull : guildId;
}
export function getStorage(emojiName, guildName, channel) {
if (guildName) {
if (guildName.match(/^\d+$/g)) {
if (client.guilds.get(guildName)) {
return client.guilds.get(guildName);
}
}
let guildId;
let guildIdFull;
client.guilds.forEach(key => {
if (guildName == getSimpleString(key.name)) {
guildIdFull = key.id;
} else if (getSimpleString(key.name).match(new RegExp("^(" + escapeRegExp(guildName) + ")"))) {
let currentGuildId = key.id;
client.guilds.get(key.id).emojis.forEach(key => {
if (key.name.toLowerCase().match(new RegExp("^(" + escapeRegExp(emojiName) + ")"))) {
guildId = currentGuildId;
}
});
}
});
if (!(guildId || guildIdFull)) {
return client;
}
return (guildIdFull) ? client.guilds.get(guildIdFull) : client.guilds.get(guildId);
} else {
return client;
}
}
export function findEmoji(emojiName, guildName, channel) {
let emoji;
let emojiFull;
if (emojiName.match(/^\d+$/g)) {
if (client.emojis.get(emojiName)) {
emoji = client.emojis.get(emojiName);
return emoji;
}
}
let storage = getStorage(emojiName, guildName, channel);
if (!storage) {
return;
}
storage.emojis.forEach(key => {
if (emojiName == key.name.toLowerCase()) {
emojiFull = key;
} else if (key.name.toLowerCase().match(new RegExp("^(" + escapeRegExp(emojiName) + ")"))) {
emoji = key;
}
});
return (emojiFull) ? emojiFull : emoji;
}
export function getEmojiName(emojiText) {
let emojiRaw;
if (emojiRaw = emojiText.match(/(?:<:[^:]+:(\d+)>)/)) {
return emojiRaw[1];
} else {
return emojiText;
}
}
export function findUserToGetAvatar(username) {
// проверка на айди
if (username.match(/^\d+$/g)) {
if (client.users.get(username)) {
return client.users.get(username);
}
}
// проверка на призывалку
let usernameId = username.match(/<@\!?(\d+)>/);
if (usernameId) {
if (client.users.get(usernameId[1])) {
return client.users.get(usernameId[1]);
}
}
let guildsCounter = 0;
let isDisplayNameSuitable = false;
let isDisplayNameCanBeSuitable = false;
let result;
client.guilds.forEach(guild => {
let usersCounter = 0;
guild.members.forEach(member => {
if (member.user.avatar) {
if (username == getSimpleString(member.displayName)) {
result = member.user;
isDisplayNameSuitable = true;
} else if (getSimpleString(member.displayName).match(new RegExp("^(" + escapeRegExp(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).match(new RegExp("^(" + escapeRegExp(username) + ")"))) {
if (!result && !isDisplayNameSuitable && !isDisplayNameCanBeSuitable) {
result = member.user;
}
}
}
}
});
});
return result;
}
export function autoreact(msg, args, isCommandCanBeAnEmoji) {
if (!args[0]) {
msg.react("📜");
return;
};
let emojiName;
let guildName;
let messageId = args[1];
let guildCheck;
emojiName = getEmojiName(args[0]);
let emojiError = ["👋", "😶", "🤔", "351002389991653378", "358952906248028160", "357960229259837440", "520641845634531328"];
if (guildCheck = emojiName.match(/^([^:]+)(?::(\S+))$/)) {
emojiName = guildCheck[1];
guildName = guildCheck[2];
}
if (messageId) {
let emoji = findEmoji(emojiName, guildName, msg.channel);
if (!emoji) {
if (isCommandCanBeAnEmoji) {
msg.react(getRandomElem(emojiError));
} else {
msg.react("343057042862243840");
}
return;
};
let wrongChannels = 0;
client.channels.forEach(key => {
if (key.type == "text") {
key.fetchMessage(messageId)
.then(messageToReact => {
messageToReact.react(emoji);
msg.react("⏳");
let removeReactionTimeout = setTimeout(() => messageToReact.reactions.get(emoji.name + ":" + emoji.id).remove(client.user), 25000);
messageToReact.awaitReactions((reaction, user) => {
if (user.id == msg.author.id && reaction.emoji.id == emoji.id) {
messageToReact.reactions.get(emoji.name + ":" + emoji.id).remove(client.user);
clearTimeout(removeReactionTimeout);
}
}, { time: 25000 });
})
.catch(() => {
if (++wrongChannels == client.channels.size) {
msg.react("🤷");
}
});
} else {
wrongChannels++;
}
});
} else {
if (!findEmoji(emojiName, guildName, msg.channel)) {
if (isCommandCanBeAnEmoji) {
msg.react(getRandomElem(emojiError));
} else {
msg.react("343057042862243840");
}
return;
}
msg.react("👌");
whoNeedsToReactToSomething[msg.author.id] = emojiName;
whichGuildThisUserMeans[msg.author.id] = guildName;
}
deleteUserMessage(msg);
}
export function deleteUserMessage(msg) {
if (msg.channel.type == "text") { // если бот не начал "беседовать" с юзером
let bot_permissions = msg.channel.permissionsFor(client.user);
if (bot_permissions.has("MANAGE_MESSAGES")) {
msg.delete(10000)
.then(() => {})
.catch(error => console.log(error));
}
}
}
export function sendEmojiLinkEmbed(msg, emoji) {
if (emoji.animated) {
msg.channel.send({embed: {title: "Emoji", description: ("<a:" + emoji.name + ":" + emoji.id + "> – " + emoji.name), image: {url: ("https://cdn.discordapp.com/emojis/" + emoji.id + ".gif")}}});
} else {
msg.channel.send({embed: {title: "Emoji", description: ("<:" + emoji.name + ":" + emoji.id + "> – " + emoji.name), image: {url: ("https://cdn.discordapp.com/emojis/" + emoji.id + ".png")}}});
}
}
export function sendUserAvatarEmbed(msg, user) {
let avaTemp = user.avatarURL;
// console.log(avaTemp);
let avaTempRE = avaTemp.match(/^((?:.*)\.(\w+))/);
let isAvaGif = (avaTempRE[2] == "gif") ? true : false;
let avatarURLFixed = isAvaGif ? avaTemp + "?size=2048" : avaTemp;
msg.channel.send({embed: {title: "Avatar", description: user.tag, image: {url: avatarURLFixed}}});
// msg.channel.send({embed: {title: "Avatar", description: user.tag, image: {url: avaTemp}}});
// if (isAvaGif) {
// msg.channel.send("В полном размере:\n<" + avaTemp + ">");
// }
}
export function isThisBotsChannel(msg) {
if (msg.channel.type == "text" && msg.channel.guild.id == "110107304413638656" && msg.channel.id != "334369998866874369") {
return false;
} else {
return true;
}
}
export function showHomestuckPage(msg, comic_embed, usedArrowButton, contentText) {
let embed = {embed: comic_embed};
if (usedArrowButton) {
if (contentText) {
msg.edit(contentText, embed);
} else {
msg.edit(embed);
}
} else {
let contentToSend = (contentText) ? contentText : embed;
msg.channel.send(contentToSend)
.then((msg) => {
msg.react("⬅")
.then(() => {
msg.react("➡");
})
.catch(error => console.log(error));
})
.catch(error => console.log(error));
}
}
export function checkHomestuckReaction(messageReaction, user) {
let msg = messageReaction.message;
let msgReaction = messageReaction.emoji.name;
if (["⬅", "➡"].includes(msgReaction) && msg.author.id == botID && user.id != botID) {
let msg = messageReaction.message;
let cMatch, eMatch, page_number;
if (cMatch = msg.content.match(/hs#(\d+)/)) {
page_number = Number(cMatch[1]);
} else if (msg.embeds[0] && (eMatch = msg.embeds[0].author.name.match(/hs#(\d+)/))) {
page_number = Number(eMatch[1]);
}
if (page_number) {
if (msgReaction == "➡") {
c.Homestuck(msg, [page_number + 1], null, true);
} else if (msgReaction == "⬅") {
c.Homestuck(msg, [page_number - 1], null, true);
}
}
}
}
export function setCinemaRole(user, doesUserNeedRole) {
client.guilds.get("110107304413638656").fetchMember(user.id)
.then((member) => {
if (doesUserNeedRole) {
console.log(member.user.tag + " asked to add them Cinema role.");
member.addRole("542396782878130206")
.then(() => {})
.catch(error => console.log(error));
} else {
console.log(member.user.tag + " asked to remove them Cinema role.");
member.removeRole("542396782878130206")
.then(() => {})
.catch(error => console.log(error));
}
})
.catch(error => console.log(error));
}
export function sfTime(s) {
return new Date(1420070400000 + s / 4194304);
}
export function dateStr(d) {
if (!d.toJSON) {
d = new Date(d);
}
d.setHours(d.getHours() + 3);
return d.toJSON().split(".")[0].replace(/T/, ' ') + ' МСК';
}
export function checkReactionForAutoreact(messageReaction, user) {
if (whoNeedsToReactToSomething[user.id]) {
let currentUser = client.users.get(user.id);
let currentEmoji = findEmoji(whoNeedsToReactToSomething[user.id], whichGuildThisUserMeans[user.id]);
messageReaction.message.react(currentEmoji);
clearTimeout(timeoutForAutoReact);
delete whoNeedsToReactToSomething[user.id];
delete whichGuildThisUserMeans[user.id];
let timerForDeletingAutoReaction = setTimeout(() => {
messageReaction.message.reactions.get(currentEmoji.name + ":" + currentEmoji.id).remove(client.user);
}, 25000);
messageReaction.message.awaitReactions((messageReactionAwaited, user) => {
if (user.id == currentUser.id && messageReactionAwaited.emoji.id == currentEmoji.id) {
messageReactionAwaited.message.reactions.get(messageReactionAwaited.emoji.name + ":" + messageReactionAwaited.emoji.id).remove(client.user);
clearTimeout(timerForDeletingAutoReaction);
}
}, { time: 25000 });
return true;
} else {
return false;
}
}
export function checkEmojiListReaction(msgReaction, user, msg, serverArray) {
if (msg.author.id == botID && user.id != botID) {
let turn = "";
if (msgReaction == "⬅") {
turn = "-";
} else if (msgReaction == "➡") {
turn = "+";
}
c.EmojiList(msg, [turn], false, true, serverArray);
}
}