comparison src/choose_server.html.luan @ 11:62dd23f0b549

start add server
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 29 Oct 2023 22:39:31 -0600
parents src/index.html.luan@43814e9f5802
children 3e2cb946d511
comparison
equal deleted inserted replaced
10:ae9ebeb1c246 11:62dd23f0b549
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
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()
10 local Io = require "luan:Io.luan"
11 local uri = Io.uri or error()
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()
17 local Config = require "site:/lib/Config.luan"
18 local Logging = require "luan:logging/Logging.luan"
19 local logger = Logging.logger "choose_server.html"
20
21
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
51 return function()
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
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() %>
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 local url = "add_server.html?id="..id.."&name="..url_encode(name)
90 if icon ~= nil then
91 url = url.."&icon="..icon
92 end
93 %>
94 <p><a href="<%=url%>"><%=html_encode(name)%></a></p>
95 <%
96 end
97 %>
98 <% footer() %>
99 </body>
100 </html>
101 <%
102 end