1
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
11
|
3 local stringify = Luan.stringify or error()
|
|
4 local ipairs = Luan.ipairs or error()
|
16
|
5 local Table = require "luan:Table.luan"
|
|
6 local sort = Table.sort or error()
|
|
7 local Time = require "luan:Time.luan"
|
|
8 local time_now = Time.now or error()
|
11
|
9 local Parsers = require "luan:Parsers.luan"
|
|
10 local json_parse = Parsers.json_parse or error()
|
|
11 local Html = require "luan:Html.luan"
|
|
12 local html_encode = Html.encode or error()
|
|
13 local url_encode = Html.url_encode or error()
|
1
|
14 local Io = require "luan:Io.luan"
|
11
|
15 local uri = Io.uri or error()
|
1
|
16 local Http = require "luan:http/Http.luan"
|
|
17 local Shared = require "site:/lib/Shared.luan"
|
|
18 local head = Shared.head or error()
|
|
19 local header = Shared.header or error()
|
|
20 local footer = Shared.footer or error()
|
16
|
21 local ago = Shared.ago or error()
|
3
|
22 local Config = require "site:/lib/Config.luan"
|
13
|
23 local Server = require "site:/lib/Server.luan"
|
16
|
24 local get_server = Server.get_by_discord_id or error()
|
|
25 local bumped_sort = Server.bumped_sort or error()
|
11
|
26 local Logging = require "luan:logging/Logging.luan"
|
16
|
27 local logger = Logging.logger "bump.html"
|
1
|
28
|
|
29
|
11
|
30 local function get_access_token()
|
|
31 local url = "https://discord.com/api/oauth2/token"
|
|
32 local config = Config.get()
|
|
33 local options = {
|
|
34 method = "POST"
|
|
35 headers = {
|
16
|
36 ["User-Agent"] = "Disboard" -- for retarded Cloudflare
|
11
|
37 }
|
|
38 authorization = {
|
|
39 username = config.discord.client_id
|
|
40 password = config.discord.client_secret
|
|
41 type = "basic"
|
|
42 }
|
|
43 parameters = {
|
|
44 grant_type = "refresh_token"
|
|
45 refresh_token = Http.request.cookies.refresh_token or error()
|
|
46 }
|
|
47 }
|
|
48 -- logger.info(stringify(options))
|
|
49 local result = uri(url,options).read_text()
|
|
50 -- logger.info(result)
|
|
51 result = json_parse(result)
|
|
52 local access_token = result.access_token or error()
|
|
53 local refresh_token = result.refresh_token or error()
|
|
54 -- logger.info("access_token = "..access_token)
|
|
55 Http.response.set_persistent_cookie("refresh_token",refresh_token)
|
|
56 return access_token
|
|
57 end
|
|
58
|
1
|
59 return function()
|
11
|
60 local access_token = get_access_token()
|
|
61 local url = "https://discord.com/api/users/@me/guilds"
|
|
62 local options = {
|
|
63 headers = {
|
16
|
64 ["User-Agent"] = "Disboard" -- for retarded Cloudflare
|
11
|
65 Authorization = "Bearer "..access_token
|
|
66 }
|
|
67 }
|
|
68 local result = uri(url,options).read_text()
|
|
69 -- logger.info(result)
|
|
70 result = json_parse(result)
|
|
71 -- logger.info(stringify(result))
|
13
|
72
|
11
|
73 local servers = {}
|
|
74 for _, server in ipairs(result) do
|
16
|
75 servers[#servers+1] = get_server(server.id)
|
11
|
76 end
|
16
|
77 sort(servers,bumped_sort)
|
|
78 -- logger.info(stringify(servers))
|
11
|
79
|
1
|
80 Io.stdout = Http.response.text_writer()
|
|
81 %>
|
|
82 <!doctype html>
|
|
83 <html>
|
|
84 <head>
|
|
85 <% head() %>
|
|
86 <title>Disearch</title>
|
|
87 </head>
|
|
88 <body>
|
|
89 <% header() %>
|
11
|
90
|
16
|
91 <h1>Bump Servers</h1>
|
11
|
92 <%
|
16
|
93 local now = time_now()
|
11
|
94 for _, server in ipairs(servers) do
|
12
|
95 %>
|
16
|
96 <p>
|
|
97 <%=html_encode(server.name)%>
|
|
98 <% server.write_img_html() %>
|
|
99 <a href="<%=server.invite_url()%>">invite</a>
|
|
100 <a href="javascript:ajax('/bump.js?server=<%=server.id%>')">bump</a>
|
|
101 bumped <% ago( now - server.bumped ) %>
|
|
102 </p>
|
12
|
103 <%
|
11
|
104 end
|
|
105 %>
|
1
|
106 <% footer() %>
|
|
107 </body>
|
|
108 </html>
|
|
109 <%
|
|
110 end
|