3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-13 21:35:02 +00:00

feat: add member cache; handle all role changes with RoleManagerPlugin; exit gracefully

This commit is contained in:
Dragory 2023-05-07 17:56:55 +03:00
parent fd60a09947
commit fa50110766
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
48 changed files with 755 additions and 264 deletions
backend/src/plugins/GuildMemberCache

View file

@ -0,0 +1,53 @@
import * as t from "io-ts";
import { GuildMemberCache } from "../../data/GuildMemberCache";
import { makeIoTsConfigParser, mapToPublicFn } from "../../pluginUtils";
import { SECONDS } from "../../utils";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { cancelDeletionOnMemberJoin } from "./events/cancelDeletionOnMemberJoin";
import { removeMemberCacheOnMemberLeave } from "./events/removeMemberCacheOnMemberLeave";
import { updateMemberCacheOnMemberUpdate } from "./events/updateMemberCacheOnMemberUpdate";
import { updateMemberCacheOnMessage } from "./events/updateMemberCacheOnMessage";
import { updateMemberCacheOnRoleChange } from "./events/updateMemberCacheOnRoleChange";
import { updateMemberCacheOnVoiceStateUpdate } from "./events/updateMemberCacheOnVoiceStateUpdate";
import { getCachedMemberData } from "./functions/getCachedMemberData";
import { GuildMemberCachePluginType } from "./types";
const PENDING_SAVE_INTERVAL = 30 * SECONDS;
export const GuildMemberCachePlugin = zeppelinGuildPlugin<GuildMemberCachePluginType>()({
name: "guild_member_cache",
showInDocs: false,
configParser: makeIoTsConfigParser(t.type({})),
events: [
updateMemberCacheOnMemberUpdate,
updateMemberCacheOnMessage,
updateMemberCacheOnVoiceStateUpdate,
updateMemberCacheOnRoleChange,
removeMemberCacheOnMemberLeave,
cancelDeletionOnMemberJoin,
],
public: {
getCachedMemberData: mapToPublicFn(getCachedMemberData),
},
beforeLoad(pluginData) {
pluginData.state.memberCache = GuildMemberCache.getGuildInstance(pluginData.guild.id);
// This won't leak memory... too much #trust
pluginData.state.initialUpdatedMembers = new Set();
},
afterLoad(pluginData) {
pluginData.state.saveInterval = setInterval(
() => pluginData.state.memberCache.savePendingUpdates(),
PENDING_SAVE_INTERVAL,
);
},
async beforeUnload(pluginData) {
clearInterval(pluginData.state.saveInterval);
await pluginData.state.memberCache.savePendingUpdates();
},
});