Stored data
A command can remember things: a counter, a score, a ranking, a cooldown, a list. What it stores stays until it is removed (or until it expires), and every command of the server can read it.
{{ $uses := dbIncr "uses" 1 }}This command has been used {{ $uses }} times.Where the data is
Section titled “Where the data is”Each server has its own space. No server can read what another one stored. Inside a server, data is either:
- Of the server: one value for the whole server, for example
"uses". - Of a member: a value for each person, for example the points of every member. Give the user ID as the last argument.
Functions
Section titled “Functions”Keys are text of 1 to 100 letters, numbers, ., _, : or -. A user ID is text, in quotes.
| Function | What it does |
|---|---|
dbSet key value |
Stores a value. dbSet key value "userID" stores it for a member |
dbSetExpire key value seconds |
The same, and it disappears after that time (1 second to a year). Also takes "userID" last |
dbGet key |
The value, or nil if there is none. dbGet key "userID" for a member |
dbDel key |
Removes it. dbDel key "userID" for a member |
dbIncr key amount |
Adds to a number and gives the new number. It starts from 0 if there is nothing yet. Use a negative amount to subtract. Two people using it at the same time never lose a count. dbIncr key amount "userID" for a member |
dbTop key count |
The members with the highest number under that key, from the highest, up to 25. Each one is a map with UserID and Value |
dbKeys prefix |
The keys of the server that start with the prefix, up to 100. dbKeys prefix "userID" for a member. Without a prefix it gives all of them |
dbIncr gives back the new number, so it prints it. When you do not want it printed, save it in a variable:
{{ $ignored := dbIncr "uses" 1 }}.
What can be stored
Section titled “What can be stored”Text, numbers, true and false, lists and maps, with lists and maps inside them (up to 6 levels). An embed or a message made with
cembed or complexMessage cannot be stored. Once stored, a value comes back as it was.
{{ dbSet "profile" (sdict "color" "pink" "tags" (cslice "a" "b")) .User.ID }}{{ (dbGet "profile" .User.ID).color }}Limits
Section titled “Limits”| Limit | Value |
|---|---|
| Values per server | 500 |
| Size of a value | 4,000 characters once stored |
| Reads and writes per use of a command | 25 |
| Length of a key | 100 characters |
dbTop |
up to 25 members |
When a server is full, a value that already exists can still change, but a new key is refused until something is removed. Expired values stop counting at once, and are cleared from the database every hour.
Trying code that stores things
Section titled “Trying code that stores things”!customcommand codetest keeps what the code stores only until the end of that test. Testing never changes the data your
server has stored.
Patterns
Section titled “Patterns”A counter
Section titled “A counter”{{ $total := dbIncr "hugs" 1 }}{{ .User.Mention }} gave a hug. Hugs given in this server: **{{ $total }}**.Points for each member, with a ranking
Section titled “Points for each member, with a ranking”{{ $action := "top" }}{{ if .Args }}{{ $action = index .Args 0 }}{{ end }}{{ if eq $action "add" }} {{ $points := dbIncr "points" 10 .User.ID }} You have **{{ $points }}** points.{{ else }} {{ range $i, $entry := dbTop "points" 5 }}{{ add $i 1 }}. {{ mentionUser $entry.UserID }}: {{ $entry.Value }} {{ end }}{{ end }}A cooldown for each member
Section titled “A cooldown for each member”{{ if dbGet "daily" .User.ID }}You already claimed it today. Come back later.{{ return }}{{ end }}{{ dbSetExpire "daily" true 86400 .User.ID }}{{ $coins := dbIncr "coins" 100 .User.ID }}You claimed 100 coins. You have {{ $coins }}.A list
Section titled “A list”{{ $list := or (dbGet "todo") (cslice) }}{{ dbSet "todo" (append $list .RawArgs) }}Added. The list has {{ add (len $list) 1 }} items.