Fix permission errors when reading audit logs (#25)
Note: This PR depends on #24
This error does not actually break any functionality but it both
clutters the logs and may cause problems at scale if we start hitting
the API error limit
diff --git a/src/utils/log.ts b/src/utils/log.ts
index 0601863..bc95dd8 100644
--- a/src/utils/log.ts
+++ b/src/utils/log.ts
@@ -4,6 +4,7 @@
import { promisify } from "util";
import generateKeyValueList from "./generateKeyValueList.js";
import client from "./client.js";
+import {DiscordAPIError} from "discord.js";
const wait = promisify(setTimeout);
@@ -84,9 +85,15 @@
event: Discord.GuildAuditLogsResolvable,
delay?: number
): Promise<Discord.GuildAuditLogsEntry[]> {
+ if (!guild.members.me?.permissions.has("ViewAuditLog")) return [];
await wait(delay ?? 250);
- const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map((m) => m);
- return auditLog as Discord.GuildAuditLogsEntry[];
+ try {
+ const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map((m) => m);
+ return auditLog as Discord.GuildAuditLogsEntry[];
+ } catch (e) {
+ if (e instanceof DiscordAPIError) return [];
+ throw e;
+ }
},
async log(log: LoggerOptions): Promise<void> {
if (!(await isLogging(log.hidden.guild, log.meta.calculateType))) return;