Database
Database is useful when you want the bot to help you memorize things
Preface
When you need to remember all of the members' levels, what should you do --- use the database
functions! What can you do to make a cooldown for every member --- use the database
functions! If you want to make a channel where your members can count in order, consider the database
functions to prevent them from counting the wrong number or counting two times.
Read the following codes. Command type: Regex
; Trigger: |
.
When a member counts a number, the bot will search for the database whose key is "expected" and then get the value of it (the expected number). If the number the member counts is correct, it won't do anything, while it will automatically delete the number the member counts and send a dm message (Please count the correct number.) to the member if it is wrong.
There are more complex commands using database
functions, but we won't take them for examples. The only one thing that you need to know is that the database
functions are useful when you want the bot to help you memorize things.
Info
You have access to a basic set of Database functions, this is almost a key value store ordered by the key and value combined.
You can have max 50 * user_count (or 500 * user_count for premium) values in the database, if you go above this all new write functions will fail, this value is also cached so it won't be detected immediately when you go above nor immediately when you're under again.
Patterns are basic PostgreSQL patterns, not Regexp: An underscore (_)
matches any single character; a percent sign (%)
matches any sequence of zero or more characters.
Keys can be max 256 bytes long and has to be strings or numbers. Values can be anything, but if they go above 100KB they will be truncated.
You can just pass a userID
of 0 to make it global (or any other number, but 0 is safe).
There can be 10 database interactions per CC, out of which dbTop/BottomEntries
, dbCount
and dbGetPattern
may be only run twice. (50,10 for premium users).
Function
Function
Description
dbSet userID key value
Sets the value for the specified key
for the specific userID
to the specified value
. userID
can be any number of type int64.
Values are stored either as of type float64 (for numbers, oct or hex) or as varying type in bytes (for slices, maps, strings etc) depending on the input argument.
dbSetExpire userID key value ttl
Same as dbSet
but with expiration in seconds.
dbIncr userID key incrBy
Increments the value for a specified key for the specified user, if there was no value then it will be set to incrBy .
Also returns the entry's current, increased value.
dbGet userID key
Retrieves a value from the database for the specified user, this returns DBEntry object.
dbGetPattern userID pattern amount nSkip
Retrieves up toamount (max 100)
entries from the database in ascending order.
dbGetPatternReverse userID pattern amount nSkip
Retrievesamount (max 100)
entries from the database in descending order.
dbDel userID key
Deletes the specified key for the specified value from the database.
dbDelByID userID ID
Deletes database entry by its ID.
dbTopEntries pattern amount nSkip
Returns amount (max 100)
top entries from the database, sorted by the value in descending order.
dbBottomEntries pattern amount nSkip
Returns amount (max 100)
top entries from the database, sorted by the value in ascending order.
dbCount (userID|key)
Returns the count of all database entries which are not expired. Optional arguments: if userID
is given, counts entries for that userID or if key
, then only those keys are counted.
DBEntry
Fields
Description
.ID
ID of the entry.
.GuildID
ID of the server.
.UserID
ID of the user.
.User
.CreatedAt
When this entry was created.
.UpdatedAt
When this entry was last updated.
.ExpiresAt
When entry will expire.
.Key
The key of the entry.
.Value
The value of the entry.
Example
To demonstrate
dbEntry
.
To demonstrate the way to delete a message with a database.
Last updated
Was this helpful?