Skip to content

Commit

Permalink
actually store guildid in database
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Mar 12, 2021
1 parent ea5fdfd commit 0c8863c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
31 changes: 20 additions & 11 deletions src/ChannelConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>}
* @private
* get the guildID of this channel
* @return {Promise<null|string>}
*/
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()]);
}
}

Expand Down
51 changes: 29 additions & 22 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Config {
*/
static database;

/**
* Discord Client
* @type {module:"discord.js".Client}
*/
static client;

/**
* Cache for all configs by tableName
* @type {{}}
Expand All @@ -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() {
Expand All @@ -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<void>}
* @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<void>}
* @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<void>}
* @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]);
}

/**
Expand All @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
6 changes: 2 additions & 4 deletions update/0.1.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0c8863c

Please sign in to comment.