Data is a simple utility class for the management of plugin data. It allows you to save and load JSON-serializable data that persists between Discord sessions.
Methods
save
Saves JSON-serializable data.
BdApi.Data.save("MyPlugin", "settings", {theme: "dark", enabled: true});
Name of the plugin saving data. Only required when using the global API.
Which piece of data to store. Acts as an identifier for this data.
The data to be saved. Must be JSON-serializable (strings, numbers, booleans, objects, arrays, null).
load
Loads previously stored data.
const settings = BdApi.Data.load("MyPlugin", "settings");
Name of the plugin loading data. Only required when using the global API.
Which piece of data to load.
Returns: The stored data, or undefined if no data exists for that key
delete
Deletes a piece of stored data. This is different than saving null or undefined.
BdApi.Data.delete("MyPlugin", "settings");
Name of the plugin deleting data. Only required when using the global API.
Which piece of data to delete.
recache
Recaches JSON-serializable save file. This forces a reload of the data from disk.
Use of recaching is discouraged!Recache loads can block the filesystem and significantly degrade performance. Use this method only for debugging or testing purposes. Avoid frequent recaching in production environments.
await BdApi.Data.recache("MyPlugin");
Name of the plugin to recache data for. Only required when using the global API.
Returns: Promise<boolean> - Promise that resolves to true if recache succeeded
Examples
Basic settings management
class MyPlugin {
constructor() {
this.BdApi = new BdApi("MyPlugin");
}
start() {
// Load settings or use defaults
this.settings = this.BdApi.Data.load("settings") || {
enabled: true,
theme: "dark",
notifications: true
};
}
updateSetting(key, value) {
this.settings[key] = value;
this.BdApi.Data.save("settings", this.settings);
}
stop() {
// Save settings on plugin stop
this.BdApi.Data.save("settings", this.settings);
}
}
Multiple data keys
const BdApi = new BdApi("MyPlugin");
// Save different types of data with different keys
BdApi.Data.save("settings", {theme: "dark"});
BdApi.Data.save("cache", {lastUpdated: Date.now()});
BdApi.Data.save("user-data", {username: "John"});
// Load specific data
const settings = BdApi.Data.load("settings");
const cache = BdApi.Data.load("cache");
Handling missing data
const BdApi = new BdApi("MyPlugin");
function getSettings() {
const settings = BdApi.Data.load("settings");
if (!settings) {
// First time loading, use defaults
const defaults = {
enabled: true,
theme: "dark"
};
BdApi.Data.save("settings", defaults);
return defaults;
}
return settings;
}
Deleting old data
const BdApi = new BdApi("MyPlugin");
// Clear old cache data
BdApi.Data.delete("cache");
// Reset to defaults by deleting and re-saving
BdApi.Data.delete("settings");
BdApi.Data.save("settings", {theme: "dark", enabled: true});
Data storage location
Data is stored in JSON files on disk:
- Windows:
%AppData%/BetterDiscord/plugins/PluginName.config.json
- macOS:
~/Library/Application Support/BetterDiscord/plugins/PluginName.config.json
- Linux:
~/.config/BetterDiscord/plugins/PluginName.config.json
Each plugin gets its own config file, and different keys are stored as properties within that file.