annotate src/lib/Server.luan @ 14:80105b716a62

start bot
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 31 Oct 2023 23:56:48 -0600
parents af2b95854072
children 552d6f944acb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local ipairs = Luan.ipairs or error()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Db = require "site:/lib/Db.luan"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Utils = require "site:/lib/Utils.luan"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local get_user = Utils.get_user or error()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Server = {}
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local function from_doc(doc)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 doc.type == "server" or error "wrong type"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 return Server.new {
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 id = doc.id
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
15 discord_id = doc.server_discord_id
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 owner_discord_id = doc.server_owner_discord_id
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 name = doc.name
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 icon = doc.icon
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 invite = doc.invite
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local function to_doc(server)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 return {
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 type = "server"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 id = server.id
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
27 server_discord_id = server.discord_id or error()
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 server_owner_discord_id = server.owner_discord_id or error()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 name = server.name or error()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 icon = server.icon or error()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 invite = server.invite or error()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 function Server.new(server)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 function server.save()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 local doc = to_doc(server)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 Db.save(doc)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 server.id = doc.id
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 function server.reload()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 return Server.get_by_id(server.id) or error(server.id)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 function server.delete()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 Db.run_in_transaction( function()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 Db.delete("id:"..server.id)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 end )
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 function server.write_img_html()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 %><img icon src="https://cdn.discordapp.com/icons/<%=server.discord_id%>/<%=server.icon%>.png"><%
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 function server.invite_url()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 return "https://discord.gg/"..server.invite
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 return server
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 function Server.get_by_id(id)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 local doc = Db.get_document("id:"..id)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 return doc and from_doc(doc)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
69 function Server.get_by_discord_id(discord_id)
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
70 local doc = Db.get_document("server_discord_id:"..discord_id)
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
71 return doc and from_doc(doc)
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
72 end
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
73
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 local function search(query,sort,rows)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 rows = rows or 1000000
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 local docs = Db.search(query,1,rows,{sort=sort})
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 local servers = {}
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 for _, doc in ipairs(docs) do
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 local server = from_doc(doc)
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 servers[#servers+1] = server
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 return servers
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 function Server.get_user_servers()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 return search( "server_owner_discord_id:"..get_user() )
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 return Server