From 02966b323653165635cb47d9b28c8eed7999a073 Mon Sep 17 00:00:00 2001 From: Tavi <66774833+Coder-Tavi@users.noreply.github.com> Date: Wed, 31 Aug 2022 00:51:40 -0700 Subject: [PATCH 1/3] fix(manager): Add banManager --- classes/banManager.js | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 classes/banManager.js diff --git a/classes/banManager.js b/classes/banManager.js new file mode 100644 index 0000000..fdeaf25 --- /dev/null +++ b/classes/banManager.js @@ -0,0 +1,68 @@ +const { Guild, GuildBan } = require('discord.js'); + +const { Sequelize } = require('sequelize'); + +module.exports.BanManager = class BanManager { + /** + * @typedef {Object} ManagerData + * @property {Array} bans Array of bans + * @property {Sequelize} sequelize Sequelize instance + */ + /** + * @param {ManagerData} data Data to initialize the manager with + */ + constructor(data) { + if (Array.isArray(data.bans) === false) throw new SyntaxError('Invalid bans array'); + this.bans = data.bans; + if (!data.sequelize || data.sequelize instanceof Sequelize === false || !data.sequelize.models.Ban) throw new SyntaxError('Invalid Sequelize instance'); + this.sequelize = data.sequelize; + } + + /** + * @private + * @description Adds a ban to the manager + * @param {GuildBan} ban Ban to add to the manager + * @returns {Promise} Promise of the ban added + */ + addBan(ban) { + if (ban instanceof GuildBan === false) return Promise.reject(new SyntaxError('Invalid Ban instance')); + return Promise.resolve(this.bans.push(ban)); + } + + /** + * @description Adds multiple bans to the manager + * @param {Guild} guild Guild to add bans from + * @returns {Promise>} Array of bans added + */ + addGuildBans(guild) { + if (guild instanceof Guild === false) return Promise.reject(new SyntaxError('Invalid Guild instance')); + return Promise.resolve(guild.bans.fetch().then((bans) => { + if (bans instanceof Collection === false) return this.addBan(bans.first()); + return Array.from(bans.values()).forEach((ban) => this.addBan(ban)); + })); + } + + /** + * @description Syncs the manager with the database, adding bans that don't exist in the database + * @returns {Promise>} Array of bans added + */ + sync() { + this.bans.forEach((ban) => { + if (ban.user.bot && ban.user.fetchFlags().then((f) => f.has('VERIFIED_BOT'))) return; // Ignore verified bots + this.sequelize.models.Ban.findOrCreate({ + where: { + serverID: ban.guild.id, + userID: ban.user.id, + reason: ban.reason, + }, + defaults: { + // userID: ban.user.id, + // serverID: ban.guild.id, + userTag: ban.user.tag, + // reason: ban.reason, + }, + }); + }) + .then(() => Promise.resolve(this.bans), (err) => Promise.reject(err)); + } +}; From c1a1e506d8d1d1d17de01983c56fc61337413c6c Mon Sep 17 00:00:00 2001 From: Tavi <66774833+Coder-Tavi@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:03:40 -0700 Subject: [PATCH 2/3] fix(manager): Add permission check --- classes/banManager.js | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/banManager.js b/classes/banManager.js index fdeaf25..99daa9e 100644 --- a/classes/banManager.js +++ b/classes/banManager.js @@ -36,6 +36,7 @@ module.exports.BanManager = class BanManager { */ addGuildBans(guild) { if (guild instanceof Guild === false) return Promise.reject(new SyntaxError('Invalid Guild instance')); + if (!guild.me.permissions.has('BAN_MEMBERS')) return Promise.reject(new SyntaxError('Missing BAN_MEMBERS in guild')); return Promise.resolve(guild.bans.fetch().then((bans) => { if (bans instanceof Collection === false) return this.addBan(bans.first()); return Array.from(bans.values()).forEach((ban) => this.addBan(ban)); From 017c3cae19941be2511f5e831d37f1a6b3311ecb Mon Sep 17 00:00:00 2001 From: Tavi <66774833+Coder-Tavi@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:10:02 -0700 Subject: [PATCH 3/3] fix(manager): Unprivate addBan() --- classes/banManager.js | 1 - 1 file changed, 1 deletion(-) diff --git a/classes/banManager.js b/classes/banManager.js index 99daa9e..f3e943f 100644 --- a/classes/banManager.js +++ b/classes/banManager.js @@ -19,7 +19,6 @@ module.exports.BanManager = class BanManager { } /** - * @private * @description Adds a ban to the manager * @param {GuildBan} ban Ban to add to the manager * @returns {Promise} Promise of the ban added