const { Observer } = require('../../../interfaces/'); const { Util } = require('../../../../util'); const CONSTANTS = {}; class UtilityHook extends Observer { constructor(client) { super(client, { name: 'utility', priority: 3 }); this.hooks = [ ['guildMemberAdd', this.welcome.bind(this)], ['guildMemberAdd', this.autorole.bind(this)], ['guildMemberAdd', this.automute.bind(this)], ['guildMemberAdd', this.stickyRole.bind(this)], ['guildMemberRemove', this.storeRoles.bind(this)] ]; } async storeRoles(member) { const { guild } = member; const settings = await guild.settings(); const setting = settings.stickyrole; if (!setting.roles.length || guild.premium < 1) return; const { roles } = member; const storeThese = roles.cache.filter((role) => setting.roles.includes(role.id) && !role.equals(guild.roles.everyone)).map((role) => role.id); if (!storeThese.length) return; await this.client.storageManager.mongodb.role_cache.updateOne({ member: member.id, guild: guild.id }, { roles: storeThese, timestamp: Math.floor(Date.now()/1000) }); } async automute(member) { const { guild } = member; const settings = await guild.settings(); const setting = settings.mute; if (!guild.me.hasPermission('MANAGE_ROLES')) return; const infraction = await this.client.storageManager.mongodb.infractions.findOne({ duration: { $gt: 0 }, guild: guild.id, target: member.id, type: 'MUTE', callbacked: false }); if (!infraction || infraction.resolved) return; if (infraction.data.muteType === 0) await member.roles.add(setting.role, 'automute upon rejoin, type 0'); else if (infraction.data.muteType === 1) { await Util.waitFor(5 * 1000); //wait 5 seconds in case other bots add roles that would break the mute const roles = member.roles.cache; const managed = roles.find((r) => r.managed); const remove = roles.filter((r) => !r.managed); //Have to do this bs for managed roles -.- if (managed) { await member.roles.add(setting.role, 'automute upon rejoin, type 1'); await member.roles.remove(remove, 'removing excess roles for type 1 mute'); } else await member.roles.set(setting.role, 'automute upon join, type 1'); } else if (infraction.data.muteType === 2) { await Util.waitFor(5 * 1000); //wait 5 seconds in case other bots add roles that would break the mute const roles = member.roles.cache; const remove = roles.filter((r) => !r.managed); //Have to do this bs for managed roles -.- await member.roles.remove(remove, 'automute upon join, type 1'); } } async stickyRole(member) { const { guild } = member; const settings = await guild.settings(); const setting = settings.stickyrole; if (!setting.roles.length || guild.premium < 1) return; if (!guild.me.hasPermission('MANAGE_ROLES')) return; const data = await this.client.storageManager.mongodb.role_cache.findOne({ guild: guild.id, member: member.id }); if (!data) return; await member.roles.add(data.roles, 'Adding stickyroles'); } async autorole(member) { const { guild } = member; const settings = await guild.settings(); const setting = settings.autorole; if (!setting.enabled) return; if (!guild.me.hasPermission('MANAGE_ROLES')) return; const _roles = await guild.resolveRoles(setting.roles); const roles = _roles.map((r) => r.id); await member.roles.add(roles, 'Adding autoroles').catch(this.client.logger.error); } async welcome(member) { const { guild, user } = member; const settings = await guild.settings(); const setting = settings.welcomer; if (!setting.enabled) return; const channel = await user.createDM(); const message = this._replaceTags(setting.message, member); await channel.send(message).catch(); } _replaceTags(text, member) { const { user, guild } = member; return text .replace(/\{mention\}/gu, `<@${member.id}>`) .replace(/\{tag\}/gu, Util.escapeMarkdown(user.tag)) .replace(/\{user\}/gu, Util.escapeMarkdown(user.username)) .replace(/\{serversize\}/gu, guild.memberCount) .replace(/\{servername\}/gu, guild.name) .replace(/\{accage\}/gu, this.client.resolver.timeAgo(Date.now()/1000 - user.createdTimestamp/1000)) //.replace(/a/, '1') .replace(/\{id\}/gu, user.id) .trim(); } } module.exports = UtilityHook;