-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathmuteUser.ts
More file actions
328 lines (295 loc) · 11.7 KB
/
muteUser.ts
File metadata and controls
328 lines (295 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { Snowflake } from "discord.js";
import { GuildPluginData } from "vety";
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError.js";
import { CaseTypes } from "../../../data/CaseTypes.js";
import { AddMuteParams } from "../../../data/GuildMutes.js";
import { MuteTypes } from "../../../data/MuteTypes.js";
import { Case } from "../../../data/entities/Case.js";
import { Mute } from "../../../data/entities/Mute.js";
import { registerExpiringMute } from "../../../data/loops/expiringMutesLoop.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { LogsPlugin } from "../../../plugins/Logs/LogsPlugin.js";
import { TemplateParseError, TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter.js";
import {
UserNotificationMethod,
UserNotificationResult,
noop,
notifyUser,
resolveMember,
resolveUser,
ucfirst,
} from "../../../utils.js";
import { muteLock } from "../../../utils/lockNameHelpers.js";
import { userToTemplateSafeUser } from "../../../utils/templateSafeObjects.js";
import { CasesPlugin } from "../../Cases/CasesPlugin.js";
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin.js";
import { MuteOptions, MutesPluginType } from "../types.js";
import { getDefaultMuteType } from "./getDefaultMuteType.js";
import { getTimeoutExpiryTime } from "./getTimeoutExpiryTime.js";
/**
* TODO: Clean up this function
*/
export async function muteUser(
pluginData: GuildPluginData<MutesPluginType>,
userId: string,
muteTime?: number,
reason?: string,
reasonWithAttachments?: string,
muteOptions: MuteOptions = {},
removeRolesOnMuteOverride: boolean | string[] | null = null,
restoreRolesOnMuteOverride: boolean | string[] | null = null,
) {
const lock = await pluginData.locks.acquire(muteLock({ id: userId }));
const muteRole = pluginData.config.get().mute_role;
const muteType = getDefaultMuteType(pluginData);
const muteExpiresAt = muteTime ? Date.now() + muteTime : null;
const timeoutUntil = getTimeoutExpiryTime(muteExpiresAt);
const muteExpiryTimestamp = muteExpiresAt ? Math.ceil(muteExpiresAt / 1000) : null;
const muteExpiryRelative = muteExpiryTimestamp ? `<t:${muteExpiryTimestamp}:R>` : null;
// No mod specified -> mark Zeppelin as the mod
if (!muteOptions.caseArgs?.modId) {
muteOptions.caseArgs = muteOptions.caseArgs ?? {};
muteOptions.caseArgs.modId = pluginData.client.user!.id;
}
const user = await resolveUser(pluginData.client, userId, "Mutes:muteUser");
if (!user.id) {
lock.unlock();
throw new RecoverablePluginError(ERRORS.INVALID_USER);
}
const member = await resolveMember(pluginData.client, pluginData.guild, user.id, true); // Grab the fresh member so we don't have stale role info
const config = await pluginData.config.getMatchingConfig({ member, userId });
const logs = pluginData.getPlugin(LogsPlugin);
let rolesToRestore: string[] = [];
if (member) {
// remove and store any roles to be removed/restored
const currentUserRoles = [...member.roles.cache.keys()];
let newRoles: string[] = currentUserRoles;
const removeRoles = removeRolesOnMuteOverride ?? config.remove_roles_on_mute;
const restoreRoles = restoreRolesOnMuteOverride ?? config.restore_roles_on_mute;
// Remove roles
if (!Array.isArray(removeRoles)) {
if (removeRoles) {
// exclude managed roles from being removed
const managedRoles = pluginData.guild.roles.cache.filter((x) => x.managed).map((y) => y.id);
newRoles = currentUserRoles.filter((r) => managedRoles.includes(r));
await member.roles.set(newRoles as Snowflake[]);
}
} else {
newRoles = currentUserRoles.filter((x) => !(<string[]>removeRoles).includes(x));
await member.roles.set(newRoles as Snowflake[]);
}
// Set roles to be restored
if (!Array.isArray(restoreRoles)) {
if (restoreRoles) {
rolesToRestore = currentUserRoles;
}
} else {
rolesToRestore = currentUserRoles.filter((x) => (<string[]>restoreRoles).includes(x));
}
if (muteType === MuteTypes.Role) {
// Verify the configured mute role is valid
const actualMuteRole = pluginData.guild.roles.cache.get(muteRole!);
if (!actualMuteRole) {
lock.unlock();
logs.logBotAlert({
body: `Cannot mute users, specified mute role Id is invalid`,
});
throw new RecoverablePluginError(ERRORS.INVALID_MUTE_ROLE_ID);
}
// Verify the mute role is not above Zep's roles
const zep = await pluginData.guild.members.fetchMe();
const zepRoles = pluginData.guild.roles.cache.filter((x) => zep.roles.cache.has(x.id));
if (zepRoles.size === 0 || !zepRoles.some((zepRole) => zepRole.position > actualMuteRole.position)) {
lock.unlock();
logs.logBotAlert({
body: `Cannot mute user, specified mute role is above Zeppelin in the role hierarchy`,
});
throw new RecoverablePluginError(ERRORS.MUTE_ROLE_ABOVE_ZEP, pluginData.guild);
}
if (!currentUserRoles.includes(muteRole!)) {
pluginData.getPlugin(RoleManagerPlugin).addPriorityRole(member.id, muteRole!);
}
} else {
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)
const cfg = pluginData.config.get();
const moveToVoiceChannel = cfg.kick_from_voice_channel ? null : cfg.move_to_voice_channel;
if (moveToVoiceChannel || cfg.kick_from_voice_channel) {
// TODO: Add back the voiceState check once we figure out how to get voice state for guild members that are loaded on-demand
try {
await member.edit({ channel: moveToVoiceChannel as Snowflake });
} catch {} // eslint-disable-line no-empty
}
}
// If the user is already muted, update the duration of their existing mute
const existingMute = await pluginData.state.mutes.findExistingMuteForUserId(user.id);
let finalMute: Mute;
let notifyResult: UserNotificationResult = { method: null, success: true };
if (existingMute) {
if (existingMute.roles_to_restore?.length || rolesToRestore?.length) {
rolesToRestore = Array.from(new Set([...existingMute.roles_to_restore, ...rolesToRestore]));
}
await pluginData.state.mutes.updateExpiryTime(user.id, muteTime, rolesToRestore);
if (muteType === MuteTypes.Timeout) {
await pluginData.state.mutes.updateTimeoutExpiresAt(user.id, timeoutUntil);
}
finalMute = (await pluginData.state.mutes.findExistingMuteForUserId(user.id))!;
} else {
const muteParams: AddMuteParams = {
userId: user.id,
type: muteType,
expiresAt: muteExpiresAt,
rolesToRestore,
};
if (muteType === MuteTypes.Role) {
muteParams.muteRole = muteRole;
} else {
muteParams.timeoutExpiresAt = timeoutUntil;
}
finalMute = await pluginData.state.mutes.addMute(muteParams);
}
registerExpiringMute(finalMute);
const timeUntilUnmuteStr = muteTime ? humanizeDuration(muteTime) : "indefinite";
const template = existingMute
? config.update_mute_message
: muteTime
? config.timed_mute_message
: config.mute_message;
let muteMessage: string | null = null;
try {
muteMessage =
template &&
(await renderTemplate(
template,
new TemplateSafeValueContainer({
guildName: pluginData.guild.name,
reason: reasonWithAttachments || "None",
time: timeUntilUnmuteStr,
moderator: muteOptions.caseArgs?.modId
? userToTemplateSafeUser(await resolveUser(pluginData.client, muteOptions.caseArgs.modId, "Mutes:muteUser"))
: null,
}),
));
} catch (err) {
if (err instanceof TemplateParseError) {
logs.logBotAlert({
body: `Invalid mute message format. The mute was still applied: ${err.message}`,
});
} else {
lock.unlock();
throw err;
}
}
if (muteMessage && member) {
let contactMethods: UserNotificationMethod[] = [];
if (muteOptions?.contactMethods) {
contactMethods = muteOptions.contactMethods;
} else {
const useDm = existingMute ? config.dm_on_update : config.dm_on_mute;
if (useDm) {
contactMethods.push({ type: "dm" });
}
const useChannel = existingMute ? config.message_on_update : config.message_on_mute;
const channel = config.message_channel
? pluginData.guild.channels.cache.get(config.message_channel as Snowflake)
: null;
if (useChannel && channel?.isTextBased()) {
contactMethods.push({ type: "channel", channel });
}
}
notifyResult = await notifyUser(member.user, muteMessage, contactMethods);
}
// Create/update a case
const casesPlugin = pluginData.getPlugin(CasesPlugin);
let theCase: Case | null =
existingMute && existingMute.case_id ? await pluginData.state.cases.find(existingMute.case_id) : null;
if (theCase) {
// Update old case
const noteDetails: string[] = [];
if (muteTime) {
noteDetails.push(`Mute updated to ${timeUntilUnmuteStr}`);
if (muteExpiryRelative) {
noteDetails.push(`Expires ${muteExpiryRelative}`);
}
} else {
noteDetails.push("Mute updated to indefinite");
}
const reasons = reason ? [reason] : [""]; // Empty string so that there is a case update even without reason
if (muteOptions.caseArgs?.extraNotes) {
reasons.push(...muteOptions.caseArgs.extraNotes);
}
for (const noteReason of reasons) {
await casesPlugin.createCaseNote({
caseId: existingMute!.case_id,
modId: muteOptions.caseArgs?.modId,
body: noteReason,
noteDetails,
postInCaseLogOverride: muteOptions.caseArgs?.postInCaseLogOverride,
});
}
} else {
// Create new case
const noteDetails: string[] = [];
if (muteTime) {
noteDetails.push(`Muted for ${timeUntilUnmuteStr}`);
if (muteExpiryRelative) {
noteDetails.push(`Expires ${muteExpiryRelative}`);
}
} else {
noteDetails.push("Muted indefinitely");
}
if (notifyResult.text) {
noteDetails.push(ucfirst(notifyResult.text));
}
theCase = await casesPlugin.createCase({
...(muteOptions.caseArgs || {}),
userId,
modId: muteOptions.caseArgs?.modId,
type: CaseTypes.Mute,
reason,
noteDetails,
});
await pluginData.state.mutes.setCaseId(user.id, theCase.id);
}
// Log the action
const mod = await resolveUser(pluginData.client, muteOptions.caseArgs?.modId, "Mutes:muteUser");
if (muteTime) {
pluginData.getPlugin(LogsPlugin).logMemberTimedMute({
mod,
user,
time: timeUntilUnmuteStr,
caseNumber: theCase.case_number,
reason: reason ?? "",
});
} else {
pluginData.getPlugin(LogsPlugin).logMemberMute({
mod,
user,
caseNumber: theCase.case_number,
reason: reason ?? "",
});
}
lock.unlock();
pluginData.state.events.emit("mute", user.id, reason, muteOptions.isAutomodAction);
return {
case: theCase,
notifyResult,
updatedExistingMute: !!existingMute,
};
}