12
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local String = require "luan:String.luan"
|
|
4 local regex = String.regex or error()
|
|
5 local Io = require "luan:Io.luan"
|
|
6 local Http = require "luan:http/Http.luan"
|
|
7 local Shared = require "site:/lib/Shared.luan"
|
|
8 local js_error = Shared.js_error or error()
|
13
|
9 local Server = require "site:/lib/Server.luan"
|
|
10 local Utils = require "site:/lib/Utils.luan"
|
|
11 local get_user = Utils.get_user or error()
|
|
12 local Db = require "site:/lib/Db.luan"
|
|
13 local run_in_transaction = Db.run_in_transaction or error()
|
12
|
14 local Logging = require "luan:logging/Logging.luan"
|
|
15 local logger = Logging.logger "add_server.js"
|
|
16
|
|
17
|
|
18 local invite_regex = regex[[^https://discord.gg/(\w+)$]]
|
|
19
|
|
20 return function()
|
|
21 Io.stdout = Http.response.text_writer()
|
|
22 local parameters = Http.request.parameters
|
|
23 local id = parameters.id or error()
|
|
24 local name = parameters.name or error()
|
|
25 local icon = parameters.icon or error()
|
|
26 local invite = parameters.invite or error()
|
|
27 invite = invite_regex.match(invite)
|
|
28 logger.info("invite = "..invite)
|
|
29 if invite==nil then
|
|
30 js_error( "invite", [[Invalid Discord invite URL]] )
|
|
31 return
|
|
32 end
|
13
|
33 run_in_transaction( function()
|
|
34 local server = Server.new{
|
|
35 owner_discord_id = get_user() or error()
|
|
36 discord_id = id
|
|
37 name = name
|
|
38 icon = icon
|
|
39 invite = invite
|
|
40 }
|
|
41 server.save()
|
|
42 end )
|
12
|
43 %>
|
|
44 location = '/servers.html';
|
|
45 <%
|
|
46 end
|