Working With Data

From CWS Wiki
Jump to: navigation, search


One of the most important steps in implementing more advanced functionality in your program, especially in discord bots, is working with data. Typically you'll want to save application state and/or user data in a way that persists across command calls and even application restarts. This becomes all too apparent when trying to create "user accounts" with your bot, since storing large quantities of data in memory is terribly inefficient and bad practice.


Beware that Discord's new Terms of Service dictate that all End User data must be encrypted. Keep this in mind when attempting to store sensitive data such as user messages!

File-Based Storage

This is the simplest of all the following methods, and is best suited to infrequently accessed and low-volume data workloads.

  • JSON Serialisation
    • The most basic method of storing data beyond just writing raw strings to file.
  • LiteDb
    • Database-In-A-File solution, supports multiple tables + cross-referencing, built for .NET
  • EfCore Sqlite
    • Using EntityFrameworkCore, Microsoft's data access framework with the Sqlite, an easy to use and customisable serverless database much like LiteDb can be configured.

LiteDb or EfCore are slightly more complex to set up but easier to manage in the long-run, and are capable of handling medium-volume workloads when necessary, perfect for smaller bots or bots for only one server.

Hosted Storage

Hosted Databases are a must-have for bots under frequent heavy load and requiring complex/high-volume data storage capabilities.

  • EfCore Hosted Database
    • EntityFrameworkCore has a wide variety of compatible database providers allowing pretty much any more heavy-duty database to be used with your bot. Thanks to the layer of abstraction this provides, it should be pretty easy to switch from one to the other, requiring perhaps some model tweaking at most.