1
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
11
|
3 local Html = require "luan:Html.luan"
|
|
4 local html_encode = Html.encode or error()
|
14
|
5 local Parsers = require "luan:Parsers.luan"
|
|
6 local json_parse = Parsers.json_parse or error()
|
1
|
7 local Io = require "luan:Io.luan"
|
14
|
8 local uri = Io.uri or error()
|
1
|
9 local Http = require "luan:http/Http.luan"
|
|
10 local Shared = require "site:/lib/Shared.luan"
|
|
11 local head = Shared.head or error()
|
|
12 local header = Shared.header or error()
|
|
13 local footer = Shared.footer or error()
|
14
|
14 local Server = require "site:/lib/Server.luan"
|
|
15 local Config = require "site:/lib/Config.luan"
|
1
|
16
|
|
17
|
14
|
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
|
|
35
|
1
|
36 return function()
|
11
|
37 local parameters = Http.request.parameters
|
14
|
38 if parameters.error == "access_denied" then
|
|
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()
|
12
|
61 name = html_encode(name)
|
14
|
62 local icon = result.icon
|
|
63 if icon == nil then
|
|
64 error_page("server must have icon")
|
|
65 return
|
|
66 end
|
1
|
67 Io.stdout = Http.response.text_writer()
|
|
68 %>
|
|
69 <!doctype html>
|
|
70 <html>
|
|
71 <head>
|
|
72 <% head() %>
|
|
73 <title>Disearch</title>
|
12
|
74 <style>
|
|
75 input[type="url"] {
|
|
76 width: 100%;
|
|
77 }
|
|
78 </style>
|
1
|
79 </head>
|
|
80 <body>
|
|
81 <% header() %>
|
11
|
82
|
|
83 <h1>Add server</h1>
|
|
84
|
12
|
85 <form page onsubmit="ajaxForm('/add_server.js',this); return false">
|
14
|
86 <input type=hidden name=id value="<%=guild_id%>">
|
12
|
87 <input type=hidden name=name value="<%=name%>">
|
|
88 <input type=hidden name=icon value="<%=icon%>">
|
|
89 <p><%=name%></p>
|
14
|
90 <p><img icon src="https://cdn.discordapp.com/icons/<%=guild_id%>/<%=icon%>.png"></p>
|
12
|
91 <p>
|
|
92 Invite URL:<br>
|
|
93 <input type=url required name=invite>
|
|
94 <span error=invite></span>
|
|
95 </p>
|
|
96 <p><input type=submit></p>
|
|
97 </form>
|
11
|
98
|
1
|
99 <% footer() %>
|
|
100 </body>
|
|
101 </html>
|
|
102 <%
|
|
103 end
|