comparison 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
comparison
equal deleted inserted replaced
13:af2b95854072 14:80105b716a62
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local Html = require "luan:Html.luan" 3 local Html = require "luan:Html.luan"
4 local html_encode = Html.encode or error() 4 local html_encode = Html.encode or error()
5 local Parsers = require "luan:Parsers.luan"
6 local json_parse = Parsers.json_parse or error()
5 local Io = require "luan:Io.luan" 7 local Io = require "luan:Io.luan"
8 local uri = Io.uri or error()
6 local Http = require "luan:http/Http.luan" 9 local Http = require "luan:http/Http.luan"
7 local Shared = require "site:/lib/Shared.luan" 10 local Shared = require "site:/lib/Shared.luan"
8 local head = Shared.head or error() 11 local head = Shared.head or error()
9 local header = Shared.header or error() 12 local header = Shared.header or error()
10 local footer = Shared.footer or error() 13 local footer = Shared.footer or error()
14 local Server = require "site:/lib/Server.luan"
15 local Config = require "site:/lib/Config.luan"
11 16
17
18 local function error_page(msg)
19 Io.stdout = Http.response.text_writer()
20 %>
21 <!doctype html>
22 <html>
23 <head>
24 <% head() %>
25 <title>Disearch</title>
26 </head>
27 <body>
28 <% header() %>
29 <p><%=msg%></p>
30 <% footer() %>
31 </body>
32 </html>
33 <%
34 end
12 35
13 return function() 36 return function()
14 local parameters = Http.request.parameters 37 local parameters = Http.request.parameters
15 local id = parameters.id or error() 38 if parameters.error == "access_denied" then
16 local name = parameters.name or error() 39 Http.response.send_redirect("/servers.html")
40 return
41 end
42 local guild_id = parameters.guild_id or error()
43 if Server.get_by_discord_id(guild_id) ~= nil then
44 error_page("already added")
45 return
46 end
47
48 local url = "https://discord.com/api/guilds/"..guild_id
49 local config = Config.get()
50 local options = {
51 headers = {
52 ["User-Agent"] = "fuck you" -- for retarded Cloudflare
53 Authorization = "Bot "..config.discord.bot_token
54 }
55 }
56 local result = uri(url,options).read_text()
57 --logger.info(result)
58 result = json_parse(result)
59
60 local name = result.name or error()
17 name = html_encode(name) 61 name = html_encode(name)
18 local icon = parameters.icon or error() 62 local icon = result.icon
63 if icon == nil then
64 error_page("server must have icon")
65 return
66 end
19 Io.stdout = Http.response.text_writer() 67 Io.stdout = Http.response.text_writer()
20 %> 68 %>
21 <!doctype html> 69 <!doctype html>
22 <html> 70 <html>
23 <head> 71 <head>
33 <% header() %> 81 <% header() %>
34 82
35 <h1>Add server</h1> 83 <h1>Add server</h1>
36 84
37 <form page onsubmit="ajaxForm('/add_server.js',this); return false"> 85 <form page onsubmit="ajaxForm('/add_server.js',this); return false">
38 <input type=hidden name=id value="<%=id%>"> 86 <input type=hidden name=id value="<%=guild_id%>">
39 <input type=hidden name=name value="<%=name%>"> 87 <input type=hidden name=name value="<%=name%>">
40 <input type=hidden name=icon value="<%=icon%>"> 88 <input type=hidden name=icon value="<%=icon%>">
41 <p><%=name%></p> 89 <p><%=name%></p>
42 <p><img icon src="https://cdn.discordapp.com/icons/<%=id%>/<%=icon%>.png"></p> 90 <p><img icon src="https://cdn.discordapp.com/icons/<%=guild_id%>/<%=icon%>.png"></p>
43 <p> 91 <p>
44 Invite URL:<br> 92 Invite URL:<br>
45 <input type=url required name=invite> 93 <input type=url required name=invite>
46 <span error=invite></span> 94 <span error=invite></span>
47 </p> 95 </p>