perf: use a memory cache for Slowmode

This commit is contained in:
Dragory 2021-11-02 23:38:02 +02:00
parent d09d6b776a
commit 121628e6b1
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
5 changed files with 30 additions and 4 deletions

View file

@ -42,7 +42,11 @@ export const SlowmodePlugin = zeppelinGuildPlugin<SlowmodePluginType>()({
prettyName: "Slowmode", prettyName: "Slowmode",
}, },
dependencies: () => [LogsPlugin], // prettier-ignore
dependencies: () => [
LogsPlugin,
],
configSchema: ConfigSchema, configSchema: ConfigSchema,
defaultOptions, defaultOptions,
@ -61,6 +65,7 @@ export const SlowmodePlugin = zeppelinGuildPlugin<SlowmodePluginType>()({
state.slowmodes = GuildSlowmodes.getGuildInstance(guild.id); state.slowmodes = GuildSlowmodes.getGuildInstance(guild.id);
state.savedMessages = GuildSavedMessages.getGuildInstance(guild.id); state.savedMessages = GuildSavedMessages.getGuildInstance(guild.id);
state.logs = new GuildLogs(guild.id); state.logs = new GuildLogs(guild.id);
state.channelSlowmodeCache = new Map();
}, },
afterLoad(pluginData) { afterLoad(pluginData) {

View file

@ -144,7 +144,12 @@ export const SlowmodeSetCmd = slowmodeCmd({
}); });
} }
// Set bot-maintained slowmode
await pluginData.state.slowmodes.setChannelSlowmode(channel.id, rateLimitSeconds); await pluginData.state.slowmodes.setChannelSlowmode(channel.id, rateLimitSeconds);
// Update cache
const slowmode = await pluginData.state.slowmodes.getChannelSlowmode(channel.id);
await pluginData.state.channelSlowmodeCache.set(channel.id, slowmode ?? null);
} }
const humanizedSlowmodeTime = humanizeDuration(args.time); const humanizedSlowmodeTime = humanizeDuration(args.time);

View file

@ -3,6 +3,7 @@ import { BasePluginType, typedGuildCommand, typedGuildEventListener } from "knub
import { GuildLogs } from "../../data/GuildLogs"; import { GuildLogs } from "../../data/GuildLogs";
import { GuildSavedMessages } from "../../data/GuildSavedMessages"; import { GuildSavedMessages } from "../../data/GuildSavedMessages";
import { GuildSlowmodes } from "../../data/GuildSlowmodes"; import { GuildSlowmodes } from "../../data/GuildSlowmodes";
import { SlowmodeChannel } from "../../data/entities/SlowmodeChannel";
export const ConfigSchema = t.type({ export const ConfigSchema = t.type({
use_native_slowmode: t.boolean, use_native_slowmode: t.boolean,
@ -20,6 +21,7 @@ export interface SlowmodePluginType extends BasePluginType {
logs: GuildLogs; logs: GuildLogs;
clearInterval: NodeJS.Timeout; clearInterval: NodeJS.Timeout;
serverLogs: GuildLogs; serverLogs: GuildLogs;
channelSlowmodeCache: Map<string, SlowmodeChannel | null>;
onMessageCreateFn; onMessageCreateFn;
}; };

View file

@ -24,5 +24,8 @@ export async function disableBotSlowmodeForChannel(
} }
} }
// Clear cache
pluginData.state.channelSlowmodeCache.set(channel.id, null);
return { failedUsers }; return { failedUsers };
} }

View file

@ -10,6 +10,7 @@ import { LogsPlugin } from "../../Logs/LogsPlugin";
import { BOT_SLOWMODE_PERMISSIONS } from "../requiredPermissions"; import { BOT_SLOWMODE_PERMISSIONS } from "../requiredPermissions";
import { SlowmodePluginType } from "../types"; import { SlowmodePluginType } from "../types";
import { applyBotSlowmodeToUserId } from "./applyBotSlowmodeToUserId"; import { applyBotSlowmodeToUserId } from "./applyBotSlowmodeToUserId";
import { SlowmodeChannel } from "../../../data/entities/SlowmodeChannel";
export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePluginType>, msg: SavedMessage) { export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePluginType>, msg: SavedMessage) {
if (msg.is_bot) return; if (msg.is_bot) return;
@ -22,8 +23,16 @@ export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePlugin
if (thisMsgLock.interrupted) return; if (thisMsgLock.interrupted) return;
// Check if this channel even *has* a bot-maintained slowmode // Check if this channel even *has* a bot-maintained slowmode
const channelSlowmode = await pluginData.state.slowmodes.getChannelSlowmode(channel.id); let channelSlowmode: SlowmodeChannel | null;
if (!channelSlowmode) return thisMsgLock.unlock(); if (pluginData.state.channelSlowmodeCache.has(channel.id)) {
channelSlowmode = pluginData.state.channelSlowmodeCache.get(channel.id) ?? null;
} else {
channelSlowmode = (await pluginData.state.slowmodes.getChannelSlowmode(channel.id)) ?? null;
pluginData.state.channelSlowmodeCache.set(channel.id, channelSlowmode);
}
if (!channelSlowmode) {
return thisMsgLock.unlock();
}
// Make sure this user is affected by the slowmode // Make sure this user is affected by the slowmode
const member = await resolveMember(pluginData.client, pluginData.guild, msg.user_id); const member = await resolveMember(pluginData.client, pluginData.guild, msg.user_id);
@ -32,7 +41,9 @@ export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePlugin
userId: msg.user_id, userId: msg.user_id,
member, member,
}); });
if (!isAffected) return thisMsgLock.unlock(); if (!isAffected) {
return thisMsgLock.unlock();
}
// Make sure we have the appropriate permissions to manage this slowmode // Make sure we have the appropriate permissions to manage this slowmode
const me = pluginData.guild.members.cache.get(pluginData.client.user!.id)!; const me = pluginData.guild.members.cache.get(pluginData.client.user!.id)!;