diff options
Diffstat (limited to 'discord')
-rw-r--r-- | discord/pylon.lua | 48 |
1 files changed, 42 insertions, 6 deletions
diff --git a/discord/pylon.lua b/discord/pylon.lua index f84db64..7f7549b 100644 --- a/discord/pylon.lua +++ b/discord/pylon.lua @@ -1,10 +1,13 @@ local consts = require'discord.consts' +local opcodes = consts.opcodes local websocket = require'http.websocket' local json = require 'dkjson' local Queue = require 'queue' local exec_webhook = require'discord.the' local cqueues = require 'cqueues' local pylon = require 'pylon' +local pprint = require 'pprint' +local Channel = require 'channel' local Discord = pylon.subclass "discord" @@ -15,7 +18,7 @@ end local function identify_payload(token) local I = consts.intents return json.encode{ - op = consts.opcodes.identify, + op = opcodes.identify, d = { properties = { os = "wilson", browser = "wilson", device = "wilson", @@ -31,14 +34,47 @@ function Discord._connect(self) 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._heartbeat(self, interval_ms) + local interval = interval_ms / 1000 + cqueues.sleep(interval * math.random()) + while true do + self.ws:send(json.encode{ + op = opcodes.heartbeat, + d = self.sequence_number, + }) + cqueues.sleep(interval) + end end function Discord.recving(self) - for event in self.ws:each() do - print(event) + for packet in self.ws:each() do + local event = json.decode(packet) + print(event.s, event.op, event.t) + if event.op ~= opcodes.dispatch then + pprint(event.d) + end + + if event.s then self.sequence_number = event.s end + + if event.op == opcodes.hello then + self.cq:wrap(self._heartbeat, self, event.d.heartbeat_interval) + elseif event.op == opcodes.dispatch then + self:handle_dispatch(event) + end + end +end + +function Discord.handle_dispatch(self, event) + local d = event.d + if event.t == 'MESSAGE_CREATE' then + if d.author.id == '293066066605768714' then + self.wilson:deliver(Channel(self, d.channel_id), { + body = d.content, + sender = '[d]' .. d.author.username + }) + end end end |