3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-18 07:35:02 +00:00

Add bot alerts for missing audit log permissions; ignore internal server errors on discord's side when fetching audit logs

This commit is contained in:
Dragory 2019-07-22 13:50:24 +03:00
parent db3e5cd32d
commit 6874a08904
3 changed files with 52 additions and 18 deletions

View file

@ -3,12 +3,14 @@ import {
EmbedOptions,
Emoji,
Guild,
GuildAuditLog,
GuildAuditLogEntry,
Member,
TextableChannel,
TextChannel,
User,
} from "eris";
import DiscordHTTPError from "eris/lib/errors/DiscordHTTPError"; // tslint:disable-line
import url from "url";
import tlds from "tlds";
import emojiRegex from "emoji-regex";
@ -129,9 +131,19 @@ export async function findRelevantAuditLogEntry(
attempts: number = 3,
attemptDelay: number = 3000,
): Promise<GuildAuditLogEntry> {
const auditLogEntries = await guild.getAuditLogs(5, null, actionType);
let auditLogs: GuildAuditLog;
try {
auditLogs = await guild.getAuditLogs(5, null, actionType);
} catch (e) {
// Ignore internal server errors which seem to be pretty common with audit log requests
if (!(e instanceof DiscordHTTPError) || e.code !== 500) {
throw e;
}
}
auditLogEntries.entries.sort((a, b) => {
const entries = auditLogs ? auditLogs.entries : [];
entries.sort((a, b) => {
if (a.createdAt > b.createdAt) return -1;
if (a.createdAt > b.createdAt) return 1;
return 0;
@ -139,7 +151,7 @@ export async function findRelevantAuditLogEntry(
const cutoffTS = Date.now() - 1000 * 60 * 2;
const relevantEntry = auditLogEntries.entries.find(entry => {
const relevantEntry = entries.find(entry => {
return entry.targetID === userId && entry.createdAt >= cutoffTS;
});