summaryrefslogtreecommitdiff
path: root/discord/pylon.lua
diff options
context:
space:
mode:
authorolive <olive@olive.ooo>2026-09-12 21:03:00 +0100
committerolive <olive@olive.ooo>2026-09-12 21:03:00 +0100
commitc593260d4f522f8ee0e51bd70db021e85a7e4d25 (patch)
tree77070e67c74bb65ed7eaca953194294a85cffa64 /discord/pylon.lua
parent1f9192f9d6639dedd243532616fb034f0e5bb14a (diff)
discord channel webhooking
Diffstat (limited to 'discord/pylon.lua')
-rw-r--r--discord/pylon.lua66
1 files changed, 54 insertions, 12 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
-