From 9127e598e0a2dbec72eea6185ad67d739d3f1e83 Mon Sep 17 00:00:00 2001 From: rebecca Date: Sat, 12 Sep 2026 14:29:25 +0100 Subject: discord: implement rest get requests --- discord/pylon.lua | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'discord/pylon.lua') diff --git a/discord/pylon.lua b/discord/pylon.lua index 7f7549b..d38b8a7 100644 --- a/discord/pylon.lua +++ b/discord/pylon.lua @@ -4,12 +4,14 @@ local websocket = require'http.websocket' local json = require 'dkjson' local Queue = require 'queue' local exec_webhook = require'discord.the' +local http_request = require'http.request' local cqueues = require 'cqueues' local pylon = require 'pylon' local pprint = require 'pprint' local Channel = require 'channel' local Discord = pylon.subclass "discord" +local API_BASE = "https://discord.com/api/v10/" function Discord.init(self) self:check_config "token" @@ -29,11 +31,26 @@ local function identify_payload(token) } end +function Discord._req_get(self, url) + local req = http_request.new_from_uri(API_BASE..url) + req.headers:upsert('content-type','application/json') + req.headers:upsert(':method','GET') + 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' + assert(status:sub(1,1)=='2','status was '..status) + return body:get_body_as_string() +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') + + local me = self:_req_get"users/@me" + pprint('I AM ',me) end function Discord._heartbeat(self, interval_ms) @@ -52,7 +69,7 @@ function Discord.recving(self) 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 + if true or event.op ~= opcodes.dispatch then pprint(event.d) end -- cgit v1.2.3