Chunk text-only log messages. Use a cooldown if running into Missing Access / Missing Permissions error during logging.
This commit is contained in:
parent
ac79eb09f5
commit
e6694f3751
3 changed files with 26 additions and 13 deletions
|
@ -1,4 +1,4 @@
|
|||
import { PluginOptions } from "knub";
|
||||
import { CooldownManager, PluginOptions } from "knub";
|
||||
import DefaultLogMessages from "../../data/DefaultLogMessages.json";
|
||||
import { GuildArchives } from "../../data/GuildArchives";
|
||||
import { GuildCases } from "../../data/GuildCases";
|
||||
|
@ -267,6 +267,7 @@ export const LogsPlugin = zeppelinGuildPlugin<LogsPluginType>()({
|
|||
state.cases = GuildCases.getGuildInstance(guild.id);
|
||||
|
||||
state.buffers = new Map();
|
||||
state.channelCooldowns = new CooldownManager();
|
||||
|
||||
state.regexRunner = getRegExpRunner(`guild-${pluginData.guild.id}`);
|
||||
},
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import * as t from "io-ts";
|
||||
import { z } from "zod";
|
||||
import { BasePluginType, typedGuildEventListener } from "knub";
|
||||
import { BasePluginType, CooldownManager, typedGuildEventListener } from "knub";
|
||||
import { GuildArchives } from "../../data/GuildArchives";
|
||||
import { GuildCases } from "../../data/GuildCases";
|
||||
import { GuildLogs } from "../../data/GuildLogs";
|
||||
|
@ -87,6 +87,7 @@ export interface LogsPluginType extends BasePluginType {
|
|||
logListener;
|
||||
|
||||
buffers: Map<string, MessageBuffer>;
|
||||
channelCooldowns: CooldownManager;
|
||||
|
||||
onMessageDeleteFn;
|
||||
onMessageDeleteBulkFn;
|
||||
|
|
|
@ -6,6 +6,7 @@ import { getLogMessage } from "./getLogMessage";
|
|||
import { TypedTemplateSafeValueContainer } from "../../../templateFormatter";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
import { MessageBuffer } from "../../../utils/MessageBuffer";
|
||||
import { createChunkedMessage, isDiscordAPIError, MINUTES } from "../../../utils";
|
||||
|
||||
const excludedUserProps = ["user", "member", "mod"];
|
||||
const excludedRoleProps = ["message.member.roles", "member.roles"];
|
||||
|
@ -82,6 +83,7 @@ export async function log<TLogType extends keyof ILogTypeData>(
|
|||
logChannelLoop: for (const [channelId, opts] of Object.entries(logChannels)) {
|
||||
const channel = pluginData.guild.channels.cache.get(channelId as Snowflake);
|
||||
if (!channel || !(channel instanceof TextChannel)) continue;
|
||||
if (pluginData.state.channelCooldowns.isOnCooldown(channelId)) continue;
|
||||
if (opts.include?.length && !opts.include.includes(typeStr)) continue;
|
||||
if (opts.exclude && opts.exclude.includes(typeStr)) continue;
|
||||
if (await shouldExclude(pluginData, opts, exclusionData)) continue;
|
||||
|
@ -102,17 +104,26 @@ export async function log<TLogType extends keyof ILogTypeData>(
|
|||
timeout: batchTime,
|
||||
consume: (part) => {
|
||||
const parse: MessageMentionTypes[] = pluginData.config.get().allow_user_mentions ? ["users"] : [];
|
||||
channel
|
||||
.send({
|
||||
...part,
|
||||
allowedMentions: { parse },
|
||||
})
|
||||
.catch((err) => {
|
||||
// tslint:disable-next-line:no-console
|
||||
console.warn(
|
||||
`Error while sending ${typeStr} log to ${pluginData.guild.id}/${channelId}: ${err.message}`,
|
||||
);
|
||||
});
|
||||
const promise =
|
||||
part.content && !part.embeds?.length
|
||||
? createChunkedMessage(channel, part.content, { parse })
|
||||
: channel.send({
|
||||
...part,
|
||||
allowedMentions: { parse },
|
||||
});
|
||||
promise.catch((err) => {
|
||||
if (isDiscordAPIError(err)) {
|
||||
// Missing Access / Missing Permissions
|
||||
// TODO: Show/log this somewhere
|
||||
if (err.code === 50001 || err.code === 50013) {
|
||||
pluginData.state.channelCooldowns.setCooldown(channelId, 2 * MINUTES);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:no-console
|
||||
console.warn(`Error while sending ${typeStr} log to ${pluginData.guild.id}/${channelId}: ${err.message}`);
|
||||
});
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue