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
|
local consts = require'discord.consts'
local websocket = require'http.websocket'
local json = require 'dkjson'
local Queue = require 'util.queue'
local exec_webhook = require'discord.the'
local cqueues = require 'cqueues'
local Discord = {}
function Discord.make(wilson, conf)
local self = setmetatable(conf, {__index=Discord})
for k,v in pairs {
wilson = wilson,
inbox = Queue.make(),
} do self[k] = v end
assert(self.token)
return self
end
local function identify_payload(token)
local I = consts.intents
return json.encode{
op = consts.opcodes.identify,
d = {
properties = {
os = "wilson", browser = "wilson", device = "wilson",
},
intents = I.guilds + I.guild_messages + I.message_content,
token = token,
}
}
end
function Discord._connect(self)
local uri = "wss://gateway.discord.gg/?v=10&encoding=json"
self.ws = websocket.new_from_uri(uri)
assert(self.ws:connect())
self.ws:send(identify_payload(self.token), 'text')
-- for a,b in self.ws:each() do
-- print('its',a,b)
-- end
end
function Discord.run(self)
local cq = cqueues.new()
self:_connect()
cq:wrap(self.recving, self)
cq:wrap(self.sending, self)
print('discord', cq:loop())
end
function Discord.recving(self)
for event in self.ws:each() do
print(event)
end
end
function Discord.sending(self)
local i = 1
while true do
exec_webhook(self.temp_wh, { username = "god", content = "h"..("i"):rep(i) })
i = i + 1
cqueues.sleep(1)
end
end
return Discord
|