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

Cache invites when resolving them

This commit is contained in:
Dragory 2019-10-13 00:21:35 +03:00
parent 5e72693a3b
commit 9ac761b4eb
3 changed files with 20 additions and 16 deletions

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;
}
}