Skip to content

Commit

Permalink
Merge pull request #52 from Coder-Tavi/master
Browse files Browse the repository at this point in the history
fix(manager): Add banManager
  • Loading branch information
phil-flip authored Aug 31, 2022
2 parents 2f4842c + 017c3ca commit db1a729
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions classes/banManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const { Guild, GuildBan } = require('discord.js');

const { Sequelize } = require('sequelize');

module.exports.BanManager = class BanManager {
/**
* @typedef {Object} ManagerData
* @property {Array<GuildBan>} 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;
}

/**
* @description Adds a ban to the manager
* @param {GuildBan} ban Ban to add to the manager
* @returns {Promise<GuildBan>} 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<GuildBan>>} Array of bans added
*/
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));
}));
}

/**
* @description Syncs the manager with the database, adding bans that don't exist in the database
* @returns {Promise<Array<GuildBan>>} 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));
}
};

0 comments on commit db1a729

Please sign in to comment.