zappyzep/backend/src/utils/safeFindRelevantAuditLogEntry.ts

31 lines
947 B
TypeScript
Raw Normal View History

import { GuildPluginData } from "knub";
import { LogType } from "../data/LogType";
import { LogsPlugin } from "../plugins/Logs/LogsPlugin";
import { findRelevantAuditLogEntry, isDiscordRESTError } from "../utils";
2020-07-23 00:37:33 +03:00
/**
* Wrapper for findRelevantAuditLogEntry() that handles permission errors gracefully.
* Calling plugin must have LogsPlugin as a dependency (or be LogsPlugin itself).
2020-07-23 00:37:33 +03:00
*/
export async function safeFindRelevantAuditLogEntry(
pluginData: GuildPluginData<any>,
2020-07-23 00:37:33 +03:00
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) {
const logs = pluginData.getPlugin(LogsPlugin);
logs.log(LogType.BOT_ALERT, {
2020-07-23 00:37:33 +03:00
body: "Missing permissions to read audit log",
});
return;
2020-07-23 00:37:33 +03:00
}
throw e;
2020-07-23 00:37:33 +03:00
}
}