perf: use a memory cache for Slowmode
This commit is contained in:
parent
d09d6b776a
commit
121628e6b1
5 changed files with 30 additions and 4 deletions
|
@ -42,7 +42,11 @@ export const SlowmodePlugin = zeppelinGuildPlugin<SlowmodePluginType>()({
|
|||
prettyName: "Slowmode",
|
||||
},
|
||||
|
||||
dependencies: () => [LogsPlugin],
|
||||
// prettier-ignore
|
||||
dependencies: () => [
|
||||
LogsPlugin,
|
||||
],
|
||||
|
||||
configSchema: ConfigSchema,
|
||||
defaultOptions,
|
||||
|
||||
|
@ -61,6 +65,7 @@ export const SlowmodePlugin = zeppelinGuildPlugin<SlowmodePluginType>()({
|
|||
state.slowmodes = GuildSlowmodes.getGuildInstance(guild.id);
|
||||
state.savedMessages = GuildSavedMessages.getGuildInstance(guild.id);
|
||||
state.logs = new GuildLogs(guild.id);
|
||||
state.channelSlowmodeCache = new Map();
|
||||
},
|
||||
|
||||
afterLoad(pluginData) {
|
||||
|
|
|
@ -144,7 +144,12 @@ export const SlowmodeSetCmd = slowmodeCmd({
|
|||
});
|
||||
}
|
||||
|
||||
// Set bot-maintained slowmode
|
||||
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);
|
||||
|
|
|
@ -3,6 +3,7 @@ import { BasePluginType, typedGuildCommand, typedGuildEventListener } from "knub
|
|||
import { GuildLogs } from "../../data/GuildLogs";
|
||||
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
||||
import { GuildSlowmodes } from "../../data/GuildSlowmodes";
|
||||
import { SlowmodeChannel } from "../../data/entities/SlowmodeChannel";
|
||||
|
||||
export const ConfigSchema = t.type({
|
||||
use_native_slowmode: t.boolean,
|
||||
|
@ -20,6 +21,7 @@ export interface SlowmodePluginType extends BasePluginType {
|
|||
logs: GuildLogs;
|
||||
clearInterval: NodeJS.Timeout;
|
||||
serverLogs: GuildLogs;
|
||||
channelSlowmodeCache: Map<string, SlowmodeChannel | null>;
|
||||
|
||||
onMessageCreateFn;
|
||||
};
|
||||
|
|
|
@ -24,5 +24,8 @@ export async function disableBotSlowmodeForChannel(
|
|||
}
|
||||
}
|
||||
|
||||
// Clear cache
|
||||
pluginData.state.channelSlowmodeCache.set(channel.id, null);
|
||||
|
||||
return { failedUsers };
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import { LogsPlugin } from "../../Logs/LogsPlugin";
|
|||
import { BOT_SLOWMODE_PERMISSIONS } from "../requiredPermissions";
|
||||
import { SlowmodePluginType } from "../types";
|
||||
import { applyBotSlowmodeToUserId } from "./applyBotSlowmodeToUserId";
|
||||
import { SlowmodeChannel } from "../../../data/entities/SlowmodeChannel";
|
||||
|
||||
export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePluginType>, msg: SavedMessage) {
|
||||
if (msg.is_bot) return;
|
||||
|
@ -22,8 +23,16 @@ export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePlugin
|
|||
if (thisMsgLock.interrupted) return;
|
||||
|
||||
// Check if this channel even *has* a bot-maintained slowmode
|
||||
const channelSlowmode = await pluginData.state.slowmodes.getChannelSlowmode(channel.id);
|
||||
if (!channelSlowmode) return thisMsgLock.unlock();
|
||||
let channelSlowmode: SlowmodeChannel | null;
|
||||
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
|
||||
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,
|
||||
member,
|
||||
});
|
||||
if (!isAffected) return thisMsgLock.unlock();
|
||||
if (!isAffected) {
|
||||
return thisMsgLock.unlock();
|
||||
}
|
||||
|
||||
// Make sure we have the appropriate permissions to manage this slowmode
|
||||
const me = pluginData.guild.members.cache.get(pluginData.client.user!.id)!;
|
||||
|
|
Loading…
Add table
Reference in a new issue