diff src/add_server.html.luan @ 14:80105b716a62

start bot
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 31 Oct 2023 23:56:48 -0600
parents 3e2cb946d511
children d0b8e1a8fc69
line wrap: on
line diff
--- a/src/add_server.html.luan	Mon Oct 30 21:32:42 2023 -0600
+++ b/src/add_server.html.luan	Tue Oct 31 23:56:48 2023 -0600
@@ -2,20 +2,68 @@
 local error = Luan.error
 local Html = require "luan:Html.luan"
 local html_encode = Html.encode or error()
+local Parsers = require "luan:Parsers.luan"
+local json_parse = Parsers.json_parse 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 Server = require "site:/lib/Server.luan"
+local Config = require "site:/lib/Config.luan"
 
 
+local function error_page(msg)
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Disearch</title>
+	</head>
+	<body>
+<%		header() %>
+		<p><%=msg%></p>
+<%		footer() %>
+	</body>
+</html>
+<%
+end
+
 return function()
 	local parameters = Http.request.parameters
-	local id = parameters.id or error()
-	local name = parameters.name or error()
+	if parameters.error == "access_denied" then
+		Http.response.send_redirect("/servers.html")
+		return
+	end
+	local guild_id = parameters.guild_id or error()
+	if Server.get_by_discord_id(guild_id) ~= nil then
+		error_page("already added")
+		return
+	end
+
+	local url = "https://discord.com/api/guilds/"..guild_id
+	local config = Config.get()
+	local options = {
+		headers = {
+			["User-Agent"] = "fuck you"  -- for retarded Cloudflare
+			Authorization = "Bot "..config.discord.bot_token
+		}
+	}
+	local result = uri(url,options).read_text()
+	--logger.info(result)
+	result = json_parse(result)
+
+	local name = result.name or error()
 	name = html_encode(name)
-	local icon = parameters.icon or error()
+	local icon = result.icon
+	if icon == nil then
+		error_page("server must have icon")
+		return
+	end
 	Io.stdout = Http.response.text_writer()
 %>
 <!doctype html>
@@ -35,11 +83,11 @@
 		<h1>Add server</h1>
 
 		<form page onsubmit="ajaxForm('/add_server.js',this); return false">
-			<input type=hidden name=id value="<%=id%>">
+			<input type=hidden name=id value="<%=guild_id%>">
 			<input type=hidden name=name value="<%=name%>">
 			<input type=hidden name=icon value="<%=icon%>">
 			<p><%=name%></p>
-			<p><img icon src="https://cdn.discordapp.com/icons/<%=id%>/<%=icon%>.png"></p>
+			<p><img icon src="https://cdn.discordapp.com/icons/<%=guild_id%>/<%=icon%>.png"></p>
 			<p>
 				Invite URL:<br>
 				<input type=url required name=invite>