summaryrefslogtreecommitdiff
path: root/discord/pylon.lua
blob: f84db64285c04cc1347dbf21fdade7b796e0e4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local consts = require'discord.consts'
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 Discord = pylon.subclass "discord"

function Discord.init(self)
	self:check_config "token"
end

local function identify_payload(token)
	local I = consts.intents
	return json.encode{
		op = consts.opcodes.identify,
		d = {
			properties = {
				os = "wilson", browser = "wilson", device = "wilson",
			},
			intents = I.guilds + I.guild_messages + I.message_content,
			token = token,
		}
	}
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')
	-- for a,b in self.ws:each() do
	-- 	print('its',a,b)
	-- end
end

function Discord.recving(self)
	for event in self.ws:each() do
		print(event)
	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, {
			username = message.sender,
			content = message.body,
		})
	end
end

return Discord