Improve audit log error handling, show BOT_ALERT in more cases
This commit is contained in:
parent
ffc25791ca
commit
5ea0f7eb46
7 changed files with 30 additions and 24 deletions
|
@ -1,7 +1,8 @@
|
||||||
import { logsEvent } from "../types";
|
import { logsEvent } from "../types";
|
||||||
import { stripObjectToScalars, findRelevantAuditLogEntry, UnknownUser } from "src/utils";
|
import { stripObjectToScalars, UnknownUser } from "src/utils";
|
||||||
import { LogType } from "src/data/LogType";
|
import { LogType } from "src/data/LogType";
|
||||||
import { Constants as ErisConstants } from "eris";
|
import { Constants as ErisConstants } from "eris";
|
||||||
|
import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry";
|
||||||
|
|
||||||
export const LogsGuildBanAddEvt = logsEvent({
|
export const LogsGuildBanAddEvt = logsEvent({
|
||||||
event: "guildBanAdd",
|
event: "guildBanAdd",
|
||||||
|
@ -10,8 +11,8 @@ export const LogsGuildBanAddEvt = logsEvent({
|
||||||
const pluginData = meta.pluginData;
|
const pluginData = meta.pluginData;
|
||||||
const user = meta.args.user;
|
const user = meta.args.user;
|
||||||
|
|
||||||
const relevantAuditLogEntry = await findRelevantAuditLogEntry(
|
const relevantAuditLogEntry = await safeFindRelevantAuditLogEntry(
|
||||||
pluginData.guild,
|
pluginData,
|
||||||
ErisConstants.AuditLogActions.MEMBER_BAN_ADD,
|
ErisConstants.AuditLogActions.MEMBER_BAN_ADD,
|
||||||
user.id,
|
user.id,
|
||||||
);
|
);
|
||||||
|
@ -35,8 +36,8 @@ export const LogsGuildBanRemoveEvt = logsEvent({
|
||||||
const pluginData = meta.pluginData;
|
const pluginData = meta.pluginData;
|
||||||
const user = meta.args.user;
|
const user = meta.args.user;
|
||||||
|
|
||||||
const relevantAuditLogEntry = await findRelevantAuditLogEntry(
|
const relevantAuditLogEntry = await safeFindRelevantAuditLogEntry(
|
||||||
pluginData.guild,
|
pluginData,
|
||||||
ErisConstants.AuditLogActions.MEMBER_BAN_REMOVE,
|
ErisConstants.AuditLogActions.MEMBER_BAN_REMOVE,
|
||||||
user.id,
|
user.id,
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import { logsEvent } from "../types";
|
import { logsEvent } from "../types";
|
||||||
import { stripObjectToScalars, findRelevantAuditLogEntry, UnknownUser } from "src/utils";
|
import { stripObjectToScalars, UnknownUser } from "src/utils";
|
||||||
import { Constants as ErisConstants } from "eris";
|
import { Constants as ErisConstants } from "eris";
|
||||||
import { LogType } from "src/data/LogType";
|
import { LogType } from "src/data/LogType";
|
||||||
import isEqual from "lodash.isequal";
|
import isEqual from "lodash.isequal";
|
||||||
import diff from "lodash.difference";
|
import diff from "lodash.difference";
|
||||||
|
import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry";
|
||||||
|
|
||||||
export const LogsGuildMemberUpdateEvt = logsEvent({
|
export const LogsGuildMemberUpdateEvt = logsEvent({
|
||||||
event: "guildMemberUpdate",
|
event: "guildMemberUpdate",
|
||||||
|
@ -46,8 +47,8 @@ export const LogsGuildMemberUpdateEvt = logsEvent({
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!skip) {
|
if (!skip) {
|
||||||
const relevantAuditLogEntry = await findRelevantAuditLogEntry(
|
const relevantAuditLogEntry = await safeFindRelevantAuditLogEntry(
|
||||||
pluginData.guild,
|
pluginData,
|
||||||
ErisConstants.AuditLogActions.MEMBER_ROLE_UPDATE,
|
ErisConstants.AuditLogActions.MEMBER_ROLE_UPDATE,
|
||||||
member.id,
|
member.id,
|
||||||
);
|
);
|
||||||
|
|
|
@ -3,9 +3,9 @@ import { IgnoredEventType, ModActionsPluginType } from "../types";
|
||||||
import { isEventIgnored } from "../functions/isEventIgnored";
|
import { isEventIgnored } from "../functions/isEventIgnored";
|
||||||
import { clearIgnoredEvents } from "../functions/clearIgnoredEvents";
|
import { clearIgnoredEvents } from "../functions/clearIgnoredEvents";
|
||||||
import { Constants as ErisConstants } from "eris";
|
import { Constants as ErisConstants } from "eris";
|
||||||
import { safeFindRelevantAuditLogEntry } from "../functions/safeFindRelevantAuditLogEntry";
|
|
||||||
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
||||||
import { CaseTypes } from "../../../data/CaseTypes";
|
import { CaseTypes } from "../../../data/CaseTypes";
|
||||||
|
import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a BAN case automatically when a user is banned manually.
|
* Create a BAN case automatically when a user is banned manually.
|
||||||
|
|
|
@ -3,12 +3,12 @@ import { IgnoredEventType, ModActionsPluginType } from "../types";
|
||||||
import { isEventIgnored } from "../functions/isEventIgnored";
|
import { isEventIgnored } from "../functions/isEventIgnored";
|
||||||
import { clearIgnoredEvents } from "../functions/clearIgnoredEvents";
|
import { clearIgnoredEvents } from "../functions/clearIgnoredEvents";
|
||||||
import { Constants as ErisConstants } from "eris";
|
import { Constants as ErisConstants } from "eris";
|
||||||
import { safeFindRelevantAuditLogEntry } from "../functions/safeFindRelevantAuditLogEntry";
|
|
||||||
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
||||||
import { CaseTypes } from "../../../data/CaseTypes";
|
import { CaseTypes } from "../../../data/CaseTypes";
|
||||||
import { logger } from "../../../logger";
|
import { logger } from "../../../logger";
|
||||||
import { LogType } from "../../../data/LogType";
|
import { LogType } from "../../../data/LogType";
|
||||||
import { stripObjectToScalars } from "../../../utils";
|
import { stripObjectToScalars } from "../../../utils";
|
||||||
|
import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a KICK case automatically when a user is kicked manually.
|
* Create a KICK case automatically when a user is kicked manually.
|
||||||
|
|
|
@ -3,9 +3,9 @@ import { IgnoredEventType, ModActionsPluginType } from "../types";
|
||||||
import { isEventIgnored } from "../functions/isEventIgnored";
|
import { isEventIgnored } from "../functions/isEventIgnored";
|
||||||
import { clearIgnoredEvents } from "../functions/clearIgnoredEvents";
|
import { clearIgnoredEvents } from "../functions/clearIgnoredEvents";
|
||||||
import { Constants as ErisConstants } from "eris";
|
import { Constants as ErisConstants } from "eris";
|
||||||
import { safeFindRelevantAuditLogEntry } from "../functions/safeFindRelevantAuditLogEntry";
|
|
||||||
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
||||||
import { CaseTypes } from "../../../data/CaseTypes";
|
import { CaseTypes } from "../../../data/CaseTypes";
|
||||||
|
import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an UNBAN case automatically when a user is unbanned manually.
|
* Create an UNBAN case automatically when a user is unbanned manually.
|
||||||
|
|
|
@ -376,16 +376,17 @@ export async function findRelevantAuditLogEntry(
|
||||||
try {
|
try {
|
||||||
auditLogs = await guild.getAuditLogs(5, null, actionType);
|
auditLogs = await guild.getAuditLogs(5, null, actionType);
|
||||||
} catch (e) {
|
} 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 we don't have permission to read audit log, set audit log requests on cooldown
|
||||||
if (isDiscordRESTError(e) && e.code === 50013) {
|
if (isDiscordRESTError(e) && e.code === 50013) {
|
||||||
auditLogNextAttemptAfterFail.set(guild.id, Date.now() + AUDIT_LOG_FAIL_COOLDOWN);
|
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
|
throw e;
|
||||||
if (!isDiscordHTTPError(e) || e.code !== 500) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const entries = auditLogs ? auditLogs.entries : [];
|
const entries = auditLogs ? auditLogs.entries : [];
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
import { findRelevantAuditLogEntry, isDiscordRESTError } from "../../../utils";
|
|
||||||
import { PluginData } from "knub";
|
import { PluginData } from "knub";
|
||||||
import { ModActionsPluginType } from "../types";
|
import { LogsPlugin } from "../plugins/Logs/LogsPlugin";
|
||||||
import { LogType } from "../../../data/LogType";
|
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(
|
export async function safeFindRelevantAuditLogEntry(
|
||||||
pluginData: PluginData<ModActionsPluginType>,
|
pluginData: PluginData<any>,
|
||||||
actionType: number,
|
actionType: number,
|
||||||
userId: string,
|
userId: string,
|
||||||
attempts?: number,
|
attempts?: number,
|
||||||
|
@ -17,11 +18,13 @@ export async function safeFindRelevantAuditLogEntry(
|
||||||
return await findRelevantAuditLogEntry(pluginData.guild, actionType, userId, attempts, attemptDelay);
|
return await findRelevantAuditLogEntry(pluginData.guild, actionType, userId, attempts, attemptDelay);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (isDiscordRESTError(e) && e.code === 50013) {
|
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",
|
body: "Missing permissions to read audit log",
|
||||||
});
|
});
|
||||||
} else {
|
return;
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue