1
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
11
|
3 local stringify = Luan.stringify or error()
|
|
4 local ipairs = Luan.ipairs or error()
|
|
5 local Parsers = require "luan:Parsers.luan"
|
|
6 local json_parse = Parsers.json_parse or error()
|
|
7 local Html = require "luan:Html.luan"
|
|
8 local html_encode = Html.encode or error()
|
|
9 local url_encode = Html.url_encode or error()
|
1
|
10 local Io = require "luan:Io.luan"
|
11
|
11 local uri = Io.uri or error()
|
1
|
12 local Http = require "luan:http/Http.luan"
|
|
13 local Shared = require "site:/lib/Shared.luan"
|
|
14 local head = Shared.head or error()
|
|
15 local header = Shared.header or error()
|
|
16 local footer = Shared.footer or error()
|
3
|
17 local Config = require "site:/lib/Config.luan"
|
11
|
18 local Logging = require "luan:logging/Logging.luan"
|
|
19 local logger = Logging.logger "choose_server.html"
|
1
|
20
|
|
21
|
11
|
22 local function get_access_token()
|
|
23 local url = "https://discord.com/api/oauth2/token"
|
|
24 local config = Config.get()
|
|
25 local options = {
|
|
26 method = "POST"
|
|
27 headers = {
|
|
28 ["User-Agent"] = "fuck you" -- for retarded Cloudflare
|
|
29 }
|
|
30 authorization = {
|
|
31 username = config.discord.client_id
|
|
32 password = config.discord.client_secret
|
|
33 type = "basic"
|
|
34 }
|
|
35 parameters = {
|
|
36 grant_type = "refresh_token"
|
|
37 refresh_token = Http.request.cookies.refresh_token or error()
|
|
38 }
|
|
39 }
|
|
40 -- logger.info(stringify(options))
|
|
41 local result = uri(url,options).read_text()
|
|
42 -- logger.info(result)
|
|
43 result = json_parse(result)
|
|
44 local access_token = result.access_token or error()
|
|
45 local refresh_token = result.refresh_token or error()
|
|
46 -- logger.info("access_token = "..access_token)
|
|
47 Http.response.set_persistent_cookie("refresh_token",refresh_token)
|
|
48 return access_token
|
|
49 end
|
|
50
|
1
|
51 return function()
|
11
|
52 local access_token = get_access_token()
|
|
53 local url = "https://discord.com/api/users/@me/guilds"
|
|
54 local options = {
|
|
55 headers = {
|
|
56 ["User-Agent"] = "fuck you" -- for retarded Cloudflare
|
|
57 Authorization = "Bearer "..access_token
|
|
58 }
|
|
59 }
|
|
60 local result = uri(url,options).read_text()
|
|
61 -- logger.info(result)
|
|
62 result = json_parse(result)
|
|
63 -- logger.info(stringify(result))
|
|
64 local servers = {}
|
|
65 for _, server in ipairs(result) do
|
|
66 if server.owner then
|
|
67 servers[#servers+1] = server
|
|
68 end
|
|
69 end
|
|
70 logger.info(stringify(servers))
|
|
71
|
1
|
72 Io.stdout = Http.response.text_writer()
|
|
73 %>
|
|
74 <!doctype html>
|
|
75 <html>
|
|
76 <head>
|
|
77 <% head() %>
|
|
78 <title>Disearch</title>
|
|
79 </head>
|
|
80 <body>
|
|
81 <% header() %>
|
11
|
82
|
|
83 <h1>Choose server to add</h1>
|
|
84 <%
|
|
85 for _, server in ipairs(servers) do
|
|
86 local id = server.id or error()
|
|
87 local name = server.name or error()
|
|
88 local icon = server.icon
|
|
89 if icon ~= nil then
|
12
|
90 local url = "add_server.html?id="..id.."&name="..url_encode(name).."&icon="..icon
|
|
91 %>
|
|
92 <p><a href="<%=url%>"><%=html_encode(name)%></a></p>
|
|
93 <%
|
|
94 else
|
|
95 %>
|
|
96 <p><%=html_encode(name)%> (missing icon)</p>
|
|
97 <%
|
11
|
98 end
|
|
99 end
|
|
100 %>
|
1
|
101 <% footer() %>
|
|
102 </body>
|
|
103 </html>
|
|
104 <%
|
|
105 end
|