diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/choose_server.html.luan	Sun Oct 29 22:39:31 2023 -0600
@@ -0,0 +1,102 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local stringify = Luan.stringify or error()
+local ipairs = Luan.ipairs or error()
+local Parsers = require "luan:Parsers.luan"
+local json_parse = Parsers.json_parse or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local url_encode = Html.url_encode or error()
+local Io = require "luan:Io.luan"
+local uri = Io.uri or error()
+local Http = require "luan:http/Http.luan"
+local Shared = require "site:/lib/Shared.luan"
+local head = Shared.head or error()
+local header = Shared.header or error()
+local footer = Shared.footer or error()
+local Config = require "site:/lib/Config.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "choose_server.html"
+
+
+local function get_access_token()
+	local url = "https://discord.com/api/oauth2/token"
+	local config = Config.get()
+	local options = {
+		method = "POST"
+		headers = {
+			["User-Agent"] = "fuck you"  -- for retarded Cloudflare
+		}
+		authorization = {
+			username = config.discord.client_id
+			password = config.discord.client_secret
+			type = "basic"
+		}
+		parameters = {
+			grant_type = "refresh_token"
+			refresh_token = Http.request.cookies.refresh_token or error()
+		}
+	}
+	-- logger.info(stringify(options))
+	local result = uri(url,options).read_text()
+	-- logger.info(result)
+	result = json_parse(result)
+	local access_token = result.access_token or error()
+	local refresh_token = result.refresh_token or error()
+	-- logger.info("access_token = "..access_token)
+	Http.response.set_persistent_cookie("refresh_token",refresh_token)
+	return access_token
+end
+
+return function()
+	local access_token = get_access_token()
+	local url = "https://discord.com/api/users/@me/guilds"
+	local options = {
+		headers = {
+			["User-Agent"] = "fuck you"  -- for retarded Cloudflare
+			Authorization = "Bearer "..access_token
+		}
+	}
+	local result = uri(url,options).read_text()
+	-- logger.info(result)
+	result = json_parse(result)
+	-- logger.info(stringify(result))
+	local servers = {}
+	for _, server in ipairs(result) do
+		if server.owner then
+			servers[#servers+1] = server
+		end
+	end
+	logger.info(stringify(servers))
+
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Disearch</title>
+	</head>
+	<body>
+<%		header() %>
+
+		<h1>Choose server to add</h1>
+<%
+	for _, server in ipairs(servers) do
+		local id = server.id or error()
+		local name = server.name or error()
+		local icon = server.icon
+		local url = "add_server.html?id="..id.."&name="..url_encode(name)
+		if icon ~= nil then
+			url = url.."&icon="..icon
+		end
+%>
+		<p><a href="<%=url%>"><%=html_encode(name)%></a></p>
+<%
+	end
+%>
+<%		footer() %>
+	</body>
+</html>
+<%
+end