3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35:02 +00:00

Port Mutes plugin

This commit is contained in:
Dragory 2020-07-22 22:08:59 +03:00
parent 479cb56928
commit ccff7384ba
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
11 changed files with 797 additions and 0 deletions

View file

@ -0,0 +1,30 @@
import { PluginData } from "knub";
import { MutesPluginType } from "../types";
import { LogType } from "../../../data/LogType";
import { resolveMember, stripObjectToScalars, UnknownUser } from "../../../utils";
export async function clearExpiredMutes(pluginData: PluginData<MutesPluginType>) {
const expiredMutes = await pluginData.state.mutes.getExpiredMutes();
for (const mute of expiredMutes) {
const member = await resolveMember(pluginData.client, pluginData.guild, mute.user_id);
if (member) {
try {
await member.removeRole(pluginData.config.get().mute_role);
} catch (e) {
pluginData.state.serverLogs.log(LogType.BOT_ALERT, {
body: `Failed to remove mute role from {userMention(member)}`,
member: stripObjectToScalars(member),
});
}
}
await pluginData.state.mutes.clear(mute.user_id);
pluginData.state.serverLogs.log(LogType.MEMBER_MUTE_EXPIRED, {
member: member
? stripObjectToScalars(member, ["user", "roles"])
: { id: mute.user_id, user: new UnknownUser({ id: mute.user_id }) },
});
}
}

View file

@ -0,0 +1,7 @@
import { Member } from "eris";
import { PluginData } from "knub";
import { MutesPluginType } from "../types";
export function memberHasMutedRole(pluginData: PluginData<MutesPluginType>, member: Member) {
return member.roles.includes(pluginData.config.get().mute_role);
}

View file

@ -0,0 +1,173 @@
import { PluginData } from "knub";
import { MuteOptions, MutesPluginType } from "../types";
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
import humanizeDuration from "humanize-duration";
import {
notifyUser,
resolveMember,
resolveUser,
stripObjectToScalars,
ucfirst,
UserNotificationResult,
} from "../../../utils";
import { renderTemplate } from "../../../templateFormatter";
import { TextChannel, User } from "eris";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { CaseTypes } from "../../../data/CaseTypes";
import { LogType } from "../../../data/LogType";
export async function muteUser(
pluginData: PluginData<MutesPluginType>,
userId: string,
muteTime: number = null,
reason: string = null,
muteOptions: MuteOptions = {},
) {
const lock = await pluginData.locks.acquire(`mute-${userId}`);
const muteRole = pluginData.config.get().mute_role;
if (!muteRole) {
lock.unlock();
throw new RecoverablePluginError(ERRORS.NO_MUTE_ROLE_IN_CONFIG);
}
const timeUntilUnmute = muteTime ? humanizeDuration(muteTime) : "indefinite";
// 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);
const member = await pluginData.client.getRESTGuildMember(pluginData.guild.id, user.id); // Grab the fresh member so we don't have stale role info
const config = pluginData.config.getMatchingConfig({ member, userId });
if (member) {
// Apply mute role if it's missing
if (!member.roles.includes(muteRole)) {
await member.addRole(muteRole);
}
// If enabled, move the user to the mute voice channel (e.g. afk - just to apply the voice perms from the mute role)
const moveToVoiceChannelId = pluginData.config.get().move_to_voice_channel;
if (moveToVoiceChannelId) {
// 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({ channelID: moveToVoiceChannelId });
} catch (e) {} // tslint:disable-line
}
}
// If the user is already muted, update the duration of their existing mute
const existingMute = await pluginData.state.mutes.findExistingMuteForUserId(user.id);
let notifyResult: UserNotificationResult = { method: null, success: true };
if (existingMute) {
await pluginData.state.mutes.updateExpiryTime(user.id, muteTime);
} else {
await pluginData.state.mutes.addMute(user.id, muteTime);
}
const template = existingMute
? config.update_mute_message
: muteTime
? config.timed_mute_message
: config.mute_message;
const muteMessage =
template &&
(await renderTemplate(template, {
guildName: pluginData.guild.name,
reason: reason || "None",
time: timeUntilUnmute,
}));
if (muteMessage && user instanceof User) {
let contactMethods = [];
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.get(config.message_channel);
if (useChannel && channel instanceof TextChannel) {
contactMethods.push({ type: "channel", channel });
}
}
notifyResult = await notifyUser(user, muteMessage, contactMethods);
}
// Create/update a case
const casesPlugin = pluginData.getPlugin(CasesPlugin);
let theCase;
if (existingMute && existingMute.case_id) {
// Update old case
// Since mutes can often have multiple notes (extraNotes), we won't post each case note individually,
// but instead we'll post the entire case afterwards
theCase = await pluginData.state.cases.find(existingMute.case_id);
const noteDetails = [`Mute updated to ${muteTime ? timeUntilUnmute : "indefinite"}`];
const reasons = [reason, ...(muteOptions.caseArgs?.extraNotes || [])];
for (const noteReason of reasons) {
await casesPlugin.createCaseNote({
caseId: existingMute.case_id,
modId: muteOptions.caseArgs?.modId,
body: noteReason,
noteDetails,
postInCaseLogOverride: false,
});
}
if (muteOptions.caseArgs?.postInCaseLogOverride !== false) {
casesPlugin.postCaseToCaseLogChannel(existingMute.case_id);
}
} else {
// Create new case
const noteDetails = [`Muted ${muteTime ? `for ${timeUntilUnmute}` : "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);
if (muteTime) {
pluginData.state.serverLogs.log(LogType.MEMBER_TIMED_MUTE, {
mod: stripObjectToScalars(mod),
user: stripObjectToScalars(user),
time: timeUntilUnmute,
reason,
});
} else {
pluginData.state.serverLogs.log(LogType.MEMBER_MUTE, {
mod: stripObjectToScalars(mod),
user: stripObjectToScalars(user),
reason,
});
}
lock.unlock();
return {
case: theCase,
notifyResult,
updatedExistingMute: !!existingMute,
};
}

View file

@ -0,0 +1,89 @@
import { PluginData } from "knub";
import { MutesPluginType, UnmuteResult } from "../types";
import { CaseArgs } from "../../Cases/types";
import { resolveUser, stripObjectToScalars } from "../../../utils";
import { memberHasMutedRole } from "./memberHasMutedRole";
import humanizeDuration from "humanize-duration";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { CaseTypes } from "../../../data/CaseTypes";
import { LogType } from "../../../data/LogType";
export async function unmuteUser(
pluginData: PluginData<MutesPluginType>,
userId: string,
unmuteTime: number = null,
caseArgs: Partial<CaseArgs> = {},
): Promise<UnmuteResult> {
const existingMute = await pluginData.state.mutes.findExistingMuteForUserId(userId);
const user = await resolveUser(pluginData.client, userId);
const member = await pluginData.client.getRESTGuildMember(pluginData.guild.id, userId); // Grab the fresh member so we don't have stale role info
if (!existingMute && !memberHasMutedRole(pluginData, member)) return;
if (unmuteTime) {
// Schedule timed unmute (= just set the mute's duration)
if (!existingMute) {
await pluginData.state.mutes.addMute(userId, unmuteTime);
} else {
await pluginData.state.mutes.updateExpiryTime(userId, unmuteTime);
}
} else {
// Unmute immediately
if (member) {
const muteRole = pluginData.config.get().mute_role;
if (member.roles.includes(muteRole)) {
await member.removeRole(muteRole);
}
} else {
console.warn(
`Member ${userId} not found in guild ${pluginData.guild.name} (${pluginData.guild.id}) when attempting to unmute`,
);
}
if (existingMute) {
await pluginData.state.mutes.clear(userId);
}
}
const timeUntilUnmute = unmuteTime && humanizeDuration(unmuteTime);
// Create a case
const noteDetails = [];
if (unmuteTime) {
noteDetails.push(`Scheduled unmute in ${timeUntilUnmute}`);
} else {
noteDetails.push(`Unmuted immediately`);
}
if (!existingMute) {
noteDetails.push(`Removed external mute`);
}
const casesPlugin = pluginData.getPlugin(CasesPlugin);
const createdCase = await casesPlugin.createCase({
...caseArgs,
userId,
modId: caseArgs.modId,
type: CaseTypes.Unmute,
noteDetails,
});
// Log the action
const mod = pluginData.client.users.get(caseArgs.modId);
if (unmuteTime) {
pluginData.state.serverLogs.log(LogType.MEMBER_TIMED_UNMUTE, {
mod: stripObjectToScalars(mod),
user: stripObjectToScalars(user),
time: timeUntilUnmute,
reason: caseArgs.reason,
});
} else {
pluginData.state.serverLogs.log(LogType.MEMBER_UNMUTE, {
mod: stripObjectToScalars(mod),
user: stripObjectToScalars(user),
reason: caseArgs.reason,
});
}
return {
case: createdCase,
};
}