diff options
Diffstat (limited to 'discord')
| -rw-r--r-- | discord/pylon.lua | 43 |
1 files changed, 16 insertions, 27 deletions
diff --git a/discord/pylon.lua b/discord/pylon.lua index 83f1758..180ec27 100644 --- a/discord/pylon.lua +++ b/discord/pylon.lua @@ -26,33 +26,20 @@ local function identify_payload(token) intents = I.guilds + I.guild_messages + I.message_content, token = token } } end -function Discord._req_get(self, url) +function Discord._req(self, url, payload) local req = request.new_from_uri(API_BASE..url) - req.headers:upsert('accept','application/json') - req.headers:upsert(':method','GET') + req.headers:upsert(':method',payload and 'POST' or 'GET') req.headers:upsert('authorization','Bot '..self.token) req.headers:upsert('user-agent','wilson (https://g.gh0.pw/wilson/, v0.0)') + if payload then + req.headers:upsert('content-type','application/json') + req:set_body(assert(json.encode(payload))) end local head,body = assert(req:go()) local status = head:get':status' local bod = body:get_body_as_string() - assert(status:sub(1,1)=='2','status was '..status.." body: "..bod) - print(bod) - return assert(json.decode(bod)) -end - -function Discord._req_post(self, url, payload) - local req = request.new_from_uri(API_BASE..url) - req:set_body(assert(json.encode(payload))) - req.headers:upsert(':method','POST') - req.headers:upsert('content-type','application/json') - req.headers:upsert('authorization','Bot '..self.token) - req.headers:upsert('user-agent','wilson (https://g.gh0.pw/wilson/, v0.0)') - local head,body = assert(req:go()) - local status = head:get':status' - local bod = body:get_body_as_string() - assert(status:sub(1,1)=='2','status was '..status.." body: "..bod) - print(bod) - return assert(json.decode(bod)) + assert(status:match"^2", 'status was '..status..' body '..bod) + local val, _, err = json.decode(bod) + if err then error(err) else return val end end function Discord._connect(self) @@ -61,9 +48,9 @@ function Discord._connect(self) assert(self.ws:connect()) self.ws:send(identify_payload(self.token), 'text') - local me = self:_req_get"users/@me" + local me = self:_req"users/@me" self.bot_id = me.id - pprint('I AM ',me) + self.log('logged in as ',me) for busname, bus in pairs(self.wilson.busses) do for _, channel in ipairs(bus) do @@ -75,17 +62,17 @@ function Discord._connect(self) end function Discord.add_channel(self, channel) - local webhooks = self:_req_get("channels/"..channel.descriptor.."/webhooks") - pprint("webhooks",webhooks) + local webhooks = self:_req("channels/"..channel.descriptor.."/webhooks") for _,wh in pairs(webhooks) do if wh.application_id == self.bot_id then - print("Found existing webhook for channel "..channel.descriptor) + -- self.log.debug("Found existing webhook for channel",channel.descriptor) self.channel_to_webhook[channel.descriptor] = wh return end end print("Making webhook for channel "..channel.descriptor) - local wh = self:_req_post("channels/"..channel.descriptor.."/webhooks", { name=self.name:gsub("discord",""):gsub("clyde","").." wilson hook" }) + local wh = self:_req_post("channels/"..channel.descriptor.."/webhooks", { + name=self.name:gsub("discord",""):gsub("clyde","").." wilson hook" }) pprint(wh) self.channel_to_webhook[channel.descriptor] = wh end @@ -129,6 +116,8 @@ function Discord.handle_dispatch(self, event) body = d.content, sender = '[d]' .. d.author.username, }) + elseif event.t == 'READY' then + pprint(event.d) end end |
