-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
267 lines (217 loc) · 5.16 KB
/
Copy pathinit.lua
File metadata and controls
267 lines (217 loc) · 5.16 KB
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
-- from incredible-gmod.ru with <3
-- https://github.com/Be1zebub/Discordia-Commands
local ratelimit = require("ratelimit")
local Commands, CommandsMap, Aliases = {}, {}, {}
local Command, get = require("discordia").class("Command")
local meta = getmetatable(Command)
function Command:__call(name, ...)
if CommandsMap[name] then
self:__init(name, ...)
else
meta.__call(self, name, ...)
self._id = #Commands + 1
Commands[self._id] = self
CommandsMap[name] = self
end
end
function Command:__init(name, ...)
self._name = name
self._aliases = {}
self:SetAlias(...)
end
-- setters
function Command:SetName(name)
self._name = name
return self
end
function Command:SetDescription(description)
self._description = description
return self
end
function Command:SetCallback(callback)
self._callback = callback
return self
end
function Command:SetCustomcheck(customcheck)
self._customcheck = customcheck
return self
end
function Command:SetGuildOnly(bool)
self._guildonly = bool ~= false
return self
end
function Command:SetPermissions(...)
self._permissions = {...}
return self
end
function Command:SetCooldown(length, count, weak)
self._cooldown = ratelimit(length, count, weak)
return self
end
function Command:SetAlias(...)
if ... then
for _, alias in ipairs({...}) do
Aliases[alias] = self
self._aliases[#self._aliases + 1] = alias
end
end
return self
end
-- getters
function get.name(self)
return self._name
end
function get.description(self)
return self._description
end
function get.callback(self)
return self._callback
end
function get.customcheck(self)
return self._customcheck
end
function get.guildonly(self)
return self._guildonly
end
function get.permissions(self)
return self._permissions
end
function get.cooldown(self)
return self._cooldown
end
function get.aliases(self)
return self._aliases
end
-- helpers
local function stringSplit(pString, pPattern)
local tbl = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(tbl, cap)
end
last_end = e + 1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(tbl, cap)
end
return tbl
end
local numberReacts = setmetatable({
["0"] = "0️⃣",
["1"] = "1️⃣",
["2"] = "2️⃣",
["3"] = "3️⃣",
["4"] = "4️⃣",
["5"] = "5️⃣",
["6"] = "6️⃣",
["7"] = "7️⃣",
["8"] = "8️⃣",
["9"] = "9️⃣",
}, {
__call = function(self, msg, num)
for char in tostring(math.floor(num)):gmatch(".") do
if self[char] then
msg:addReaction(self[char])
end
end
end
})
--
return setmetatable({
list = Commands,
map = CommandsMap,
aliases = Aliases,
New = function(_, name, desc)
return Command(name, desc)
end,
Example = function()
Command("/help", "/commands")
:SetDescription("Shows a commands list")
:SetCooldown(30, 1)
:SetCallback(function(msg, args)
local cmdName = args[1]
if cmdName then
local cmd = CommandsMap[cmdName] or Aliases[cmdName]
if cmd == nil then
msg:reply("Unknown command!")
return
end
local desc = cmd.description
if desc == nil then
msg:reply("This command has no description!")
return
end
if cmd.aliases then
desc = desc .."\nAliases: **".. table.concat(cmd.aliases, ", ") .."**"
end
msg:reply({
embed = {
title = cmdName,
description = desc,
color = 0x2895C8
}
})
else
local commands = {}
for _, cmd in ipairs(Commands) do
if cmd.description then
commands[#commands + 1] = "**" .. cmd.name .. ":** " .. cmd.description
end
end
msg:reply({
embed = {
title = "Commands list:",
description = table.concat(commands, "\n"),
color = 0x2895C8
}
})
end
end)
Command("/ping")
:SetDescription("Replies pong!")
:SetCallback(function(msg)
local new = msg:reply("Pong!")
new:setContent("Pong! `".. math.abs(math.Round((new.createdAt - msg.createdAt) * 1000)) .." ms`")
end)
end
}, {
__call = function(_, msg)
if msg.author.bot then return end
local args = stringSplit(msg.content, " ")
local command = table.remove(args, 1)
local cmd = CommandsMap[command] or Aliases[command]
if (cmd and cmd.callback) == nil then return end
if cmd.customcheck and cmd.customcheck(msg) == false then
msg:reply("You dont have permissions to run this command!")
return false
end
if cmd.guildonly and msg.guild == nil then
msg:reply("You cant use this command in DM!")
return false
end
if cmd.permissions then
if msg.member == nil then return false end -- permissions in dm? bruh, its guild only feature
for _, permission in ipairs(cmd.permissions) do
if msg.member:hasPermission(msg.channel, permission) == false then
msg:reply("You dont have permissions to run this command!")
return false
end
end
end
if cmd.cooldown then
local cooldown, left = cmd.cooldown(msg.author.id)
if cooldown then
msg:addReaction("🕖")
numberReacts(msg, left)
return false
end
end
cmd.callback(msg, args)
return true
end
})