-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.js
173 lines (158 loc) · 4.53 KB
/
tools.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
const { Permissions, TextChannel, GuildMember } = require('discord.js')
/**
* Generate a random int between min and max
* @param {Number} min
* @param {Number} max
* @returns {Number}
*/
function random(min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1)) + min
}
/**
* get the percentage of x to y
* @param {Number} x
* @param {Number} y
*/
function getPercentage(x, y) {
return (x / y) * 100
}
/**
* @param {number} a
* @param {number} b
* @param {number} threshold
*/
function isInRange(a, b, threshold = 1000) {
// round to nearest threshold
a = Math.round(a / threshold) * threshold
b = Math.round(b / threshold) * threshold
return a % b
}
/**
* check if hex is valid
* @param {String} hex
* @example
* isHex('#ffffff') // true
* isHex('#fff') // true
* isHex('#fffffff') // false
* isHex('#fffffff') // false
* isHex('#ffffff') // true
* isHex('#FFaeFb') // true
* ishHex('FFaeFb') // true
* isHex('123456') // true
*/
function isHex(hex) {
if (!hex.startsWith('#')) hex = '#' + hex
return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(hex)
}
/**
* mark word at offset and length with "
* @param {string} text
* @param {number} offset
* @param {number} length
* @param {string} marking
* @returns {string}
* @example
* markWord('hello world', 5, 5) // 'hello "world"'
* markWord('das ist ein test', 4, 3 ) // 'das "ist" ein test'
* markWord('das ist ein test', 4, 3, '*' ) // 'das *ist* ein test'
*/
function markWord(text, offset, length, marking = '"') {
var before = text.substring(0, offset)
var after = text.substring(offset + length)
return `${before}${marking}${text.substring(offset, offset + length)}${marking}${after}`
}
/**
* this removes one entry of the array with that name
* @param {any[]} arr
* @param {any} name
* @returns {any[]} the new array
*
* @example
* removeElemByName(['a', 'b', 'c'], 'b') // ['a', 'c']
* removeElemByName(['a', 'b', 'c'], 'd') // ['a', 'b', 'c']
* removeElemByName(['a', 'b', 'c', 'b'], 'b') // ['a', 'c', 'b']
*/
function removeElemByName(arr, name) {
var index = arr.indexOf(name)
if (index == -1) return arr
return arr.filter((elem, i) => i != index)
}
/**
* @param {TextChannel | null} channel
* @param {GuildMember | null | undefined} member
*/
function hasPermissionToSendMessages(channel, member = channel?.guild?.members.me) {
if (!channel) return false
// @ts-ignore
let permissions = channel.permissionsFor(member)
return !(permissions && (!permissions.has(Permissions.FLAGS.SEND_MESSAGES) || !permissions.has(Permissions.FLAGS.VIEW_CHANNEL)))
}
/**
* @param {{"":{ broadcastChannel: string, users:{ id: string, wentOnline: number, wentOffline: number }[] }}} dbData
*/
function getStatsFromDB(dbData) {
let res = {
managingGuilds: 0,
managingUsers: 0,
avgUptime: 0,
avgDowntime: 0
}
for (let guildId in dbData) {
if (guildId == '') continue
res.managingGuilds++
let guild = dbData[guildId]
for (let user of guild.users) {
res.managingUsers++
if (typeof user.wentOnline == 'number' && typeof user.wentOffline == 'number') {
// when wentOnline is bigger than wentOffline, the user is online
if (user.wentOnline > user.wentOffline) {
res.avgUptime += Date.now() - user.wentOnline
res.avgDowntime += user.wentOnline - user.wentOffline
}
else {
res.avgUptime += user.wentOffline - user.wentOnline
res.avgDowntime += Date.now() - user.wentOffline
}
}
}
}
if (res.managingUsers > 0) {
if (res.avgUptime > 0) res.avgUptime /= res.managingUsers
if (res.avgDowntime > 0) res.avgDowntime /= res.managingUsers
}
return res
}
/**
* @param {number} num
* @param {number} nearest pow of 10
*/
function roundToNearest(num, nearest) {
nearest = Math.pow(10, nearest)
return Math.round(num / nearest) * nearest
}
/**
* this asyncronously waits for the given time
* @param {number} ms
* @returns
*/
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
function objLength(obj) {
return Object.keys(obj).length
}
module.exports = {
random,
getPercentage,
isHex,
markWord,
isInRange,
removeElemByName,
hasPermissionToSendMessages,
getStatsFromDB,
roundToNearest,
sleep,
objLength
}