FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NotaBot/bot_utility.lua at master · DigitalPulseSoftware/NotaBot · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DigitalPulseSoftware
/
NotaBot
Public
Notifications
You must be signed in to change notification settings
Fork
13
Star
25
Code
Issues
13
Pull requests
9
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
NotaBot
/
bot_utility.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
549 lines (459 loc) · 13 KB
Breadcrumbs
NotaBot
/
bot_utility.lua
Copy path
File metadata and controls
549 lines (459 loc) · 13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
--
Copyright (C) 2018 Jérôme Leclercq
--
This file is part of the "Not a Bot" application
--
For conditions of distribution and use, see copyright notice in LICENSE
local
discordia
=
require
(
'
discordia
'
)
local
enums
=
discordia
.
enums
local
fs
=
require
(
"
coro-fs
"
)
local
json
=
require
(
"
json
"
)
local
path
=
require
(
"
path
"
)
local
discordDomains
=
{
--
no subdomain
[
"
discord.com
"
]
=
true
,
[
"
discordapp.com
"
]
=
true
,
--
public test build
[
"
ptb.discord.com
"
]
=
true
,
[
"
ptb.discordapp.com
"
]
=
true
,
--
canary
[
"
canary.discord.com
"
]
=
true
,
[
"
canary.discordapp.com
"
]
=
true
,
}
function
Bot
:
DecodeChannel
(
guild
,
message
)
assert
(
guild
)
assert
(
message
)
local
channelId
=
message
:
match
(
"
<#(%d+)>
"
)
if
(
not
channelId
)
then
channelId
=
message
:
match
(
"
^(%d+)$
"
)
if
(
not
channelId
)
then
return
nil
,
"
Invalid channelId id
"
end
end
local
channel
=
guild
:
getChannel
(
channelId
)
if
(
not
channel
)
then
return
nil
,
"
This channel is not part of this guild
"
end
return
channel
end
function
Bot
:
DecodeEmoji
(
guild
,
message
)
assert
(
guild
)
assert
(
message
)
local
emojiId
=
message
:
match
(
"
<a?:[%w_]+:(%d+)>
"
)
if
(
emojiId
)
then
--
Custom emoji
local
emoji
=
self
:
GetEmojiData
(
guild
,
emojiId
)
if
(
not
emoji
)
then
return
nil
,
"
Failed to get emoji, maybe this is a global emoji?
"
end
return
emoji
else
--
Discord emoji
local
emoji
=
self
:
GetEmojiData
(
guild
,
message
)
if
(
not
emoji
)
then
return
nil
,
"
Invalid emoji
"
end
return
emoji
end
end
function
Bot
:
DecodeMember
(
guild
,
message
)
assert
(
guild
)
assert
(
message
)
local
userId
=
message
:
match
(
"
<@!?(%d+)>
"
)
if
(
not
userId
)
then
userId
=
message
:
match
(
"
^(%d+)$
"
)
if
(
not
userId
)
then
return
nil
,
"
Invalid user id
"
end
end
local
member
=
guild
:
getMember
(
userId
)
if
(
not
member
)
then
return
nil
,
"
This user is not part of this guild
"
end
return
member
end
function
Bot
:
DecodeMessage
(
messageContent
,
ignoreEscaped
,
fullContent
)
assert
(
messageContent
)
local
pattern
=
"
(<?)https?://([%w%.]+)/channels/(%d+)/(%d+)/(%d+)(>?)
"
if
(
fullContent
)
then
pattern
=
"
^
"
..
pattern
..
"
$
"
end
local
e1
,
domain
,
guildId
,
channelId
,
messageId
,
e2
=
messageContent
:
match
(
pattern
)
if
(
not
e1
or
not
discordDomains
[
domain
])
then
return
nil
,
"
Invalid link
"
end
if
(
ignoreEscaped
and
e1
==
"
<
"
and
e2
==
"
>
"
)
then
return
nil
,
"
Escaped link
"
end
local
guild
=
self
.
Client
:
getGuild
(
guildId
)
if
(
not
guild
)
then
return
nil
,
"
Unavailable guild
"
end
local
channel
=
guild
:
getChannel
(
channelId
)
if
(
not
channel
)
then
return
nil
,
"
Unavailable channel
"
end
local
message
=
channel
:
getMessage
(
messageId
)
if
(
not
message
)
then
return
nil
,
"
Message not found
"
end
return
message
end
function
Bot
:
DecodeRole
(
guild
,
message
)
assert
(
guild
)
assert
(
message
)
local
roleId
=
message
:
match
(
"
<@&(%d+)>
"
)
if
(
not
roleId
)
then
roleId
=
message
:
match
(
"
^(%d+)$
"
)
if
(
not
roleId
)
then
return
nil
,
"
Invalid role
"
end
end
local
role
=
guild
:
getRole
(
roleId
)
if
(
not
role
)
then
return
nil
,
"
This role is not part of this guild
"
end
return
role
end
function
Bot
:
DecodeUser
(
message
)
assert
(
message
)
local
userId
=
message
:
match
(
"
<@!?(%d+)>
"
)
if
(
not
userId
)
then
userId
=
message
:
match
(
"
^(%d+)$
"
)
if
(
not
userId
)
then
return
nil
,
"
Invalid user id
"
end
end
local
user
=
self
.
Client
:
getUser
(
userId
)
if
(
not
user
)
then
return
nil
,
"
Invalid user (maybe this account was deleted?)
"
end
return
user
end
function
Bot
:
GenerateMessageLink
(
message
)
local
guildId
=
message
.
guild
and
message
.
guild
.
id
or
"
@me
"
return
string.format
(
"
https://discord.com/channels/%s/%s/%s
"
,
guildId
,
message
.
channel
.
id
,
message
.
id
)
end
local
publicChannels
=
{
[
enums
.
channelType
.
text
]
=
true
,
[
enums
.
channelType
.
voice
]
=
true
,
[
enums
.
channelType
.
news
]
=
true
,
[
enums
.
channelType
.
publicThread
]
=
true
,
[
enums
.
channelType
.
newsThread
]
=
true
,
}
function
Bot
:
IsPublicChannel
(
channel
)
return
publicChannels
[
channel
.
type
]
end
local
ehandler
=
function
(
err
)
return
debug.traceback
(
tostring
(
err
))
end
function
Bot
:
ProtectedCall
(
context
,
func
, ...)
local
success
,
a
,
b
,
c
,
d
,
e
,
f
=
xpcall
(
func
,
ehandler
,
...
)
if
(
not
success
)
then
local
err
=
string.format
(
"
%s failed: %s
"
,
context
,
a
)
self
.
Client
:
warning
(
err
)
return
false
,
err
end
return
success
,
a
,
b
,
c
,
d
,
e
,
f
end
--
Serialization/unserialization
function
Bot
:
SerializeToFile
(
filepath
,
data
,
pretty
)
local
dirname
=
path
.
dirname
(
filepath
)
if
(
dirname
~=
"
.
"
)
then
local
success
,
err
=
fs
.
mkdirp
(
dirname
)
if
(
not
success
)
then
return
false
,
string.format
(
"
Failed to create directory %s: %s
"
,
filepath
,
err
)
end
end
local
outputFile
,
err
=
io.open
(
filepath
,
"
w+
"
)
if
(
not
outputFile
)
then
return
false
,
string.format
(
"
Failed to open %s: %s
"
,
filepath
,
err
)
end
local
encoderState
=
{}
if
(
pretty
)
then
encoderState
.
indent
=
true
end
local
success
,
serializedDataOrErr
=
pcall
(
json
.
encode
,
data
,
encoderState
)
if
(
not
success
)
then
return
false
,
string.format
(
"
Failed to serialize data: %s
"
,
serializedDataOrErr
)
end
local
success
,
err
=
outputFile
:
write
(
serializedDataOrErr
)
if
(
not
success
)
then
return
false
,
string.format
(
"
Failed to write data to file %s: %s
"
,
filepath
,
err
)
end
outputFile
:
close
()
return
true
end
function
Bot
:
UnserializeFromFile
(
filepath
)
local
saveFile
,
err
=
io.open
(
filepath
,
"
r
"
)
if
(
not
saveFile
)
then
return
nil
,
string.format
(
"
Failed to open %s: %s
"
,
filepath
,
err
)
end
local
content
,
err
=
saveFile
:
read
(
"
*a
"
)
if
(
not
content
)
then
return
nil
,
string.format
(
"
Failed to read %s content: %s
"
,
filepath
,
err
)
end
saveFile
:
close
()
local
success
,
contentOrErr
=
pcall
(
json
.
decode
,
content
)
if
(
not
success
)
then
return
nil
,
string.format
(
"
Failed to unserialize %s content: %s
"
,
filepath
,
contentOrErr
)
end
return
contentOrErr
end
local
fileTypes
=
{
aac
=
"
sound
"
,
avi
=
"
video
"
,
apng
=
"
image
"
,
bmp
=
"
image
"
,
flac
=
"
video
"
,
gif
=
"
image
"
,
ico
=
"
image
"
,
jpg
=
"
image
"
,
jpeg
=
"
image
"
,
ogg
=
"
sound
"
,
m4a
=
"
sound
"
,
mkv
=
"
video
"
,
mov
=
"
video
"
,
mp1
=
"
sound
"
,
mp2
=
"
sound
"
,
mp3
=
"
sound
"
,
mp4
=
"
video
"
,
png
=
"
image
"
,
tif
=
"
image
"
,
wav
=
"
sound
"
,
webm
=
"
video
"
,
webp
=
"
image
"
,
wma
=
"
sound
"
,
wmv
=
"
video
"
}
function
Bot
:
BuildQuoteEmbed
(
message
,
opt
)
local
author
=
message
.
author
local
content
=
message
.
content
local
maxContentSize
=
1800
-
(
opt
and
opt
.
initialContentSize
or
0
)
local
decorateEmbed
=
function
(
embed
)
--
Replace footer and timestamp
embed
.
author
=
{
name
=
author
.
tag
,
icon_url
=
author
.
avatarURL
}
embed
.
thumbnail
=
opt
and
opt
.
bigAvatar
and
{
url
=
author
.
avatarURL
}
or
nil
embed
.
timestamp
=
message
.
timestamp
return
embed
end
--
Quoting an embed? Copy it
if
(
#
content
==
0
and
(
not
message
.
attachments
or
#
message
.
attachments
==
0
)
and
message
.
embed
)
then
return
decorateEmbed
(
message
.
embed
)
end
local
fields
local
imageUrl
local
function
ProcessAttachments
(
attachments
)
local
images
=
{}
local
files
=
{}
local
sounds
=
{}
local
videos
=
{}
for
_
,
attachment
in
pairs
(
attachments
)
do
local
matchedExt
=
attachment
.
url
:
match
(
"
//.-/.+%.(.-)[?].*$
"
)
or
attachment
.
url
:
match
(
"
//.-/.+%.(.*)$
"
)
or
"
"
--
Edge case for embed images with no extensions
local
ext
=
(
#
matchedExt
==
0
and
attachment
.
thumbnail
and
"
png
"
or
matchedExt
):
lower
()
--
handle urls ending in .jpg:large
ext
=
ext
:
match
(
"
^(.+):.-$
"
)
or
ext
local
fileType
=
fileTypes
[
ext
]
local
t
=
files
if
(
fileType
)
then
if
(
fileType
==
"
image
"
)
then
t
=
images
elseif
(
fileType
==
"
sound
"
)
then
t
=
sounds
elseif
(
fileType
==
"
video
"
)
then
t
=
videos
end
end
table.insert
(
t
,
image
)
end
--
Special shortcut for one image attachment
if
(
#
attachments
==
1
and
#
images
==
1
)
then
imageUrl
=
images
[
1
].
url
else
--
Should only happens for attachments, not embeds
fields
=
{}
local
function
LinkList
(
title
,
attachments
)
if
(
#
attachments
==
0
)
then
return
end
local
desc
=
{}
for
_
,
attachment
in
pairs
(
attachments
)
do
if
attachment
.
filename
then
table.insert
(
desc
,
"
[
"
..
(
attachment
.
filename
)
..
"
](
"
..
attachment
.
url
..
"
)
"
)
else
table.insert
(
desc
,
attachment
.
url
)
end
end
table.insert
(
fields
, {
name
=
title
,
value
=
table.concat
(
desc
,
"
\n
"
),
inline
=
true
})
end
LinkList
(
"
Images 🖼️
"
,
images
)
LinkList
(
"
Sounds 🎵
"
,
sounds
)
LinkList
(
"
Videos 🎥
"
,
videos
)
LinkList
(
"
Files 🖥️
"
,
files
)
if
(
#
images
>
0
)
then
imageUrl
=
images
[
1
].
url
end
end
end
if
(
message
.
attachments
)
then
ProcessAttachments
(
message
.
attachments
)
end
if
(
message
.
embeds
and
not
message
.
attachments
)
then
ProcessAttachments
(
message
.
embeds
)
end
if
(
fields
)
then
maxContentSize
=
maxContentSize
-
#
json
.
encode
(
fields
)
end
--
Fix emojis
content
=
content
:
gsub
(
"
(<a?:([%w_]+):(%d+)>)
"
,
function
(
mention
,
emojiName
,
emojiId
)
--
Bot are allowed to use emojis from every servers they are on
local
emojiData
=
Bot
:
GetEmojiData
(
nil
,
emojiId
)
local
canUse
=
false
if
(
emojiData
)
then
if
(
emojiData
.
Custom
)
then
local
emoji
=
emojiData
.
Emoji
local
guild
=
emojiData
.
FromGuild
--
Check if bot has permissions to use this emoji (on the guild it comes from)
local
botMember
=
guild
:
getMember
(
Client
.
user
)
--
Should never make a HTTP request
local
found
=
true
for
_
,
role
in
pairs
(
emoji
.
roles
)
do
found
=
false
--
Set false if we enter the loop
if
(
botMember
:
hasRole
(
role
))
then
found
=
true
break
end
end
canUse
=
found
else
canUse
=
true
end
else
canUse
=
false
end
if
(
canUse
)
then
return
mention
else
return
"
:
"
..
emojiName
..
"
:
"
end
end
)
if
(
#
content
>
maxContentSize
)
then
content
=
content
:
sub
(
1
,
maxContentSize
)
..
"
... <truncated>
"
end
--
TODO: support multiple stickers (up to 3 per message), even if there's already an attached image?
--
A sticker can be a (1) PNG, (2) APNG or (3) LOTTIE (JSON), if it's a LOTTIE, don't attach it
--
https://discord.com/developers/docs/resources/sticker#sticker-object-sticker-format-types
if
(
not
imageUrl
and
message
.
_stickers
and
message
.
_stickers
[
1
].
format_type
~=
3
)
then
imageUrl
=
"
https://media.discordapp.net/stickers/
"
..
message
.
_stickers
[
1
].
id
..
"
.png?size=128
"
end
return
decorateEmbed
({
image
=
imageUrl
and
{
url
=
imageUrl
}
or
nil
,
description
=
content
,
fields
=
fields
})
end
function
Bot
:
FetchChannelMessages
(
channel
,
nextId
,
limit
,
fromEnd
)
limit
=
limit
or
1000
fromEnd
=
fromEnd
or
false
local
seenMessages
=
{}
local
channelMessages
=
{}
while
limit
>
0
do
local
requestLimit
=
math.min
(
limit
,
100
)
local
messages
,
err
if
fromEnd
then
messages
,
err
=
channel
:
getMessagesBefore
(
nextId
,
requestLimit
)
else
messages
,
err
=
channel
:
getMessagesAfter
(
nextId
or
channel
.
id
,
requestLimit
)
end
if
not
messages
then
return
nil
,
err
end
if
fromEnd
then
for
message
in
messages
:
iter
()
do
local
messageId
=
message
.
id
if
not
seenMessages
[
messageId
]
then
seenMessages
[
messageId
]
=
true
table.insert
(
channelMessages
,
message
)
if
not
nextId
or
messageId
<
nextId
then
nextId
=
messageId
end
end
end
else
for
message
in
messages
:
iter
()
do
local
messageId
=
message
.
id
if
not
seenMessages
[
messageId
]
then
seenMessages
[
messageId
]
=
true
table.insert
(
channelMessages
,
message
)
if
not
nextId
or
messageId
>
nextId
then
nextId
=
messageId
end
end
end
end
if
#
messages
<
requestLimit
then
break
end
limit
=
limit
-
#
messages
end
table.sort
(
channelMessages
,
function
(
a
,
b
)
return
a
.
id
<
b
.
id
end
)
return
channelMessages
end
function
Bot
:
MessagesToTable
(
messages
)
local
authors
=
{}
local
messageData
=
{}
for
_
,
message
in
ipairs
(
messages
)
do
local
author
=
message
.
member
or
message
.
author
if
not
authors
[
author
.
id
]
then
authors
[
author
.
id
]
=
{
--
User fields
accentColor
=
author
.
accentColor
and
discordia
.
Color
(
author
.
accentColor
):
toHex
()
or
nil
,
avatar
=
author
.
avatar
,
avatarURL
=
author
.
avatarURL
,
banner
=
author
.
banner
,
bot
=
author
.
bot
,
createdAt
=
author
.
timestamp
,
discriminator
=
author
.
discriminator
,
flags
=
author
.
publicFlags
,
premiumType
=
author
.
premiumType
,
system
=
author
.
system
,
username
=
author
.
username
,
--
Member fields
color
=
(
author
.
getColor
and
author
:
getColor
()
or
discordia
.
Color
()):
toHex
(),
joinedAt
=
author
.
joinedAt
,
nickname
=
author
.
nickname
,
premiumSince
=
author
.
premiumSince
}
end
local
fields
=
{
attachments
=
message
.
attachments
,
author
=
message
.
author
and
message
.
author
.
id
or
nil
,
content
=
#
message
.
content
>
0
and
message
.
content
or
nil
,
createdAt
=
message
.
timestamp
,
embed
=
message
.
embed
,
components
=
message
.
components
,
interaction
=
message
.
interaction
,
components
=
message
.
components
,
tts
=
message
.
tts
or
nil
}
local
embed
=
message
.
embed
if
(
embed
)
then
embed
.
type
=
nil
local
author
=
embed
.
author
if
(
author
)
then
author
.
proxy_icon_url
=
nil
end
end
table.insert
(
messageData
,
fields
)
end
return
{
authors
=
authors
,
messages
=
messageData
}
end
Back
|
FazBrowse Home
|
New Git URL