Skip to content

Using Key Value Load Save

Craig Edwards edited this page Nov 14, 2019 · 3 revisions

Sporks will allow you to save persistent data to its database, which is isolated and accessible only to events triggered within your guild.

Loading values

Calling the load() function will cause the bot to attempt to find a key in its key/value database, for example:

var who_it_was = load("last_user_id");
create_message(CHANNEL_ID, "the last user id was " + who_it_was);

Saving values

Conversely, calling save() will save a key to the database:

save("last_user_id", someuser.id);

Saving and loading whole objects

Note that the key/value store is designed to store singular values. If you want to store an object as a value, you should pass it through JSON.stringify() to turn it into a string, or when reading it back, use JSON.parse():

save("last_user", JSON.stringify(someuser));
var someuser = JSON.parse(load("last_user"));

Please be aware that load() and save() can be expensive calls in terms of processing time and can eat into your 10 milliseconds CPU budget per event. Plan out your usage of load() and save() to minimise the number of calls to these methods and the amount of data stored.

Limits

  • The load() and save() methods save your data to a database which has a limit of 1024 key/value pairs per guild. This limitation is in place to prevent any one user from taking up all the disk space.
  • Each key has a maximum length of 100
  • Each value has a maximum length of 65536 bytes, this may not be equal to the number of characters stored if you are storing any international characters, emoji, etc.
  • Time spent fetching key/value items from the database still counts towards your 10ms CPU quota.