diff --git a/backend/src/plugins/Logs/events/LogsGuildBanEvts.ts b/backend/src/plugins/Logs/events/LogsGuildBanEvts.ts index 25c4c808..ff327923 100644 --- a/backend/src/plugins/Logs/events/LogsGuildBanEvts.ts +++ b/backend/src/plugins/Logs/events/LogsGuildBanEvts.ts @@ -1,7 +1,8 @@ import { logsEvent } from "../types"; -import { stripObjectToScalars, findRelevantAuditLogEntry, UnknownUser } from "src/utils"; +import { stripObjectToScalars, UnknownUser } from "src/utils"; import { LogType } from "src/data/LogType"; import { Constants as ErisConstants } from "eris"; +import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry"; export const LogsGuildBanAddEvt = logsEvent({ event: "guildBanAdd", @@ -10,8 +11,8 @@ export const LogsGuildBanAddEvt = logsEvent({ const pluginData = meta.pluginData; const user = meta.args.user; - const relevantAuditLogEntry = await findRelevantAuditLogEntry( - pluginData.guild, + const relevantAuditLogEntry = await safeFindRelevantAuditLogEntry( + pluginData, ErisConstants.AuditLogActions.MEMBER_BAN_ADD, user.id, ); @@ -35,8 +36,8 @@ export const LogsGuildBanRemoveEvt = logsEvent({ const pluginData = meta.pluginData; const user = meta.args.user; - const relevantAuditLogEntry = await findRelevantAuditLogEntry( - pluginData.guild, + const relevantAuditLogEntry = await safeFindRelevantAuditLogEntry( + pluginData, ErisConstants.AuditLogActions.MEMBER_BAN_REMOVE, user.id, ); diff --git a/backend/src/plugins/Logs/events/LogsUserUpdateEvts.ts b/backend/src/plugins/Logs/events/LogsUserUpdateEvts.ts index ee479b55..189af03c 100644 --- a/backend/src/plugins/Logs/events/LogsUserUpdateEvts.ts +++ b/backend/src/plugins/Logs/events/LogsUserUpdateEvts.ts @@ -1,9 +1,10 @@ import { logsEvent } from "../types"; -import { stripObjectToScalars, findRelevantAuditLogEntry, UnknownUser } from "src/utils"; +import { stripObjectToScalars, UnknownUser } from "src/utils"; import { Constants as ErisConstants } from "eris"; import { LogType } from "src/data/LogType"; import isEqual from "lodash.isequal"; import diff from "lodash.difference"; +import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry"; export const LogsGuildMemberUpdateEvt = logsEvent({ event: "guildMemberUpdate", @@ -46,8 +47,8 @@ export const LogsGuildMemberUpdateEvt = logsEvent({ } if (!skip) { - const relevantAuditLogEntry = await findRelevantAuditLogEntry( - pluginData.guild, + const relevantAuditLogEntry = await safeFindRelevantAuditLogEntry( + pluginData, ErisConstants.AuditLogActions.MEMBER_ROLE_UPDATE, member.id, ); diff --git a/backend/src/plugins/ModActions/events/CreateBanCaseOnManualBanEvt.ts b/backend/src/plugins/ModActions/events/CreateBanCaseOnManualBanEvt.ts index 764055f3..2cc55a2f 100644 --- a/backend/src/plugins/ModActions/events/CreateBanCaseOnManualBanEvt.ts +++ b/backend/src/plugins/ModActions/events/CreateBanCaseOnManualBanEvt.ts @@ -3,9 +3,9 @@ import { IgnoredEventType, ModActionsPluginType } from "../types"; import { isEventIgnored } from "../functions/isEventIgnored"; import { clearIgnoredEvents } from "../functions/clearIgnoredEvents"; import { Constants as ErisConstants } from "eris"; -import { safeFindRelevantAuditLogEntry } from "../functions/safeFindRelevantAuditLogEntry"; import { CasesPlugin } from "../../Cases/CasesPlugin"; import { CaseTypes } from "../../../data/CaseTypes"; +import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry"; /** * Create a BAN case automatically when a user is banned manually. diff --git a/backend/src/plugins/ModActions/events/CreateKickCaseOnManualKickEvt.ts b/backend/src/plugins/ModActions/events/CreateKickCaseOnManualKickEvt.ts index 6606b708..4dae0ccf 100644 --- a/backend/src/plugins/ModActions/events/CreateKickCaseOnManualKickEvt.ts +++ b/backend/src/plugins/ModActions/events/CreateKickCaseOnManualKickEvt.ts @@ -3,12 +3,12 @@ import { IgnoredEventType, ModActionsPluginType } from "../types"; import { isEventIgnored } from "../functions/isEventIgnored"; import { clearIgnoredEvents } from "../functions/clearIgnoredEvents"; import { Constants as ErisConstants } from "eris"; -import { safeFindRelevantAuditLogEntry } from "../functions/safeFindRelevantAuditLogEntry"; import { CasesPlugin } from "../../Cases/CasesPlugin"; import { CaseTypes } from "../../../data/CaseTypes"; import { logger } from "../../../logger"; import { LogType } from "../../../data/LogType"; import { stripObjectToScalars } from "../../../utils"; +import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry"; /** * Create a KICK case automatically when a user is kicked manually. diff --git a/backend/src/plugins/ModActions/events/CreateUnbanCaseOnManualUnbanEvt.ts b/backend/src/plugins/ModActions/events/CreateUnbanCaseOnManualUnbanEvt.ts index 9e9fd7a5..d3016c02 100644 --- a/backend/src/plugins/ModActions/events/CreateUnbanCaseOnManualUnbanEvt.ts +++ b/backend/src/plugins/ModActions/events/CreateUnbanCaseOnManualUnbanEvt.ts @@ -3,9 +3,9 @@ import { IgnoredEventType, ModActionsPluginType } from "../types"; import { isEventIgnored } from "../functions/isEventIgnored"; import { clearIgnoredEvents } from "../functions/clearIgnoredEvents"; import { Constants as ErisConstants } from "eris"; -import { safeFindRelevantAuditLogEntry } from "../functions/safeFindRelevantAuditLogEntry"; import { CasesPlugin } from "../../Cases/CasesPlugin"; import { CaseTypes } from "../../../data/CaseTypes"; +import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry"; /** * Create an UNBAN case automatically when a user is unbanned manually. diff --git a/backend/src/utils.ts b/backend/src/utils.ts index 31b04d8e..9b811df0 100644 --- a/backend/src/utils.ts +++ b/backend/src/utils.ts @@ -376,16 +376,17 @@ export async function findRelevantAuditLogEntry( try { auditLogs = await guild.getAuditLogs(5, null, actionType); } catch (e) { + // Ignore internal server errors which seem to be pretty common with audit log requests + if (!isDiscordHTTPError(e) || e.code !== 500) { + // Ignore, try again next attempt + } + // If we don't have permission to read audit log, set audit log requests on cooldown if (isDiscordRESTError(e) && e.code === 50013) { auditLogNextAttemptAfterFail.set(guild.id, Date.now() + AUDIT_LOG_FAIL_COOLDOWN); - throw e; } - // Ignore internal server errors which seem to be pretty common with audit log requests - if (!isDiscordHTTPError(e) || e.code !== 500) { - throw e; - } + throw e; } const entries = auditLogs ? auditLogs.entries : []; diff --git a/backend/src/plugins/ModActions/functions/safeFindRelevantAuditLogEntry.ts b/backend/src/utils/safeFindRelevantAuditLogEntry.ts similarity index 61% rename from backend/src/plugins/ModActions/functions/safeFindRelevantAuditLogEntry.ts rename to backend/src/utils/safeFindRelevantAuditLogEntry.ts index 0a497c1f..d8d1e55b 100644 --- a/backend/src/plugins/ModActions/functions/safeFindRelevantAuditLogEntry.ts +++ b/backend/src/utils/safeFindRelevantAuditLogEntry.ts @@ -1,13 +1,14 @@ -import { findRelevantAuditLogEntry, isDiscordRESTError } from "../../../utils"; import { PluginData } from "knub"; -import { ModActionsPluginType } from "../types"; -import { LogType } from "../../../data/LogType"; +import { LogsPlugin } from "../plugins/Logs/LogsPlugin"; +import { findRelevantAuditLogEntry, isDiscordRESTError } from "../utils"; +import { LogType } from "../data/LogType"; /** - * Wrapper for findRelevantAuditLogEntry() that handles permission errors gracefully + * Wrapper for findRelevantAuditLogEntry() that handles permission errors gracefully. + * Calling plugin must have LogsPlugin as a dependency (or be LogsPlugin itself). */ export async function safeFindRelevantAuditLogEntry( - pluginData: PluginData, + pluginData: PluginData, actionType: number, userId: string, attempts?: number, @@ -17,11 +18,13 @@ export async function safeFindRelevantAuditLogEntry( return await findRelevantAuditLogEntry(pluginData.guild, actionType, userId, attempts, attemptDelay); } catch (e) { if (isDiscordRESTError(e) && e.code === 50013) { - pluginData.state.serverLogs.log(LogType.BOT_ALERT, { + const logs = pluginData.getPlugin(LogsPlugin); + logs.log(LogType.BOT_ALERT, { body: "Missing permissions to read audit log", }); - } else { - throw e; + return; } + + throw e; } }