Skip to content

Commit

Permalink
Update enmap 5, fix setup, ensure defaultsettings
Browse files Browse the repository at this point in the history
  • Loading branch information
eslachance committed Jun 12, 2019
1 parent 713dea8 commit 02aa698
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -8,7 +8,7 @@ const Discord = require("discord.js");
// We also load the rest of the things we need in this file:
const { promisify } = require("util");
const readdir = promisify(require("fs").readdir);
const Enmap = require("enmap");
const Enmap = require("../enmap");

// This is your client. Some people call it `bot`, some people call it `self`,
// some might call it `cootchie`. Either way, when you see `client.something`,
Expand All @@ -32,7 +32,7 @@ require("./modules/functions.js")(client);
client.commands = new Enmap();
client.aliases = new Enmap();

// Now we integrate the use of Evie's awesome Enhanced Map module, which
// Now we integrate the use of Evie's awesome EnMap module, which
// essentially saves a collection to disk. This is great for per-server configs,
// and makes things extremely easy for this purpose.
client.settings = new Enmap({name: "settings"});
Expand Down
28 changes: 15 additions & 13 deletions modules/functions.js
Expand Up @@ -33,28 +33,30 @@ module.exports = (client) => {
the default settings are used.
*/

// THIS IS HERE BECAUSE SOME PEOPLE DELETE ALL THE GUILD SETTINGS
// And then they're stuck because the default settings are also gone.
// So if you do that, you're resetting your defaults. Congrats.
const defaultSettings = {
"prefix": "~",
"modLogChannel": "mod-log",
"modRole": "Moderator",
"adminRole": "Administrator",
"systemNotice": "true",
"welcomeChannel": "welcome",
"welcomeMessage": "Say hello to {{user}}, everyone! We all need a warm welcome sometimes :D",
"welcomeEnabled": "false"
};

// getSettings merges the client defaults with the guild settings. guild settings in
// enmap should only have *unique* overrides that are different from defaults.
client.getSettings = (guild) => {
client.settings.ensure("default", defaultSettings);
if(!guild) return client.settings.get("default");
const guildConf = client.settings.get(guild.id) || {};
// This "..." thing is the "Spread Operator". It's awesome!
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
return ({...client.settings.get("default"), ...guildConf});
}

// writeSettings overrides, or adds, any configuration item that is different
// than the defaults. This ensures less storage wasted and to detect overrides.
client.writeSettings = (id, newSettings) => {
const defaults = client.settings.get("default");
let settings = client.settings.get(id) || {};
// Using the spread operator again, and lodash's "pickby" function to remove any key
// from the settings that aren't in the defaults (meaning, they don't belong there)
client.settings.set(id, {
..._.pickBy(settings, (v, k) => !_.isNil(defaults[k])),
..._.pickBy(newSettings, (v, k) => !_.isNil(defaults[k]))
});
};

/*
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -24,10 +24,10 @@
},
"homepage": "https://github.com/An-Idiots-Guide/guidebot#readme",
"dependencies": {
"better-sqlite-pool": "^0.2.2",
"better-sqlite3": "^5.4.0",
"chalk": "^2.4.2",
"discord.js": "^11.5.1",
"enmap": "^4.8.7",
"enmap": "^5.0.0",
"inquirer": "^6.3.1",
"moment": "^2.24.0",
"moment-duration-format": "^2.3.2"
Expand Down
2 changes: 1 addition & 1 deletion setup.js
Expand Up @@ -59,7 +59,7 @@ let prompts = [

baseConfig = baseConfig
.replace("{{ownerID}}", answers.ownerID)
.replace("{{token}}", `"${answers.token}"`)
.replace("{{token}}", `"${answers.token}"`);

fs.writeFileSync("./config.js", baseConfig);
console.log("REMEMBER TO NEVER SHARE YOUR TOKEN WITH ANYONE!");
Expand Down

0 comments on commit 02aa698

Please sign in to comment.