3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-18 23:09:59 +00:00
zeppelin/backend/src/utils/safeFindRelevantAuditLogEntry.ts
2020-10-01 01:43:38 +03:00

30 lines
947 B
TypeScript

import { GuildPluginData } from "knub";
import { LogsPlugin } from "../plugins/Logs/LogsPlugin";
import { findRelevantAuditLogEntry, isDiscordRESTError } from "../utils";
import { LogType } from "../data/LogType";
/**
* 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: GuildPluginData<any>,
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, {
body: "Missing permissions to read audit log",
});
return;
}
throw e;
}
}