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