sticky role and auto mute

This commit is contained in:
Erik 2020-08-09 02:22:23 +03:00
parent d6d33ffd63
commit 10ce17431b

View File

@ -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');
}