mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
feat: add member cache; handle all role changes with RoleManagerPlugin; exit gracefully
This commit is contained in:
parent
fd60a09947
commit
fa50110766
48 changed files with 755 additions and 264 deletions
|
@ -1,8 +1,7 @@
|
|||
import { Snowflake } from "discord.js";
|
||||
import moment from "moment-timezone";
|
||||
import { MuteTypes } from "../../../data/MuteTypes";
|
||||
import { memberRolesLock } from "../../../utils/lockNameHelpers";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin";
|
||||
import { getTimeoutExpiryTime } from "../functions/getTimeoutExpiryTime";
|
||||
import { mutesEvt } from "../types";
|
||||
|
||||
|
@ -18,15 +17,9 @@ export const ReapplyActiveMuteOnJoinEvt = mutesEvt({
|
|||
}
|
||||
|
||||
if (mute.type === MuteTypes.Role) {
|
||||
const muteRole = pluginData.config.get().mute_role;
|
||||
|
||||
if (muteRole) {
|
||||
const memberRoleLock = await pluginData.locks.acquire(memberRolesLock(member));
|
||||
try {
|
||||
await member.roles.add(muteRole as Snowflake);
|
||||
} finally {
|
||||
memberRoleLock.unlock();
|
||||
}
|
||||
const muteRoleId = pluginData.config.get().mute_role;
|
||||
if (muteRoleId) {
|
||||
pluginData.getPlugin(RoleManagerPlugin).addPriorityRole(member.id, muteRoleId);
|
||||
}
|
||||
} else {
|
||||
if (!member.isCommunicationDisabled()) {
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import { GuildMember } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { MuteTypes } from "../../../data/MuteTypes";
|
||||
import { Mute } from "../../../data/entities/Mute";
|
||||
import { clearExpiringMute } from "../../../data/loops/expiringMutesLoop";
|
||||
import { MuteTypes } from "../../../data/MuteTypes";
|
||||
import { resolveMember, verboseUserMention } from "../../../utils";
|
||||
import { memberRolesLock } from "../../../utils/lockNameHelpers";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin";
|
||||
import { MutesPluginType } from "../types";
|
||||
|
||||
export async function clearMute(
|
||||
|
@ -23,15 +24,16 @@ export async function clearMute(
|
|||
|
||||
if (member) {
|
||||
const lock = await pluginData.locks.acquire(memberRolesLock(member));
|
||||
const roleManagerPlugin = pluginData.getPlugin(RoleManagerPlugin);
|
||||
|
||||
try {
|
||||
const defaultMuteRole = pluginData.config.get().mute_role;
|
||||
if (mute) {
|
||||
const muteRole = mute.mute_role || pluginData.config.get().mute_role;
|
||||
const muteRoleId = mute.mute_role || pluginData.config.get().mute_role;
|
||||
|
||||
if (mute.type === MuteTypes.Role) {
|
||||
if (muteRole) {
|
||||
await member.roles.remove(muteRole);
|
||||
if (muteRoleId) {
|
||||
roleManagerPlugin.removePriorityRole(member.id, muteRoleId);
|
||||
}
|
||||
} else {
|
||||
await member.timeout(null);
|
||||
|
@ -39,19 +41,18 @@ export async function clearMute(
|
|||
|
||||
if (mute.roles_to_restore) {
|
||||
const guildRoles = pluginData.guild.roles.cache;
|
||||
const newRoles = [...member.roles.cache.keys()].filter((roleId) => roleId !== muteRole);
|
||||
for (const toRestore of mute?.roles_to_restore) {
|
||||
if (guildRoles.has(toRestore) && toRestore !== muteRole && !newRoles.includes(toRestore)) {
|
||||
newRoles.push(toRestore);
|
||||
const newRoles = [...member.roles.cache.keys()].filter((roleId) => roleId !== muteRoleId);
|
||||
for (const roleIdToRestore of mute?.roles_to_restore) {
|
||||
if (guildRoles.has(roleIdToRestore) && roleIdToRestore !== muteRoleId) {
|
||||
roleManagerPlugin.addRole(member.id, roleIdToRestore);
|
||||
}
|
||||
}
|
||||
await member.roles.set(newRoles);
|
||||
}
|
||||
} else {
|
||||
// Unmuting someone without an active mute -> remove timeouts and/or mute role
|
||||
const muteRole = pluginData.config.get().mute_role;
|
||||
if (muteRole && member.roles.cache.has(muteRole)) {
|
||||
await member.roles.remove(muteRole);
|
||||
roleManagerPlugin.removePriorityRole(member.id, muteRole);
|
||||
}
|
||||
if (member.isCommunicationDisabled()) {
|
||||
await member.timeout(null);
|
||||
|
|
|
@ -1,26 +1,27 @@
|
|||
import { Snowflake } from "discord.js";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
|
||||
import { CaseTypes } from "../../../data/CaseTypes";
|
||||
import { AddMuteParams } from "../../../data/GuildMutes";
|
||||
import { MuteTypes } from "../../../data/MuteTypes";
|
||||
import { Case } from "../../../data/entities/Case";
|
||||
import { Mute } from "../../../data/entities/Mute";
|
||||
import { AddMuteParams } from "../../../data/GuildMutes";
|
||||
import { registerExpiringMute } from "../../../data/loops/expiringMutesLoop";
|
||||
import { MuteTypes } from "../../../data/MuteTypes";
|
||||
import { LogsPlugin } from "../../../plugins/Logs/LogsPlugin";
|
||||
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
|
||||
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
|
||||
import { TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter";
|
||||
import {
|
||||
UserNotificationMethod,
|
||||
UserNotificationResult,
|
||||
notifyUser,
|
||||
resolveMember,
|
||||
resolveUser,
|
||||
ucfirst,
|
||||
UserNotificationMethod,
|
||||
UserNotificationResult,
|
||||
} from "../../../utils";
|
||||
import { muteLock } from "../../../utils/lockNameHelpers";
|
||||
import { userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
|
||||
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
||||
import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin";
|
||||
import { MuteOptions, MutesPluginType } from "../types";
|
||||
import { getDefaultMuteType } from "./getDefaultMuteType";
|
||||
import { getTimeoutExpiryTime } from "./getTimeoutExpiryTime";
|
||||
|
@ -91,38 +92,29 @@ export async function muteUser(
|
|||
}
|
||||
|
||||
if (muteType === MuteTypes.Role) {
|
||||
// Apply mute role if it's missing
|
||||
if (!currentUserRoles.includes(muteRole!)) {
|
||||
try {
|
||||
await member.roles.add(muteRole!);
|
||||
} catch (e) {
|
||||
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 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);
|
||||
}
|
||||
|
||||
const zep = await resolveMember(pluginData.client, pluginData.guild, pluginData.client.user!.id);
|
||||
const zepRoles = pluginData.guild.roles.cache.filter((x) => zep!.roles.cache.has(x.id));
|
||||
// If we have roles and one of them is above the muted role, throw generic error
|
||||
if (zepRoles.size >= 0 && zepRoles.some((zepRole) => zepRole.position > actualMuteRole.position)) {
|
||||
lock.unlock();
|
||||
logs.logBotAlert({
|
||||
body: `Cannot mute user ${member.id}: ${e}`,
|
||||
});
|
||||
throw e;
|
||||
} else {
|
||||
// Otherwise, throw error that mute role is above zeps roles
|
||||
lock.unlock();
|
||||
logs.logBotAlert({
|
||||
body: `Cannot mute users, specified mute role is above Zeppelin in the role hierarchy`,
|
||||
});
|
||||
throw new RecoverablePluginError(ERRORS.MUTE_ROLE_ABOVE_ZEP, pluginData.guild);
|
||||
}
|
||||
}
|
||||
// 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 users, 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 {
|
||||
await member.disableCommunicationUntil(timeoutUntil);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue