Improve audit log error handling, show BOT_ALERT in more cases

This commit is contained in:
Dragory 2020-08-05 02:25:13 +03:00
parent ffc25791ca
commit 5ea0f7eb46
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
7 changed files with 30 additions and 24 deletions

View file

@ -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,
);

View file

@ -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,
);

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -1,27 +0,0 @@
import { findRelevantAuditLogEntry, isDiscordRESTError } from "../../../utils";
import { PluginData } from "knub";
import { ModActionsPluginType } from "../types";
import { LogType } from "../../../data/LogType";
/**
* Wrapper for findRelevantAuditLogEntry() that handles permission errors gracefully
*/
export async function safeFindRelevantAuditLogEntry(
pluginData: PluginData<ModActionsPluginType>,
actionType: number,
userId: string,
attempts?: number,
attemptDelay?: number,
) {
try {
return await findRelevantAuditLogEntry(pluginData.guild, actionType, userId, attempts, attemptDelay);
} catch (e) {
if (isDiscordRESTError(e) && e.code === 50013) {
pluginData.state.serverLogs.log(LogType.BOT_ALERT, {
body: "Missing permissions to read audit log",
});
} else {
throw e;
}
}
}