This commit is contained in:
Daniel Odendahl Jr
2017-06-02 00:55:38 +00:00
parent d1131d6092
commit a4c7ca11c8
2 changed files with 69 additions and 69 deletions
+1 -1
View File
@@ -36,7 +36,7 @@
"dependencies": { "dependencies": {
"bufferutil": "^3.0.1", "bufferutil": "^3.0.1",
"canvas": "automattic/node-canvas", "canvas": "automattic/node-canvas",
"cheerio": "^1.0.0", "cheerio": "^1.0.0-rc.1",
"cleverio": "dragonfire535/cleverio", "cleverio": "dragonfire535/cleverio",
"custom-translate": "dragonfire535/custom-translate", "custom-translate": "dragonfire535/custom-translate",
"discord.js": "hydrabolt/discord.js", "discord.js": "hydrabolt/discord.js",
+68 -68
View File
@@ -6,50 +6,50 @@ const Sequelize = require('sequelize');
* @extends {SettingProvider} * @extends {SettingProvider}
*/ */
class SequelizeProvider extends SettingProvider { class SequelizeProvider extends SettingProvider {
/** /**
* @external PostgreSQLDatabase * @external PostgreSQLDatabase
* @see {@link https://www.npmjs.com/package/sequelize} * @see {@link https://www.npmjs.com/package/sequelize}
*/ */
/** /**
* @param {SQLDatabase} db - Database for the provider * @param {SQLDatabase} db - Database for the provider
*/ */
constructor(db) { constructor(db) {
super(); super();
/** /**
* Database that will be used for storing/retrieving settings * Database that will be used for storing/retrieving settings
* @type {SQLDatabase} * @type {SQLDatabase}
*/ */
this.db = db; this.db = db;
/** /**
* Client that the provider is for (set once the client is ready, after using {@link CommandoClient#setProvider}) * Client that the provider is for (set once the client is ready)
* @name SequelizeProvider#client * @name SequelizeProvider#client
* @type {CommandoClient} * @type {CommandoClient}
* @readonly * @readonly
*/ */
Object.defineProperty(this, 'client', { value: null, writable: true }); Object.defineProperty(this, 'client', { value: null, writable: true });
/** /**
* Settings cached in memory, mapped by guild ID (or 'global') * Settings cached in memory, mapped by guild ID (or 'global')
* @type {Map} * @type {Map}
* @private * @private
*/ */
this.settings = new Map(); this.settings = new Map();
/** /**
* Listeners on the Client, mapped by the event name * Listeners on the Client, mapped by the event name
* @type {Map} * @type {Map}
* @private * @private
*/ */
this.listeners = new Map(); this.listeners = new Map();
/** /**
* Sequelize Model Object * Sequelize Model Object
* @type {SequelizeModel} * @type {SequelizeModel}
* @private * @private
*/ */
this.model = this.db.define('settings', { this.model = this.db.define('settings', {
guild: { guild: {
type: Sequelize.STRING, type: Sequelize.STRING,
@@ -60,17 +60,17 @@ class SequelizeProvider extends SettingProvider {
settings: { type: Sequelize.TEXT } settings: { type: Sequelize.TEXT }
}); });
/** /**
* @external SequelizeModel * @external SequelizeModel
* @see {@link http://docs.sequelizejs.com/en/latest/api/model/} * @see {@link http://docs.sequelizejs.com/en/latest/api/model/}
*/ */
} }
async init(client) { async init(client) {
this.client = client; this.client = client;
await this.db.sync(); await this.db.sync();
// Load all settings // Load all settings
const rows = await this.model.findAll(); const rows = await this.model.findAll();
for (const row of rows) { for (const row of rows) {
let settings; let settings;
@@ -134,8 +134,8 @@ class SequelizeProvider extends SettingProvider {
settings[key] = val; settings[key] = val;
await this.model.upsert( await this.model.upsert(
{ guild: guild !== 'global' ? guild : '0', settings: JSON.stringify(settings) }, { guild: guild !== 'global' ? guild : '0', settings: JSON.stringify(settings) },
{ where: { guild: guild !== 'global' ? guild : '0' } } { where: { guild: guild !== 'global' ? guild : '0' } }
); );
if (guild === 'global') this.updateOtherShards(key, val); if (guild === 'global') this.updateOtherShards(key, val);
return val; return val;
@@ -149,8 +149,8 @@ class SequelizeProvider extends SettingProvider {
const val = settings[key]; const val = settings[key];
settings[key] = undefined; settings[key] = undefined;
await this.model.upsert( await this.model.upsert(
{ guild: guild !== 'global' ? guild : '0', settings: JSON.stringify(settings) }, { guild: guild !== 'global' ? guild : '0', settings: JSON.stringify(settings) },
{ where: { guild: guild !== 'global' ? guild : '0' } } { where: { guild: guild !== 'global' ? guild : '0' } }
); );
if (guild === 'global') this.updateOtherShards(key, undefined); if (guild === 'global') this.updateOtherShards(key, undefined);
return val; return val;
@@ -163,12 +163,12 @@ class SequelizeProvider extends SettingProvider {
await this.model.destroy({ where: { guild: guild !== 'global' ? guild : '0' } }); await this.model.destroy({ where: { guild: guild !== 'global' ? guild : '0' } });
} }
/** /**
* Loads all settings for a guild * Loads all settings for a guild
* @param {string} guild - Guild ID to load the settings of (or 'global') * @param {string} guild - Guild ID to load the settings of (or 'global')
* @param {Object} settings - Settings to load * @param {Object} settings - Settings to load
* @private * @private
*/ */
setupGuild(guild, settings) { setupGuild(guild, settings) {
if (typeof guild !== 'string') throw new TypeError('The guild must be a guild ID or "global".'); if (typeof guild !== 'string') throw new TypeError('The guild must be a guild ID or "global".');
guild = this.client.guilds.get(guild) || null; guild = this.client.guilds.get(guild) || null;
@@ -179,18 +179,18 @@ class SequelizeProvider extends SettingProvider {
else this.client._commandPrefix = settings.prefix; else this.client._commandPrefix = settings.prefix;
} }
// Load all command/group statuses // Load all command/group statuses
for (const command of this.client.registry.commands.values()) this.setupGuildCommand(guild, command, settings); for (const command of this.client.registry.commands.values()) this.setupGuildCommand(guild, command, settings);
for (const group of this.client.registry.groups.values()) this.setupGuildGroup(guild, group, settings); for (const group of this.client.registry.groups.values()) this.setupGuildGroup(guild, group, settings);
} }
/** /**
* Sets up a command's status in a guild from the guild's settings * Sets up a command's status in a guild from the guild's settings
* @param {?Guild} guild - Guild to set the status in * @param {?Guild} guild - Guild to set the status in
* @param {Command} command - Command to set the status of * @param {Command} command - Command to set the status of
* @param {Object} settings - Settings of the guild * @param {Object} settings - Settings of the guild
* @private * @private
*/ */
setupGuildCommand(guild, command, settings) { setupGuildCommand(guild, command, settings) {
if (typeof settings[`cmd-${command.name}`] === 'undefined') return; if (typeof settings[`cmd-${command.name}`] === 'undefined') return;
if (guild) { if (guild) {
@@ -201,13 +201,13 @@ class SequelizeProvider extends SettingProvider {
} }
} }
/** /**
* Sets up a group's status in a guild from the guild's settings * Sets up a group's status in a guild from the guild's settings
* @param {?Guild} guild - Guild to set the status in * @param {?Guild} guild - Guild to set the status in
* @param {CommandGroup} group - Group to set the status of * @param {CommandGroup} group - Group to set the status of
* @param {Object} settings - Settings of the guild * @param {Object} settings - Settings of the guild
* @private * @private
*/ */
setupGuildGroup(guild, group, settings) { setupGuildGroup(guild, group, settings) {
if (typeof settings[`grp-${group.id}`] === 'undefined') return; if (typeof settings[`grp-${group.id}`] === 'undefined') return;
if (guild) { if (guild) {
@@ -218,12 +218,12 @@ class SequelizeProvider extends SettingProvider {
} }
} }
/** /**
* Updates a global setting on all other shards if using the {@link ShardingManager}. * Updates a global setting on all other shards if using the {@link ShardingManager}.
* @param {string} key - Key of the setting to update * @param {string} key - Key of the setting to update
* @param {*} val - Value of the setting * @param {*} val - Value of the setting
* @private * @private
*/ */
updateOtherShards(key, val) { updateOtherShards(key, val) {
if (!this.client.shard) return; if (!this.client.shard) return;
key = JSON.stringify(key); key = JSON.stringify(key);