-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscordutil.lua
198 lines (155 loc) Β· 3.46 KB
/
discordutil.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
local function Log(b)
CLIENT._logger._level = b and 3 or 0
end
local function Mention(user)
return "<@!" .. user.id .. ">"
end
local function Tag(user)
return user.tag or user.user.tag
end
local function MessageLink(msg)
return msg.link
end
local function ChannelName(obj)
return obj.mentionString or obj.channel.mentionString
end
local ResolveID
do
local stringfind, stringmatch = string.find, string.match
function ResolveID(str)
if stringfind(str, "<@!", 1, true) then
return stringmatch(str, "%d+"), 1
elseif stringfind(str, "<#", 1, true) then
return stringmatch(str, "%d+"), 2
elseif stringfind(str, "<@&", 1, true) then
return stringmatch(str, "%d+"), 3
elseif stringfind(str, "<@", 1, true) then
return stringmatch(str, "%d+"), 4
end
return str, 0
end
end
local GetChannel
do
local lookup = {}
function GetChannel(name)
if lookup[name] then return lookup[name] end
for c in SERVER.textChannels:iter() do
if c.name == name then
lookup[name] = c
return c
end
end
return nil
end
end
local AddEmote, AddEmoteCallback
do
local emoji = {
alert = ":error:758776477754851399",
clock = ":late:589133896952709131",
stop = "π",
zero = "0οΈβ£",
erase = "ποΈ",
done = ":agree:589133885506322448",
no = ":disagree:589133888769490954",
date = "π
",
new = "π",
ok = "π",
star = "β",
restricted = "π·",
lock = "π",
gmoderror = ":error:758776477754851399"
}
function AddEmote(msg, emote)
msg:addReaction(emoji[emote])
end
function AddEmoteToList(lookupname, emote)
emoji[lookupname] = emote
end
function AddEmoteCallback(msg, emote, callback, legacy)
if not msg._EmoteCallbacks then
msg._EmoteCallbacks = {}
end
msg._EmoteCallbacks[legacy and emote or emoji[emote]] = callback
end
end
local function CallEmoteCallback(emote, msg, user)
if not msg._EmoteCallbacks or not user then return end
local ename = emote.emojiId and ":" .. emote.emojiHash or emote.emojiHash
local c = msg._EmoteCallbacks[ename]
if c then
c(msg, user, ename)
end
end
local FindMember, FindMemberByName
do
function FindMember(str)
for u in SERVER.members:iter() do
if u.tag == str then return u end
end
return nil
end
function FindMemberByName(str)
for u in SERVER.members:iter() do
if u.name == str then return u end
end
return nil
end
end
local everyone
do
local cached_everyone
function everyone()
if cached_everyone then return cached_everyone end
for r in SERVER.roles:iter() do
if r.name == "@everyone" then
cached_everyone = r
return r
end
end
return nil
end
end
local GetRole
do
local cache = {}
function GetRole(role)
if cache[role] then return cache[role] end
for r in SERVER.roles:iter() do
if r.name == role then
cache[role] = r
return r
end
end
return nil
end
end
local function GetAllRoles()
return SERVER.roles:toArray()
end
local function HasRole(user, role)
for r in user.roles:iter() do
if r.name == role then return true end
end
return false
end
return {
Log = Log,
Mention = Mention,
Tag = Tag,
MessageLink = MessageLink,
ChannelName = ChannelName,
GetChannel = GetChannel,
AddEmote = AddEmote,
AddEmoteToList = AddEmoteToList,
AddEmoteCallback = AddEmoteCallback,
CallEmoteCallback = CallEmoteCallback,
FindMember = FindMember,
FindMemberByName = FindMemberByName,
everyone = everyone,
GetRole = GetRole,
GetAllRoles = GetAllRoles,
HasRole = HasRole,
ResolveID = ResolveID,
}