📚
YAGPDB
  • YAGPDB Coding System - Documentation
  • Getting Started
  • Useful Functions
  • Primary Level
    • The Dot and Variables
      • User
      • Guild / Server
      • Member
      • Channel
      • Message
      • Reaction
      • Time
    • Custom Types
    • Functions
      • Type Conversion
      • String Manipulation
      • Math Functions
      • Message Functions
      • Mentions
      • Role Functions
      • Current User
      • Miscellaneous
      • ExecCC
    • Conditional Branching
  • Intermediate Level
  • Database
  • Range Action
  • With Action
  • Custom Embeds
  • Advanced Level
    • Using RegEx
  • Custom Commands Examples
    • Texas Hold'em
Powered by GitBook
On this page
  • Preface
  • Info
  • Function
  • DBEntry

Was this helpful?

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: |.

{{$input := (joinStr " " .CmdArgs)}}
{{$expected := ((dbGet 0 "expected").Value)}}
{{if not $expected}}
  {{dbSet 0 "expected" $input}}
{{else}}
  {{if eq (toInt $input) (toInt $expected)}}
    {{$expected = (add $expected 1)}}
    {{dbSet 0 "expected" $expected}}
  {{else}}
    {{deleteTrigger 3}}
    {{sendDM "Please count the correct number."}}
  {{end}}
{{end}}

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 userIDof 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.

If you use ExpiresAt with a permanent database, it will output 0001-01-01 00:00:00 +0000 UTC.

Example

  • To demonstrate dbEntry.

{{dbSetExpire 0 "test" "The db will be deleted after 120 seconds" 120}}
{{with (dbGet 0 "test")}} {{/*Save the db objects into the "."*/}}
```
Expires At: {{.ExpiresAt}}
ID for this db: {{.ID}}; ServerID: {{.GuildID}}
UserID: {{.UserID}}. Same as: {{.User.ID}}
Created At: {{.CreatedAt}}
Key: {{.Key}}
Value: {{.Value}}
```
{{end}}
{{dbSet 0 "test2" "Hi"}}
{{sleep 2}}
{{dbSetExpire 0 "test2" "Bye" 10}}
```
Created At: {{(dbGet 0 "test2").CreatedAt}}
Updated At: {{(dbGet 0 "test2").UpdatedAt}}
```

If you use dbSetExpire (it will be deleted after 10 seconds for example) first and then use dbSet to save a new value into the same database, the database will not be deleted 10 seconds later because you use dbSet for its last update.

{{dbSetExpire 0 "Hi" "test" 5}}
{{dbSet 0 "Hi" "test"}}
{{sendMessage nil (joinStr " " "Before 5 seconds:" (dbGet 0 "Hi").Value)}}
{{sleep 6}}
{{sendMessage nil (joinStr " " "After 5 seconds:" (dbGet 0 "Hi").Value)}}
It doesn't get deleted.
  • To demonstrate the way to delete a message with a database.

{{$id := sendMessageRetID nil "Hi"}}
{{dbSet 0 "test" (toString $id)}}
{{sleep 5}}
{{deleteMessage nil (toInt64 (dbGet 0 "test").Value) 1}}
{{dbDel 0 "test"}}

Remember to toString the $id, or the value saved inside the database may be changed to an unwanted value, which is not valid to delete the message.

PreviousConditional BranchingNextRange Action

Last updated 4 years ago

Was this helpful?

.

user object