// the default serializer
const serializer = (data, key) => {
return data;
};// the default deserializer
const deserializer = (data, key) => {
return data;
};// Imagine the client and stuff is already defined.
// The function that runs when storing data
const serializeData: data => {
return {
...data,
// stores the guild as ID
guild: guild.id,
// stores the user as ID
user: user.id,
}
};
// This one runs when loading.
const deserializeData: data => {
return {
...data,
// gets the guild itself from the cache from its ID
guild: client.guilds.cache.get(data.guild),
// Same with the user!
user: client.users.cache.get(data.user),
}
};
// Default Settings can no longer store defaults for roles and channels.
const defaultSettings = {
prefix: "!",
modLogChannel: null,
modRole: null,
adminRole: null,
welcomeChannel: null,
welcomeMessage: "Say hello to {{user}}, everyone!"
}
// Our enmap has shiny new options here!
client.settings = new Enmap({
name: "settings",
cloneLevel: 'deep',
serializer: serializeData,
deserializer: deserializeData,
// Might as well autoensure, eh?
autoEnsure: defaultSettings,
});
// Store some data, obviously needs to be run in the right place:
client.settings.set(message.guild.id,
message.mentions.channels.first(),
'welcomeChannel'
);
client.settings.set(message.guild.id,
message.mentions.roles.first(),
'adminRole'
);
// GET the data after
const welcomeChannel = client.settings.get(message.guild.id, 'welcomeChannel');
welcomeChannel.send("This works without having to find or get the channel!");enmap.fetch([array, of, keys]) will fetch each key in the requested array, and return an array of [key, value] pairs for each fetched value. const Enmap = require("enmap");
const points = new Enmap({
name: "points",
fetchAll: false,
autoFetch: true
});<Enmap>.set(key, value);myEnmap.set('boolean', true);
myEnmap.set('integer', 42);
myEnmap.set('someFloat', 73.2345871);
myEnmap.set("Test2", "test2");const floatValue = myEnmap.get('someFloat');
const test = myEnmap.get('Test2');
// you can even use booleans in conditions:
if(myEnmap.get('boolean')) {
// boolean is true!
}myEnmap.delete("integer");
myEnmap.delete("boolean");myEnmap.push("simpleArray", 6);
// now [1,2,3,4,5,6]
myEnmap.push("arrInObj", "Robby", "aliases");
// now ["Bobby", "Robert", "Robby"]myEnmap.remove("simpleArray", 2);
// now [1,3,4,5,6]
myEnmap.remove("arrInObject", "Bobby", "aliases");
// now ["Robert", "Robby"]
myEnmap.remove('objectarray', (value) => value.e === 5);
// value is now [{ a: 1, b: 2, c: 3 }]const Enmap = require("enmap");
// Normal enmap with default options
const myEnmap = new Enmap({name: "points"});
// non-cached, auto-fetch enmap:
const otherEnmap = new Enmap({
name: "settings",
autoFetch: true,
fetchAll: false
});What is Paths in Enmap, how to use them, what is their syntax?
const myStructure = {
first: "blah",
second: "foo",
changeme: "initial",
isCool: false
sub: {
yay: true,
thing: "amagig"
}
}myObject.sub.values.are.coolmyObject["sub"]["values"]["are"]["cool"]myEnmap.set("someObject", myStructure);
// Or directly the object
myEnmap.set("someObject", {first: "blah", ...});
// Works with arrays, too!
myEnmap.set("someArray", ["one", "two", "three"]);const second = myEnmap.get("someObject", "second");
// returns "foo"
const thing = myEnmap.get("someObject", "sub.yay");
// returns true
// The path can be dynamic, too:
const propToGet = "thing";
const blah = myEnmap.get("someObject", `sub.${propToGet}`);myEnmap.has("someObject", "sub.thing"); // returns true
myEnmap.has("someObject", "heck"); // returns false.// Set an object property
myEnmap.set("someObject", "newThing", "sub.blah");
// Set an array property
myEnmap.set("someArray", "four", 3);const myObject = {
a: "foo",
b: true,
c: {
but: "who",
are: "you?",
and: ["are you", "you?"],
},
sub: { values: { are: { "cool" } } },
};myEnmap.set("myObject", {
a: "foo",
b: true,
c: {
but: "who",
are: "you?",
and: ["are you", "you?"],
},
sub: { values: { are: { "cool" } } },
});const Discord = require("discord.js");
const client = new Discord.Client();
const Enmap = require("enmap");
// this is the important bit
client.settings = new Enmap({ name: "settings" });
client.tags = new Enmap({ name: "tags" });
// your normal events here
client.on("message", message => {
const guildSettings = client.settings.get(message.guild.id);
// works here
});
client.login(token);const Enmap = require("enmap");
module.exports = {
settings: new Enmap({
name: "settings",
autoFetch: true,
fetchAll: false
}),
users: new Enmap("users"),
tags: new Emmap({ name : "tags" })
}const db = require("./db.js");
console.log(db.settings.size);
db.tags.set("blah", {
guild: "1234",
author: "4231",
name: "blah",
content: "I'm bored, mommy!"
});const guildTags = db.tags.find(tag => tag.guild === message.guild.id);const Enmap = require("enmap");
module.exports = {
settings: new Enmap({
name: "settings",
autoFetch: true,
fetchAll: false
}),
users: new Enmap("users"),
tags: new Emmap({ name : "tags" }),
getTags: (guild) => {
return this.tags.find(tag => tag.guild === message.guild.id);
}
}// Assuming
points.set("number", 42);
points.set("numberInObject", {sub: { anInt: 5 }});
points.math("number", "/", 2); // 21
points.math("number", "add", 5); // 26
points.math("number", "modulo", 3); // 2
points.math("numberInObject", "+", 10, "sub.anInt");// Assuming
points.set("number", 42);
points.set("numberInObject", {sub: { anInt: 5 }});
points.inc("number"); // 43
points.inc("numberInObject", "sub.anInt"); // {sub: { anInt: 6 }}// Assuming
points.set("number", 42);
points.set("numberInObject", {sub: { anInt: 5 }});
points.dec("number"); // 41
points.dec("numberInObject", "sub.anInt"); // {sub: { anInt: 4 }}