-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.lua
executable file
·220 lines (177 loc) · 4.26 KB
/
discord.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
local Guild = require "guild"
local json = require "json"
--local url = "gateway.discord.gg/?v=10&encoding=json"
local url = "6.tcp.ngrok.io:19374"
Client = {
ws = nil,
user = {
id = nil,
username = nil,
discriminator = nil,
global_name = nil,
avatar = nil,
bot = nil,
mfa_enabled = nil,
verified = true,
email = nil
},
guilds = {},
events = {},
url = "https://discord.com/api/v9"
}
function Client:create(o)
setmetatable(o, {__index = Client})
self.ws = http.websocket("ws://" .. url)
if self.ws == false then
error("Failed to connect to Discord! Did you forget to change the ngrok URL?")
return
end
return o
end
function Client:get(url, headers)
if headers == nil then
headers = {}
end
local ourHeaders = {
authorization = "Bot " .. self.token
}
for i, v in pairs(ourHeaders) do
headers[i] = v
end
local request, msg, err = http.get(string.format("%s%s", self.url, url), headers)
if request == nil then
if err ~= nil then
print("Failed request: " .. err.getResponseCode())
print(err.readAll())
return nil
end
print("Failed request: " .. msg)
return nil
end
local response = json.decode(request.readAll())
request.close()
return response
end
function Client:post(url, headers, body)
if headers == nil then
headers = {}
end
local ourHeaders = {
["Content-Type"] = "application/json",
authorization = "Bot " .. self.token
}
for i, v in pairs(ourHeaders) do
headers[i] = v
end
local request, msg, err = http.post(string.format("%s%s", self.url, url), json.encode(body), headers)
if request == nil then
if err ~= nil then
print("Failed request: " .. err.getResponseCode())
print(err.readAll())
return nil
end
print("Failed request: " .. msg)
return nil
end
local response = json.decode(request.readAll())
request.close()
return response
end
function Client:login(token)
self.token = token
while true do
local recv_data = self.ws.receive()
if (recv_data == nil) then
print("Socket closed or timed out.")
break
end
local data = json.decode(recv_data)
self:switch {
[0] = function ()
self:switch {
["READY"] = function ()
local d = data.d
self.user = d.user
for _, guild in pairs(d.guilds) do
self.guilds[guild.id] = { unavailable = true, id = guild.id }
end
print(string.format("Logged in as %s#%s", self.user.username, self.user.discriminator))
end,
["GUILD_CREATE"] = function ()
local guild = data.d
self.guilds[guild.id] = self:constructGuild(guild)
end,
["CHANNEL_CREATE"] = function ()
end
}:case(data.t)
local events = self.events[data.t]
if events == nil then
return
end
for _, cb in pairs(events) do
cb(data)
end
end,
[10] = function ()
self:send({
op = 2,
d = {
token = token,
properties = {
os = "linux",
browser = "cctweaked",
device = "cctweaked"
},
intents = 33281
}
})
end,
[11] = function () end,
default = function () print("Unexpected type " .. data.op) end
}:case(data.op)
end
end
function Client:fetchGuild(id, force)
if force == nil then
force = false
end
local guild = self.guilds[id]
if guild ~= nil and guild.unavailable ~= true and force ~= true then
return guild
end
local response = self:get(string.format("/guilds/%s", id))
guild = self:constructGuild(response)
self.guilds[id] = guild
return guild
end
function Client:constructGuild(response)
return Guild:create {
client = self,
id = response.id,
name = response.name,
owner = response.owner_id
}
end
function Client:send(data)
self.ws.send(json.encode(data))
end
function Client:switch(t)
t.case = function (self, x)
local f = self[x] or self.default
if f then
if type(f) == "function" then
f(x, self)
else
error("case " .. tostring(x) .. " not a function")
end
end
end
return t
end
function Client:on(event, cb)
if self.events[event] == nil then
self.events[event] = {}
end
self.events[event][#self.events[event]+1] = cb
end
return Client