Persistence

Persistence

Read or write persistent data that is persisted even if the process is killed. The persistence is a key-value store backed by fpersist. You can store values for any key, but there are convenience methods provided for storing data attached to a particular user or server, or in a global store. Persistence can be accessed via Monochrome#getPersistence. For examples of using persistence to store and retrieve persistent data, see the demo addQuote command and demo getRandomQuote command.

Methods

(async) deleteData(key)

Description:
  • Delete the value associated with a specified key. If the key doesn't exist, nothing happens and no error is thrown.
Source:
Parameters:
Name Type Description
key string

(async) editData(key, editFunction)

Description:
  • Edit the data associated with a key. This function is atomic in the sense that no one else can be editing the value for the same key at the same time.
Source:
Example
await persistence.editData('some_key', (data) => {
  data.randomNumber = Math.random() * 100;

  if (!data.numberOfTimesEdited) {
    data.numberOfTimesEdited = 0;
  }

  data.numberOfTimesEdited += 1;

  return data;
});
Parameters:
Name Type Description
key string
editFunction Persistence~editFunction The function that performs the edit.

(async) editDataForServer(serverId, editFunction)

Description:
  • Edit the data associated with a server. This function is atomic in the sense that no one else can be editing the value for the same key at the same time.
Source:
Example
const serverId = '123456789';
await editDataForServer(serverId, (data) => {
  data.favoriteUserInServer = 'nobody';
  return data;
});
Parameters:
Name Type Description
serverId string
editFunction Persistence~editFunction The function that performs the edit.

(async) editDataForUser(userId, editFunction)

Description:
  • Edit the data associated with a user. This function is atomic in the sense that no one else can be editing the value for the same key at the same time.
Source:
Example
const userId = '123456789';
await editDataForUser(userId, (data) => {
  data.userIsWorthMyTime = false;
  return data;
});
Parameters:
Name Type Description
userId string
editFunction Persistence~editFunction The function that performs the edit.

(async) editGlobalData(editFunction)

Description:
  • Edit the data global data. This function is atomic in the sense that no one else can be editing the value for the same key at the same time.
Source:
Example
await editGlobalData((data) => {
  if (!data.scoreboard) {
    data.scoreboard = {};
  }

  data.scoreboard['John Wick'] = 100;
  return data;
});
Parameters:
Name Type Description
editFunction Persistence~editFunction The function that performs the edit.

(async) editPrefixesForServerId(serverId, prefixes)

Description:
  • Edit the prefixes associated with a server.
Source:
Example
const serverId = '123456789';
const newPrefixes = ['!', '@', '&!'];
await editPrefixesForServer(serverId, newPrefixes);
Parameters:
Name Type Description
serverId string
prefixes Array.<string> The new prefixes for the server.

(async) getData(key) → {Object}

Description:
  • Get the value associated with the specified key. If no such value exists, an empty object {} is returned.
Source:
Example
const data = await persistence.getData('some_key');
console.log(JSON.stringify(data));
Parameters:
Name Type Description
key string
Returns:
The value associated with the specified key.
Type
Object

(async) getDataForServer(serverId) → {Object}

Description:
  • Get the data associated with a server. If no such data exists, an empty object {} is returned.
Source:
Example
const serverId = '123456789';
const data = await persistence.getDataForServer(serverId);
console.log(JSON.stringify(data));
Parameters:
Name Type Description
serverId string
Returns:
The value associated with the specified serverId
Type
Object

(async) getDataForUser(userId) → {Object}

Description:
  • Get the data associated with a user. If no such data exists, an empty object {} is returned.
Source:
Example
const userId = '123456789';
const data = await persistence.getDataForUser(userId);
console.log(JSON.stringify(data));
Parameters:
Name Type Description
userId string
Returns:
The value associated with the specified userId
Type
Object

(async) getGlobalData() → {Object}

Description:
  • Get the global data. If no such data exists, an empty object {} is returned.
Source:
Example
const data = await persistence.getGlobalData();
console.log(JSON.stringify(data));
Returns:
The global data.
Type
Object

getPrefixesForMessage(msg) → {Array.<string>}

Description:
  • Get the prefixes for the location where msg was sent. This method is synchronous, in order to avoid the overhead of using promises. If called very soon after the bot starts, it might not return the correct prefixes. It might return the default prefixes even though the server has custom prefixes.
Source:
Parameters:
Name Type Description
msg external:"Eris.Message"
Returns:
Type
Array.<string>

getPrefixesForServer(serverId) → {Array.<string>}

Description:
  • Get the command prefixes associated with a server ID. This method is synchronous, in order to avoid the overhead of using promises. If called very soon after the bot starts, it might not return the correct prefixes. It might return the default prefixes even though the server has custom prefixes.
Source:
Example
const serverId = '123456789';
const prefixes = persistence.getPrefixesForServer(serverId);
const firstPrefix = prefixes[0];
const numberOfPrefixes = prefixes.length;
Parameters:
Name Type Description
serverId string
Returns:
Type
Array.<string>

getPrimaryPrefixForMessage(msg) → {string}

Description:
  • Get the primary prefix for the location where msg was sent. This method is synchronous, in order to avoid the overhead of using promises. If called very soon after the bot starts, it might not return the correct prefixes. It might return the default prefixes even though the server has custom prefixes.
Source:
Parameters:
Name Type Description
msg external:"Eris.Message"
Returns:
Type
string

(async) resetPrefixesForServerId(serverId)

Description:
  • Reset the prefixes associated with a server.
Source:
Parameters:
Name Type Description
serverId string

(async) stop()

Description:
  • Tell persistence that it should stop allowing write operations.
Source:

Type Definitions

editFunction(data) → {Object}

Source:
Parameters:
Name Type Description
data Object The current data associated with the key. If there is none, an empty object {} is given. This data can be manipulated and then returned to persist it.
Returns:
The new data to associate with the key.
Type
Object