diff --git a/src/ChannelConfig.js b/src/ChannelConfig.js index 279ee7a9f..b7a429e5d 100644 --- a/src/ChannelConfig.js +++ b/src/ChannelConfig.js @@ -11,30 +11,39 @@ class ChannelConfig extends Config { * Constructor - create a channel config * * @param {module:"discord.js".Snowflake} id channel id - * @param {module:"discord.js".Snowflake} guildid * @param {Object} [json] options * @param {Boolean} [json.invites] allow invites * @param {Object} [json.lock] permissions before locking (only affected perms) * @return {ChannelConfig} the config of the channel */ - - constructor(id, guildid, json = {}) { + constructor(id, json = {}) { super(id); - this.guildid = guildid; this.invites = json.invites; this.lock = json.lock || {}; } /** - * Insert this config into the DB - * @param {String} json - * @param {String} escapedTable - * @return {Promise} - * @private + * get the guildID of this channel + * @return {Promise} */ - static async _insert(json, escapedTable) { - return this.database.query(`INSERT INTO ${escapedTable} (config,id,guildid) VALUES (?,?,?)`,[json,this.id, this.guildid]); + async getGuildID() { + try { + /** @type {module:"discord.js".GuildChannel} */ + const channel = await this.constructor.client.channels.fetch(this.id); + return channel.guild.id; + } + catch (e) { + // unknown channel, missing access + if ([10003, 50001].includes(e.code)) { + return null; + } + throw e; + } + } + + async _insert() { + return this.constructor.database.query(`INSERT INTO ${this.constructor.getTableName()} (config,id,guildid) VALUES (?,?,?)`,[this.toJSONString(), this.id, await this.getGuildID()]); } } diff --git a/src/Config.js b/src/Config.js index bd2fb9575..8435c0622 100644 --- a/src/Config.js +++ b/src/Config.js @@ -14,6 +14,12 @@ class Config { */ static database; + /** + * Discord Client + * @type {module:"discord.js".Client} + */ + static client; + /** * Cache for all configs by tableName * @type {{}} @@ -36,9 +42,11 @@ class Config { /** * save database * @param {Database} db + * @param {module:"discord.js".Client} client */ - static init(db) { + static init(db, client) { this.database = db; + this.client = client; } static getCache() { @@ -55,56 +63,55 @@ class Config { return cache; } + /** + * get the escaped table name + * @return {string} + */ + static getTableName() { + return this.database.escapeId(this.tableName); + } + /** * Save to db and cache * @async * @return {Promise<>} */ async save() { - const json = this.toJSONString(); - const escapedTable = this.constructor.database.escapeId(this.constructor.tableName); - - const result = await this.constructor._select(json, escapedTable); + const result = await this._select(); if(result){ - await this.constructor._update(json, escapedTable); + await this._update(); } else { - await this.constructor._insert(json, escapedTable) + await this._insert() this.constructor.getCache().set(this.id, this); } } /** - * Get a config from the DB - * @param {String} json - * @param {String} escapedTable + * Get this config from the DB * @return {Promise} * @private */ - static async _select(json, escapedTable) { - return this.database.query(`SELECT * FROM ${escapedTable} WHERE id = ?`,[this.id]); + async _select() { + return this.constructor.database.query(`SELECT id, config FROM ${this.constructor.getTableName()} WHERE id = ?`,[this.id]); } /** - * Update a config in the DB - * @param {String} json - * @param {String} escapedTable + * Update this config in the DB * @return {Promise} * @private */ - static async _update(json, escapedTable) { - return this.database.query(`UPDATE ${escapedTable} SET config = ? WHERE id = ?`,[json,this.id]); + async _update() { + return this.constructor.database.query(`UPDATE ${this.constructor.getTableName()} SET config = ? WHERE id = ?`,[this.toJSONString(),this.id]); } /** * Insert a config into the DB - * @param {String} json - * @param {String} escapedTable * @return {Promise} * @private */ - static async _insert(json, escapedTable) { - return this.database.query(`INSERT INTO ${escapedTable} (config,id) VALUES (?,?)`,[json,this.id]); + async _insert() { + return this.constructor.database.query(`INSERT INTO ${this.constructor.getTableName()} (config,id) VALUES (?,?)`,[this.toJSONString(), this.id]); } /** @@ -116,7 +123,7 @@ class Config { static async get(id) { if (!this.getCache().has(id)) { - let result = await this.database.query(`SELECT * FROM ${this.database.escapeId(this.tableName)} WHERE id = ?`, id); + let result = await this.database.query(`SELECT id, config FROM ${this.getTableName()} WHERE id = ?`, [id]); if(!result) return new this(id); this.getCache().set(result.id, new this(result.id, JSON.parse(result.config))); setTimeout(() => { diff --git a/src/util.js b/src/util.js index a9d3dbd65..cc08bec3f 100644 --- a/src/util.js +++ b/src/util.js @@ -33,7 +33,7 @@ util.init = (db, client) => { database = db; bot = client; ChatTriggeredFeature.init(db); - Config.init(db); + Config.init(db, client); RateLimiter.init(db); }; diff --git a/update/0.1.1.js b/update/0.1.1.js index 2829b8874..a64bee0e8 100644 --- a/update/0.1.1.js +++ b/update/0.1.1.js @@ -54,13 +54,11 @@ async function updateGuildID(channelID) { await database.query('UPDATE channels SET guildid = ? WHERE id = ?',[channel.guild.id, channelID]); } catch (e) { - if (e.code === 10003) { + // unknown channel, missing access + if ([10003, 50001].includes(e.code)) { await database.query('DELETE FROM channels WHERE id = ?',[channelID]); return false; } - if (e.code === 50001) { - return false; - } throw e; } return true;