3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-19 08:05:01 +00:00

implemented role removal mute

This commit is contained in:
roflmaoqwerty 2020-06-20 13:49:33 +10:00
parent 5d56142b27
commit 8bf47ff418
3 changed files with 39 additions and 9 deletions

View file

@ -34,7 +34,7 @@ export class GuildMutes extends BaseGuildRepository {
return mute != null; return mute != null;
} }
async addMute(userId, expiryTime): Promise<Mute> { async addMute(userId, expiryTime, rolesToRestore?: string[]): Promise<Mute> {
const expiresAt = expiryTime const expiresAt = expiryTime
? moment() ? moment()
.add(expiryTime, "ms") .add(expiryTime, "ms")
@ -45,6 +45,7 @@ export class GuildMutes extends BaseGuildRepository {
guild_id: this.guildId, guild_id: this.guildId,
user_id: userId, user_id: userId,
expires_at: expiresAt, expires_at: expiresAt,
roles_to_restore: rolesToRestore ?? [],
}); });
return this.mutes.findOne({ where: result.identifiers[0] }); return this.mutes.findOne({ where: result.identifiers[0] });

View file

@ -16,5 +16,5 @@ export class Mute {
@Column() case_id: number; @Column() case_id: number;
@Column() roles_to_restore: string[]; @Column("simple-array") roles_to_restore: string[];
} }

View file

@ -1,4 +1,4 @@
import { Member, Message, TextChannel, User } from "eris"; import { Member, Message, TextChannel, User, MemberOptions } from "eris";
import { GuildCases } from "../data/GuildCases"; import { GuildCases } from "../data/GuildCases";
import moment from "moment-timezone"; import moment from "moment-timezone";
import { ZeppelinPlugin } from "./ZeppelinPlugin"; import { ZeppelinPlugin } from "./ZeppelinPlugin";
@ -175,6 +175,31 @@ export class MutesPlugin extends ZeppelinPlugin<TConfigSchema> {
const config = this.getMatchingConfig({ member, userId }); const config = this.getMatchingConfig({ member, userId });
if (member) { if (member) {
// remove and store any roles to be removed/restored
const currentUserRoles = member.roles;
const memberOptions: MemberOptions = {};
let rolesToRestore = [];
// remove roles
if (!Array.isArray(config.remove_roles_on_mute)) {
if (config.remove_roles_on_mute) {
memberOptions.roles = [];
await member.edit(memberOptions);
}
} else {
memberOptions.roles = currentUserRoles.filter(x => !(<string[]>config.remove_roles_on_mute).includes(x));
await member.edit(memberOptions);
}
// set roles to be restored
if (!Array.isArray(config.restore_roles_on_mute)) {
if (config.restore_roles_on_mute) {
rolesToRestore = currentUserRoles;
}
} else {
rolesToRestore = currentUserRoles.filter(x => (<string[]>config.restore_roles_on_mute).includes(x));
}
// Apply mute role if it's missing // Apply mute role if it's missing
if (!member.roles.includes(muteRole)) { if (!member.roles.includes(muteRole)) {
await member.addRole(muteRole); await member.addRole(muteRole);
@ -188,11 +213,6 @@ export class MutesPlugin extends ZeppelinPlugin<TConfigSchema> {
await member.edit({ channelID: moveToVoiceChannelId }); await member.edit({ channelID: moveToVoiceChannelId });
} catch (e) {} // tslint:disable-line } catch (e) {} // tslint:disable-line
} }
if (config.remove_roles_on_mute) {
if (config.remove_roles_on_mute === true) {
return;
}
}
// If the user is already muted, update the duration of their existing mute // If the user is already muted, update the duration of their existing mute
const existingMute = await this.mutes.findExistingMuteForUserId(user.id); const existingMute = await this.mutes.findExistingMuteForUserId(user.id);
@ -201,7 +221,7 @@ export class MutesPlugin extends ZeppelinPlugin<TConfigSchema> {
if (existingMute) { if (existingMute) {
await this.mutes.updateExpiryTime(user.id, muteTime); await this.mutes.updateExpiryTime(user.id, muteTime);
} else { } else {
await this.mutes.addMute(user.id, muteTime); await this.mutes.addMute(user.id, muteTime, rolesToRestore);
} }
const template = existingMute const template = existingMute
@ -329,6 +349,15 @@ export class MutesPlugin extends ZeppelinPlugin<TConfigSchema> {
if (member.roles.includes(muteRole)) { if (member.roles.includes(muteRole)) {
await member.removeRole(muteRole); await member.removeRole(muteRole);
} }
// restore roles if appropriate
if (existingMute.roles_to_restore) {
const memberOptions: MemberOptions = {};
memberOptions.roles = Array.from(
new Set([...existingMute.roles_to_restore, ...member.roles.filter(x => x !== muteRole)]),
);
member.edit(memberOptions);
}
} else { } else {
logger.warn( logger.warn(
`Member ${userId} not found in guild ${this.guild.name} (${this.guildId}) when attempting to unmute`, `Member ${userId} not found in guild ${this.guild.name} (${this.guildId}) when attempting to unmute`,