From 10ce17431b933541ce6da2397a962f28e015ade0 Mon Sep 17 00:00:00 2001 From: Navy Date: Sun, 9 Aug 2020 02:22:23 +0300 Subject: [PATCH] sticky role and auto mute --- .../components/observers/UtilityHook.js | 73 ++++++++++++++++++- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/structure/client/components/observers/UtilityHook.js b/structure/client/components/observers/UtilityHook.js index 07447ab..ff77ce7 100644 --- a/structure/client/components/observers/UtilityHook.js +++ b/structure/client/components/observers/UtilityHook.js @@ -14,17 +14,84 @@ class UtilityHook extends Observer { this.hooks = [ ['guildMemberAdd', this.welcome.bind(this)], - ['guildMemberAdd', this.autorole.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.enabled) return; + 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'); }