forked from DigitalPulseSoftware/NotaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.lua
284 lines (233 loc) · 5.3 KB
/
utils.lua
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
util = util or {}
local timeUnits = {}
do
local minuteSecond = 60
local hourSecond = 60 * minuteSecond
local daySecond = 24 * hourSecond
local weekSecond = 7 * daySecond
local monthSecond = 30 * daySecond -- Prendre 4 semaines n'aurait fait que 28 jours
local yearSecond = 365 * daySecond -- Prendre 12 mois n'aurait fait que 360 jours
local centurySecond = 100 * yearSecond
local millenniumSecond = 10 * centurySecond
table.insert(timeUnits, {
NameSingle = "mi",
NameSingular = "millennium",
NamePlural = "millennia",
Seconds = millenniumSecond
})
table.insert(timeUnits, {
NameSingle = "c",
NameSingular = "century",
NamePlural = "centuries",
Seconds = centurySecond
})
table.insert(timeUnits, {
NameSingle = "y",
NameSingular = "year",
NamePlural = "years",
Seconds = yearSecond
})
table.insert(timeUnits, {
NameSingle = "M",
NameSingular = "month",
NamePlural = "months",
Seconds = monthSecond
})
table.insert(timeUnits, {
NameSingle = "w",
NameSingular = "week",
NamePlural = "weeks",
Seconds = weekSecond
})
table.insert(timeUnits, {
NameSingle = "d",
NameSingular = "day",
NamePlural = "days",
Seconds = daySecond
})
table.insert(timeUnits, {
NameSingle = "h",
NameSingular = "hour",
NamePlural = "hours",
Seconds = hourSecond
})
table.insert(timeUnits, {
NameSingle = "m",
NameSingular = "minute",
NamePlural = "minutes",
Seconds = minuteSecond
})
table.insert(timeUnits, {
NameSingle = "s",
NameSingular = "second",
NamePlural = "seconds",
Seconds = 1
})
end
function string.ConvertToTime(str)
local seconds = tonumber(str)
if (seconds) then
return seconds
end
seconds = 0
local units = timeUnits
local valid = false
for unit, timeUnit in string.gmatch(str, "([%d-]+)%s*(%a+)") do
unit = tonumber(unit)
if (not unit) then
return -- Not valid
end
for k,v in pairs(units) do
if (timeUnit == v.NameSingle or timeUnit == v.NameSingular or timeUnit == v.NamePlural) then
valid = true
seconds = seconds + unit * v.Seconds
end
end
if (not valid) then
return
end
end
if (not valid) then
return
end
return seconds
end
function string.GetArguments(txt, limit)
local args = {}
for k,v in pairs(string.Explode('"', txt)) do
if (k % 2 == 0) then
table.insert(args, v)
else
for k,v in pairs(string.Explode(" ", v)) do
if (#v > 0) then
table.insert(args, v)
end
end
end
end
if (limit and #args > limit) then
args[limit] = table.concat(args, " ", limit)
for i = limit + 1, #args do
args[i] = nil
end
end
return args
end
function string.ToTable(str)
local tbl = {}
for i = 1, string.len( str ) do
tbl[i] = string.sub( str, i, i )
end
return tbl
end
local totable = string.ToTable
local string_sub = string.sub
local string_find = string.find
local string_len = string.len
function string.Explode(separator, str, withpattern)
if ( separator == "" ) then return totable( str ) end
if ( withpattern == nil ) then withpattern = false end
local ret = {}
local current_pos = 1
for i = 1, string_len( str ) do
local start_pos, end_pos = string_find( str, separator, current_pos, not withpattern )
if ( not start_pos ) then break end
ret[ i ] = string_sub( str, current_pos, start_pos - 1 )
current_pos = end_pos + 1
end
ret[ #ret + 1 ] = string_sub( str, current_pos )
return ret
end
function string.UpperizeFirst(str)
return string.upper(str:sub(1,1)) .. str:sub(2)
end
function table.empty(tab)
for k,v in pairs(tab) do
return false
end
return true
end
function table.NiceConcat(tab)
if (#tab > 1) then
return table.concat(tab, ", ", 1, #tab - 1) .. " and " .. tab[#tab]
elseif (#tab == 1) then
return tab[1]
else
return ""
end
end
function util.FormatTime(seconds, depth)
if (type(seconds) ~= "number") then
return seconds
end
local units = timeUnits
if (seconds < units[#units].Seconds) then
return "zero " .. units[#units].NameSingular
end
depth = depth or 0
local txt = {}
for k,v in pairs(units) do
if (seconds >= v.Seconds) then
local count = math.floor(seconds/v.Seconds)
seconds = seconds - count*v.Seconds
if (count > 1) then
table.insert(txt, count .. " " .. v.NamePlural)
else
table.insert(txt, "one " .. v.NameSingular)
end
if (depth > 0) then
depth = depth - 1
if (depth < 1) then
break
end
end
end
end
return table.NiceConcat(txt)
end
function util.MemberHasAnyRole(member, roles)
for _, roleId in pairs(roles) do
if (member:hasRole(roleId)) then
return true
end
end
return false
end
function util.MemberHasAllRoles(member, roles)
if (#roles == 0) then
return true
end
for _, roleId in pairs(roles) do
if (not member:hasRole(roleId)) then
return false
end
end
return true
end
function table.binsearch( tbl, value, comp )
local comp = comp or function (a, b)
if (a == b) then return 0 end
if (a < b) then return -1 end
if (a > b) then return 1 end
end
local iStart,iEnd,iMid = 1,#tbl,0
while iStart <= iEnd do
iMid = math.floor( (iStart+iEnd)/2 )
local r = comp( value, tbl[iMid] )
if r == 0 then
return tbl[iMid], iMid
elseif r < 0 then
iEnd = iMid - 1
else
iStart = iMid + 1
end
end
return nil, iStart, iEnd
end
function table.length( tbl )
local count = 0
for _ in pairs(tbl) do
count = count + 1
end
return count
end