Cache invites when resolving them

This commit is contained in:
Dragory 2019-10-13 00:21:35 +03:00
parent 6ff8c8d290
commit e3d734801d
3 changed files with 20 additions and 16 deletions

View file

@ -16,13 +16,11 @@ import {
UnknownUser,
verboseChannelMention,
} from "../utils";
import { decorators as d } from "knub";
import { mergeConfig } from "knub/dist/configUtils";
import { Invite, Member, Message, TextChannel } from "eris";
import { Invite, Member, TextChannel } from "eris";
import escapeStringRegexp from "escape-string-regexp";
import { SimpleCache } from "../SimpleCache";
import { Queue } from "../Queue";
import Timeout = NodeJS.Timeout;
import { ModActionsPlugin } from "./ModActions";
import { MutesPlugin } from "./Mutes";
import { LogsPlugin } from "./Logs";
@ -34,6 +32,7 @@ import { GuildLogs } from "../data/GuildLogs";
import { SavedMessage } from "../data/entities/SavedMessage";
import moment from "moment-timezone";
import { renderTemplate } from "../templateFormatter";
import Timeout = NodeJS.Timeout;
type MessageInfo = { channelId: string; messageId: string };
@ -585,17 +584,7 @@ export class AutomodPlugin extends ZeppelinPlugin<TConfigSchema> {
}
}
const invites: Array<Invite | void> = await Promise.all(
uniqueInviteCodes.map(async code => {
if (inviteCache.has(code)) {
return inviteCache.get(code);
} else {
const invite = await this.bot.getInvite(code).catch(noop);
inviteCache.set(code, invite);
return invite;
}
}),
);
const invites: Array<Invite | null> = await Promise.all(uniqueInviteCodes.map(code => this.resolveInvite(code)));
for (const invite of invites) {
if (!invite) return true;

View file

@ -167,7 +167,7 @@ export class CensorPlugin extends ZeppelinPlugin<TConfigSchema> {
const inviteCodes = getInviteCodesInString(messageContent);
let invites: Invite[] = await Promise.all(inviteCodes.map(code => this.bot.getInvite(code).catch(() => null)));
let invites: Invite[] = await Promise.all(inviteCodes.map(code => this.resolveInvite(code)));
invites = invites.filter(v => !!v);

View file

@ -8,6 +8,7 @@ import {
deepKeyIntersect,
isSnowflake,
isUnicodeEmoji,
MINUTES,
resolveMember,
resolveUser,
resolveUserId,
@ -15,11 +16,12 @@ import {
trimIndents,
UnknownUser,
} from "../utils";
import { Member, User } from "eris";
import { Invite, Member, User } from "eris";
import DiscordRESTError from "eris/lib/errors/DiscordRESTError"; // tslint:disable-line
import { performance } from "perf_hooks";
import { decodeAndValidateStrict, StrictValidationError } from "../validatorUtils";
import { mergeConfig } from "knub/dist/configUtils";
import { SimpleCache } from "../SimpleCache";
const SLOW_RESOLVE_THRESHOLD = 1500;
@ -53,6 +55,8 @@ export function trimPluginDescription(str) {
return trimIndents(emptyLinesTrimmed, lastLineIndentation);
}
const inviteCache = new SimpleCache<Promise<Invite>>(10 * MINUTES, 200);
export class ZeppelinPlugin<TConfig extends {} = IBasePluginConfig> extends Plugin<TConfig> {
public static pluginInfo: PluginInfo;
public static showInDocs: boolean = true;
@ -255,4 +259,15 @@ export class ZeppelinPlugin<TConfig extends {} = IBasePluginConfig> extends Plug
return member;
}
async resolveInvite(code: string): Promise<Invite | null> {
if (inviteCache.has(code)) {
return inviteCache.get(code);
}
const promise = this.bot.getInvite(code).catch(() => null);
inviteCache.set(code, promise);
return promise;
}
}