-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.js
149 lines (125 loc) · 4.39 KB
/
functions.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
const yes = ['yes', 'y', 'ye', 'yea', 'correct'];
const no = ['no', 'n', 'nah', 'nope', 'fuck off'];
const MONEY = ['', 'k', 'M', 'G', 'T', 'P', 'E'];
const inviteRegex = /(https?:\/\/)?(www\.|canary\.|ptb\.)?discord(\.gg|(app)?\.com\/invite|\.me)\/([^ ]+)\/?/gi;
const botInvRegex = /(https?:\/\/)?(www\.|canary\.|ptb\.)?discord(app)\.com\/(api\/)?oauth2\/authorize\?([^ ]+)\/?/gi;
module.exports = {
getMember(message, toFind = '') {
toFind = toFind.toLowerCase();
const target = message.guild.members.cache.get(toFind) ||
(message.mentions.members && message.mentions.members.first()) ||
message.guild.members.cache.find(member => {
return member.displayName.toLowerCase().includes(toFind) ||
member.user.tag.toLowerCase().includes(toFind);
}) ||
message.member;
return target;
},
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
formatDate(date) {
return new Intl.DateTimeFormat('en-US').format(date);
},
async promptMessage(message, author, time, validReactions) {
time *= 1000;
for (const reaction of validReactions) {
await message.react(reaction);
}
const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && user.id === author.id;
try {
const collected = await message.awaitReactions(filter, { max: 1, time });
const reaction = collected.first();
return reaction && reaction.emoji.name;
} catch (error) {
return null;
}
},
drawImageWithTint(ctx, image, color, x, y, width, height) {
const { fillStyle, globalAlpha } = ctx;
ctx.fillStyle = color;
ctx.drawImage(image, x, y, width, height);
ctx.globalAlpha = 0.5;
ctx.fillRect(x, y, width, height);
ctx.fillStyle = fillStyle;
ctx.globalAlpha = globalAlpha;
},
randomRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
shuffle(array) {
const arr = [...array];
for (let i = arr.length - 1; i >= 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
},
async verify(channel, user, { time = 30000, extraYes = [], extraNo = [] } = {}) {
const filter = res => {
const value = res.content.toLowerCase();
return (!user || res.author.id === user.id) &&
(yes.includes(value) || no.includes(value) || extraYes.includes(value) || extraNo.includes(value));
};
try {
const collected = await channel.awaitMessages(filter, { max: 1, time });
const choice = collected.first().content.toLowerCase();
return yes.includes(choice) || extraYes.includes(choice);
} catch (error) {
return false;
}
},
chunk(array, chunkSize) {
const temp = [];
for (let i = 0; i < array.length; i += chunkSize) {
temp.push(array.slice(i, i + chunkSize));
}
return temp;
},
getWrapText(ctx, text, maxWidth) {
const words = text.split(' ');
const lines = [];
let line = '';
for (const word of words) {
if (ctx.measureText(`${line}${word}`).width < maxWidth) {
line += `${word} `;
} else {
lines.push(line.trim());
line = `${word} `;
}
}
lines.push(line.trim());
return lines;
},
crFormat(number) {
const ranking = Math.log10(number) / 3 | 0;
if (!ranking) return number.toString();
const last = MONEY[ranking];
const scale = Math.pow(10, ranking * 3);
const scaled = number / scale;
return `${scaled.toFixed(2)}${last}`;
},
formatNumber(number, minimumFractionDigits = 0) {
return Number.parseFloat(number).toLocaleString(undefined, {
minimumFractionDigits,
maximumFractionDigits: 2
});
},
list(arr, conj = 'and') {
const len = arr.length;
if (len === 0) return '';
if (len === 1) return arr[0];
return `${arr.slice(0, -1).join(', ')}${len > 1 ? `${len > 2 ? ',' : ''} ${conj} ` : ''}${arr.slice(-1)}`;
},
firstUpperCase(text, split = ' ') {
return text.split(split).map(word => `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join(' ');
},
shorten(text, maxLen = 2000) {
return text.length > maxLen ? `${text.substr(0, maxLen - 3)}...` : text;
},
stripInvites(str, { guild = true, bot = true, text = '[redacted invite]' } = {}) {
if (guild) str = str.replace(inviteRegex, text);
if (bot) str = str.replace(botInvRegex, text);
return str;
},
};