Skip to content

Commit

Permalink
Merge branch 'master' into automod-slowmode-use-contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx authored Dec 28, 2023
2 parents d278663 + 8860d4f commit e8e9212
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
4 changes: 4 additions & 0 deletions backend/src/RecoverablePluginError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export enum ERRORS {
INVALID_USER,
INVALID_MUTE_ROLE_ID,
MUTE_ROLE_ABOVE_ZEP,
USER_ABOVE_ZEP,
USER_NOT_MODERATABLE,
}

export const RECOVERABLE_PLUGIN_ERROR_MESSAGES = {
Expand All @@ -20,6 +22,8 @@ export const RECOVERABLE_PLUGIN_ERROR_MESSAGES = {
[ERRORS.INVALID_USER]: "Invalid user",
[ERRORS.INVALID_MUTE_ROLE_ID]: "Specified mute role is not valid",
[ERRORS.MUTE_ROLE_ABOVE_ZEP]: "Specified mute role is above Zeppelin in the role hierarchy",
[ERRORS.USER_ABOVE_ZEP]: "Cannot mute user, specified user is above Zeppelin in the role hierarchy",
[ERRORS.USER_NOT_MODERATABLE]: "Cannot mute user, specified user is not moderatable",
};

export class RecoverablePluginError extends Error {
Expand Down
12 changes: 10 additions & 2 deletions backend/src/plugins/Mutes/events/ReapplyActiveMuteOnJoinEvt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import moment from "moment-timezone";
import { MuteTypes } from "../../../data/MuteTypes";
import { noop } from "../../../utils.js";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin";
import { getTimeoutExpiryTime } from "../functions/getTimeoutExpiryTime";
Expand All @@ -12,6 +13,7 @@ export const ReapplyActiveMuteOnJoinEvt = mutesEvt({
event: "guildMemberAdd",
async listener({ pluginData, args: { member } }) {
const mute = await pluginData.state.mutes.findExistingMuteForUserId(member.id);
const logs = pluginData.getPlugin(LogsPlugin);
if (!mute) {
return;
}
Expand All @@ -25,11 +27,17 @@ export const ReapplyActiveMuteOnJoinEvt = mutesEvt({
if (!member.isCommunicationDisabled()) {
const expiresAt = mute.expires_at ? moment.utc(mute.expires_at).valueOf() : null;
const timeoutExpiresAt = getTimeoutExpiryTime(expiresAt);
await member.disableCommunicationUntil(timeoutExpiresAt);
if (member.moderatable) {
await member.disableCommunicationUntil(timeoutExpiresAt).catch(noop);
} else {
logs.logBotAlert({
body: `Cannot mute user, specified user is not moderatable`,
});
}
}
}

pluginData.getPlugin(LogsPlugin).logMemberMuteRejoin({
logs.logMemberMuteRejoin({
member,
});
},
Expand Down
22 changes: 20 additions & 2 deletions backend/src/plugins/Mutes/functions/muteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { TemplateSafeValueContainer, renderTemplate } from "../../../templateFor
import {
UserNotificationMethod,
UserNotificationResult,
noop,
notifyUser,
resolveMember,
resolveUser,
Expand Down Expand Up @@ -108,7 +109,7 @@ export async function muteUser(
if (zepRoles.size === 0 || !zepRoles.some((zepRole) => zepRole.position > actualMuteRole.position)) {
lock.unlock();
logs.logBotAlert({
body: `Cannot mute users, specified mute role is above Zeppelin in the role hierarchy`,
body: `Cannot mute user, specified mute role is above Zeppelin in the role hierarchy`,
});
throw new RecoverablePluginError(ERRORS.MUTE_ROLE_ABOVE_ZEP, pluginData.guild);
}
Expand All @@ -117,7 +118,24 @@ export async function muteUser(
pluginData.getPlugin(RoleManagerPlugin).addPriorityRole(member.id, muteRole!);
}
} else {
await member.disableCommunicationUntil(timeoutUntil);
if (!member.manageable) {
lock.unlock();
logs.logBotAlert({
body: `Cannot mute user, specified user is above Zeppelin in the role hierarchy`,
});
throw new RecoverablePluginError(ERRORS.USER_ABOVE_ZEP, pluginData.guild);
}

if (!member.moderatable) {
// redundant safety, since canActOn already checks this
lock.unlock();
logs.logBotAlert({
body: `Cannot mute user, specified user is not moderatable`,
});
throw new RecoverablePluginError(ERRORS.USER_NOT_MODERATABLE, pluginData.guild);
}

await member.disableCommunicationUntil(timeoutUntil).catch(noop);
}

// If enabled, move the user to the mute voice channel (e.g. afk - just to apply the voice perms from the mute role)
Expand Down
12 changes: 10 additions & 2 deletions backend/src/plugins/Mutes/functions/renewTimeoutMute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import { MAX_TIMEOUT_DURATION } from "../../../data/Mutes";
import { Mute } from "../../../data/entities/Mute";
import { DBDateFormat, resolveMember } from "../../../utils";
import { DBDateFormat, noop, resolveMember } from "../../../utils";
import { LogsPlugin } from "../../Logs/LogsPlugin.js";
import { MutesPluginType } from "../types";

export async function renewTimeoutMute(pluginData: GuildPluginData<MutesPluginType>, mute: Mute) {
Expand All @@ -24,6 +25,13 @@ export async function renewTimeoutMute(pluginData: GuildPluginData<MutesPluginTy
}

const expiryTimestamp = moment.utc(newExpiryTime).valueOf();
await member.disableCommunicationUntil(expiryTimestamp);
if (!member.moderatable) {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Cannot renew user's timeout, specified user is not moderatable`,
});
return;
}

await member.disableCommunicationUntil(expiryTimestamp).catch(noop);
await pluginData.state.mutes.updateTimeoutExpiresAt(mute.user_id, expiryTimestamp);
}
8 changes: 5 additions & 3 deletions backend/src/plugins/Mutes/functions/unmuteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CaseTypes } from "../../../data/CaseTypes";
import { AddMuteParams } from "../../../data/GuildMutes";
import { MuteTypes } from "../../../data/MuteTypes";
import { Mute } from "../../../data/entities/Mute";
import { resolveMember, resolveUser } from "../../../utils";
import { noop, resolveMember, resolveUser } from "../../../utils";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { CaseArgs } from "../../Cases/types";
import { LogsPlugin } from "../../Logs/LogsPlugin";
Expand Down Expand Up @@ -54,8 +54,10 @@ export async function unmuteUser(
}

// Update timeout
if (existingMute?.type === MuteTypes.Timeout || createdMute?.type === MuteTypes.Timeout) {
await member?.disableCommunicationUntil(timeoutExpiresAt);
if (member && (existingMute?.type === MuteTypes.Timeout || createdMute?.type === MuteTypes.Timeout)) {
if (!member.moderatable) return null;

await member.disableCommunicationUntil(timeoutExpiresAt).catch(noop);
await pluginData.state.mutes.updateTimeoutExpiresAt(userId, timeoutExpiresAt);
}
} else {
Expand Down

0 comments on commit e8e9212

Please sign in to comment.