-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.lua
More file actions
183 lines (149 loc) · 4.32 KB
/
Copy pathdriver.lua
File metadata and controls
183 lines (149 loc) · 4.32 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
local Lunalink = require('lunalink')
local json = require('json')
local http = require('coro-http')
local class = Lunalink.class
local abstract = Lunalink.AbstractDriver
local websocket = Lunalink.WebSocket
local LunaStreamDriver, get = class('LunaStreamDriver', abstract)
local sf = string.format
function LunaStreamDriver:__init(lunalink, node)
abstract.__init(self)
self._lunalink = lunalink
self._node = node
self._id = 'starie'
self._base_url_form = '%s://%s:%s/v1/%s'
self._proto_list = node._options.secure
and { http = 'https', ws = 'wss' }
or { http = 'http', ws = 'ws' }
self._wsUrl = sf(
self._base_url_form,
self._proto_list.ws,
node._options.host,
node._options.port,
'websocket'
)
self._httpUrl = sf(
self._base_url_form,
self._proto_list.http,
node._options.host,
node._options.port,
''
):sub(1, -2)
self._sessionId = ''
self._playerFunctions = Lunalink.Functions()
self._functions = Lunalink.Functions()
self._wsClient = nil
end
function get:id()
return 'starie'
end
function get:wsUrl()
return self._wsUrl
end
function get:httpUrl()
return self._httpUrl
end
function get:sessionId()
return self._sessionId
end
function get:playerFunctions()
return self._playerFunctions
end
function get:functions()
return self._functions
end
function get:lunalink()
return self._lunalink
end
function get:node()
return self._node
end
function LunaStreamDriver:connect()
local is_resume = self._lunalink._options.config.resume
local client_name = sf(
'%s/%s (%s)',
self._lunalink._manifest.name,
self._lunalink._manifest.version,
self._lunalink._manifest.github
)
local headers = {
{ 'authorization', self._node._options.auth },
{ 'user-id', self._lunalink._id },
{ 'client-name', client_name },
{ 'user-agent', self._lunalink._options.config.userAgent },
{ 'num-shards', self._lunalink._shardCount },
}
if self._sessionId ~= nil and is_resume then
table.insert(headers, { 'session-id', self._sessionId })
end
self._wsClient = websocket({
url = self._wsUrl,
headers = headers
})
self._wsClient:on('open', function ()
self._node:wsOpenEvent()
end)
self._wsClient:on('message', function (data)
self._node:wsMessageEvent(data.json_payload)
end)
self._wsClient:on('close', function (code, reason)
self._node:wsCloseEvent(code, reason)
end)
self._wsClient:on('error', function (err)
self._node:wsErrorEvent(err)
self._wsClient = nil
end)
self._wsClient:connect()
end
function LunaStreamDriver:requester(options)
options = options or {}
local req_body = ''
if string.match(options.path ,'/sessions') and self._sessionId == nil then
error('sessionId not initalized! Please wait for lavalink get connected!')
end
local url = self._httpUrl .. options.path
if options.params then url = url .. self:_urlparams(options.params) end
if options.data then req_body = json.encode(options.data) end
local lavalink_headers = {
{ 'authorization', self._node._options.auth },
{ 'user-agent', self._lunalink._options.config.userAgent },
table.unpack(options.headers and options.headers or {})
}
local req_method = options.method and options.method or 'GET'
local res, res_body_string = http.request(req_method, url, lavalink_headers, req_body)
if res.code == 204 then
self:debug("%s %s %s", req_method, url, req_body)
return nil
end
if res.code ~= 200 then
self:debug("%s %s %s", req_method, url, req_body)
self:debug(
'Something went wrong with lavalink server. '
.. 'Status code: %s\n Headers: %s',
res.code, json.encode(options.headers)
)
return nil
end
local final_data = json.decode(res_body_string)
self:debug("%s %s %s", req_method, url, req_body)
return final_data or res_body_string
end
function LunaStreamDriver:wsClose()
if self._wsClient then self._wsClient:close(1000, 'Self close') end
end
function LunaStreamDriver:updateSession(sessionId, mode, timeout)
local options = {
path = sf('/sessions/%s', sessionId),
headers = {
{ 'content-type', 'application/json' },
},
method = 'PATCH',
data = {
resuming = mode,
timeout = timeout,
}
}
self:debug("Session updated! resume: %s, timeout: %s", mode, timeout)
self:requester(options)
end
return LunaStreamDriver