view src/choose_server.html.luan @ 14:80105b716a62

start bot
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 31 Oct 2023 23:56:48 -0600
parents af2b95854072
children
line wrap: on
line source

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 Utils = require "site:/lib/Utils.luan"
local list_to_set = Utils.list_to_set or error()
local Server = require "site:/lib/Server.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 set = list_to_set{}
	for _, server in ipairs(Server.get_user_servers()) do
		set[server.discord_id] = true
	end

	local servers = {}
	for _, server in ipairs(result) do
		if server.owner and not set[server.id] 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
		if icon ~= nil then
			local url = "add_server.html?id="..id.."&name="..url_encode(name).."&icon="..icon
%>
			<p><a href="<%=url%>"><%=html_encode(name)%></a></p>
<%
		else
%>
			<p><%=html_encode(name)%> (missing icon)</p>
<%
		end
	end
%>
<%		footer() %>
	</body>
</html>
<%
end