From c593260d4f522f8ee0e51bd70db021e85a7e4d25 Mon Sep 17 00:00:00 2001 From: olive Date: Sat, 12 Sep 2026 21:03:00 +0100 Subject: discord channel webhooking --- discord/pylon.lua | 66 ++++++++++++++++++++++++++++++++++++++++++++---------- main.lua | 2 ++ nanochat/pylon.lua | 4 ---- pylon.lua | 1 - 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/discord/pylon.lua b/discord/pylon.lua index 4a3e832..83f1758 100644 --- a/discord/pylon.lua +++ b/discord/pylon.lua @@ -16,6 +16,7 @@ local API_BASE = "https://discord.com/api/v10/" local Discord = class.extend(BasePylon) function Discord.init(self) self:_check_fields "token" + self.channel_to_webhook = {} end local function identify_payload(token) @@ -27,14 +28,31 @@ local function identify_payload(token) function Discord._req_get(self, url) local req = request.new_from_uri(API_BASE..url) - req.headers:upsert('content-type','application/json') + req.headers:upsert('accept','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 assert(json.decode(body:get_body_as_string())) + 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)) end function Discord._connect(self) @@ -44,7 +62,32 @@ function Discord._connect(self) self.ws:send(identify_payload(self.token), 'text') local me = self:_req_get"users/@me" + self.bot_id = me.id pprint('I AM ',me) + + for busname, bus in pairs(self.wilson.busses) do + for _, channel in ipairs(bus) do + if channel.pylon == self then + self:add_channel(channel) + end + end + end +end + +function Discord.add_channel(self, channel) + local webhooks = self:_req_get("channels/"..channel.descriptor.."/webhooks") + pprint("webhooks",webhooks) + for _,wh in pairs(webhooks) do + if wh.application_id == self.bot_id then + print("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" }) + pprint(wh) + self.channel_to_webhook[channel.descriptor] = wh end function Discord._heartbeating(self, interval_ms) @@ -80,19 +123,19 @@ 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 + if d.author.id == self.channel_to_webhook[d.channel_id].id then return end + if d.author.id == self.bot_id then return end + self.wilson:deliver(Channel(self, d.channel_id), { + body = d.content, + sender = '[d]' .. d.author.username, + }) end end function Discord.sending(self) for dest_channel, message in self.inbox:iter() do - print('DDD',message.sender,message.body) - exec_webhook(self.temp_wh, { + print('DDD',dest_channel,message.sender,message.body) + exec_webhook(self.channel_to_webhook[dest_channel.descriptor].url, { username = message.sender, content = message.body, }) @@ -100,4 +143,3 @@ function Discord.sending(self) end return Discord - diff --git a/main.lua b/main.lua index cd81b0e..b1cb708 100644 --- a/main.lua +++ b/main.lua @@ -3,6 +3,7 @@ local class = require'r.class' local config = require'config' local Channel = require'channel' +local pprint = require'pprint' local pylon_classes = { irc = require'irc.pylon', @@ -50,6 +51,7 @@ function Wilson.deliver(self, source_channel, message) if bus then for _, dest_channel in ipairs(bus) do if source_channel ~= dest_channel then + pprint(source_channel, "-->", dest_channel, message) dest_channel.pylon:post(dest_channel, message) end end diff --git a/nanochat/pylon.lua b/nanochat/pylon.lua index 7dae58b..b295745 100644 --- a/nanochat/pylon.lua +++ b/nanochat/pylon.lua @@ -20,10 +20,6 @@ function Nanochat:_connect() self.usids = {} end -function Nanochat:add_channel(channel) - -- don't need to do anything -end - function Nanochat:recving() while true do self.sock:write("SKIP "..self.lastid.."\n") diff --git a/pylon.lua b/pylon.lua index f00faa6..cd0ae9e 100644 --- a/pylon.lua +++ b/pylon.lua @@ -28,4 +28,3 @@ function BasePylon.run(self) print(self.name, self.cq:loop()) end return BasePylon - -- cgit v1.2.3