perf: use a memory cache for AutoReactions
This commit is contained in:
parent
31f18ba27f
commit
d09d6b776a
5 changed files with 24 additions and 3 deletions
|
@ -33,7 +33,11 @@ export const AutoReactionsPlugin = zeppelinGuildPlugin<AutoReactionsPluginType>(
|
|||
`),
|
||||
},
|
||||
|
||||
dependencies: () => [LogsPlugin],
|
||||
// prettier-ignore
|
||||
dependencies: () => [
|
||||
LogsPlugin,
|
||||
],
|
||||
|
||||
configSchema: ConfigSchema,
|
||||
defaultOptions,
|
||||
|
||||
|
@ -51,5 +55,6 @@ export const AutoReactionsPlugin = zeppelinGuildPlugin<AutoReactionsPluginType>(
|
|||
beforeLoad(pluginData) {
|
||||
pluginData.state.savedMessages = GuildSavedMessages.getGuildInstance(pluginData.guild.id);
|
||||
pluginData.state.autoReactions = GuildAutoReactions.getGuildInstance(pluginData.guild.id);
|
||||
pluginData.state.cache = new Map();
|
||||
},
|
||||
});
|
||||
|
|
|
@ -19,6 +19,7 @@ export const DisableAutoReactionsCmd = autoReactionsCmd({
|
|||
}
|
||||
|
||||
await pluginData.state.autoReactions.removeFromChannel(args.channelId);
|
||||
pluginData.state.cache.delete(args.channelId);
|
||||
sendSuccessMessage(pluginData, msg.channel, `Auto-reactions disabled in <#${args.channelId}>`);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -59,6 +59,7 @@ export const NewAutoReactionsCmd = autoReactionsCmd({
|
|||
}
|
||||
|
||||
await pluginData.state.autoReactions.set(args.channel.id, finalReactions);
|
||||
pluginData.state.cache.delete(args.channel.id);
|
||||
sendSuccessMessage(pluginData, msg.channel, `Auto-reactions set for <#${args.channel.id}>`);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,6 +6,7 @@ import { missingPermissionError } from "../../../utils/missingPermissionError";
|
|||
import { readChannelPermissions } from "../../../utils/readChannelPermissions";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { autoReactionsEvt } from "../types";
|
||||
import { AutoReaction } from "../../../data/entities/AutoReaction";
|
||||
|
||||
const p = Permissions.FLAGS;
|
||||
|
||||
|
@ -15,8 +16,19 @@ export const AddReactionsEvt = autoReactionsEvt({
|
|||
allowSelf: true,
|
||||
|
||||
async listener({ pluginData, args: { message } }) {
|
||||
const autoReaction = await pluginData.state.autoReactions.getForChannel(message.channel.id);
|
||||
if (!autoReaction) return;
|
||||
let autoReaction: AutoReaction | null = null;
|
||||
const lock = await pluginData.locks.acquire(`auto-reactions-${message.channel.id}`);
|
||||
if (pluginData.state.cache.has(message.channel.id)) {
|
||||
autoReaction = pluginData.state.cache.get(message.channel.id) ?? null;
|
||||
} else {
|
||||
autoReaction = (await pluginData.state.autoReactions.getForChannel(message.channel.id)) ?? null;
|
||||
pluginData.state.cache.set(message.channel.id, autoReaction);
|
||||
}
|
||||
lock.unlock();
|
||||
|
||||
if (!autoReaction) {
|
||||
return;
|
||||
}
|
||||
|
||||
const me = pluginData.guild.members.cache.get(pluginData.client.user!.id)!;
|
||||
if (me) {
|
||||
|
|
|
@ -3,6 +3,7 @@ import { BasePluginType, typedGuildCommand, typedGuildEventListener } from "knub
|
|||
import { GuildAutoReactions } from "../../data/GuildAutoReactions";
|
||||
import { GuildLogs } from "../../data/GuildLogs";
|
||||
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
||||
import { AutoReaction } from "../../data/entities/AutoReaction";
|
||||
|
||||
export const ConfigSchema = t.type({
|
||||
can_manage: t.boolean,
|
||||
|
@ -15,6 +16,7 @@ export interface AutoReactionsPluginType extends BasePluginType {
|
|||
logs: GuildLogs;
|
||||
savedMessages: GuildSavedMessages;
|
||||
autoReactions: GuildAutoReactions;
|
||||
cache: Map<string, AutoReaction | null>;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue