Port GuildConfigReloader plugin
This commit is contained in:
parent
836fadf19e
commit
c467a740db
4 changed files with 65 additions and 1 deletions
|
@ -0,0 +1,18 @@
|
||||||
|
import { zeppelinPlugin } from "../ZeppelinPluginBlueprint";
|
||||||
|
import { GuildConfigReloaderPluginType } from "./types";
|
||||||
|
import { Configs } from "../../data/Configs";
|
||||||
|
import { reloadChangedGuilds } from "./functions/reloadChangedGuilds";
|
||||||
|
|
||||||
|
export const GuildConfigReloaderPlugin = zeppelinPlugin<GuildConfigReloaderPluginType>()("guild_config_reloader", {
|
||||||
|
async onLoad(pluginData) {
|
||||||
|
pluginData.state.guildConfigs = new Configs();
|
||||||
|
pluginData.state.highestConfigId = await pluginData.state.guildConfigs.getHighestId();
|
||||||
|
|
||||||
|
reloadChangedGuilds(pluginData);
|
||||||
|
},
|
||||||
|
|
||||||
|
onUnload(pluginData) {
|
||||||
|
clearTimeout(pluginData.state.nextCheckTimeout);
|
||||||
|
pluginData.state.unloaded = true;
|
||||||
|
},
|
||||||
|
});
|
|
@ -0,0 +1,24 @@
|
||||||
|
import { PluginData } from "knub";
|
||||||
|
import { GuildConfigReloaderPluginType } from "../types";
|
||||||
|
import { SECONDS } from "../../../utils";
|
||||||
|
|
||||||
|
const CHECK_INTERVAL = 1 * SECONDS;
|
||||||
|
|
||||||
|
export async function reloadChangedGuilds(pluginData: PluginData<GuildConfigReloaderPluginType>) {
|
||||||
|
if (pluginData.state.unloaded) return;
|
||||||
|
|
||||||
|
const changedConfigs = await pluginData.state.guildConfigs.getActiveLargerThanId(pluginData.state.highestConfigId);
|
||||||
|
for (const item of changedConfigs) {
|
||||||
|
if (!item.key.startsWith("guild-")) continue;
|
||||||
|
|
||||||
|
const guildId = item.key.slice("guild-".length);
|
||||||
|
console.log(`Config changed, reloading guild ${guildId}`);
|
||||||
|
await pluginData.getKnubInstance().reloadGuild(guildId);
|
||||||
|
|
||||||
|
if (item.id > pluginData.state.highestConfigId) {
|
||||||
|
pluginData.state.highestConfigId = item.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pluginData.state.nextCheckTimeout = setTimeout(() => reloadChangedGuilds(pluginData), CHECK_INTERVAL);
|
||||||
|
}
|
18
backend/src/plugins/GuildConfigReloader/types.ts
Normal file
18
backend/src/plugins/GuildConfigReloader/types.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { BasePluginType } from "knub";
|
||||||
|
import { GuildMutes } from "../../data/GuildMutes";
|
||||||
|
import { GuildCases } from "../../data/GuildCases";
|
||||||
|
import { GuildLogs } from "../../data/GuildLogs";
|
||||||
|
import { GuildArchives } from "../../data/GuildArchives";
|
||||||
|
import { TConfigSchema } from "../Mutes/types";
|
||||||
|
import { Configs } from "../../data/Configs";
|
||||||
|
import Timeout = NodeJS.Timeout;
|
||||||
|
|
||||||
|
export interface GuildConfigReloaderPluginType extends BasePluginType {
|
||||||
|
config: TConfigSchema;
|
||||||
|
state: {
|
||||||
|
guildConfigs: Configs;
|
||||||
|
unloaded: boolean;
|
||||||
|
highestConfigId: number;
|
||||||
|
nextCheckTimeout: Timeout;
|
||||||
|
};
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ import { RemindersPlugin } from "./Reminders/RemindersPlugin";
|
||||||
import { UsernameSaverPlugin } from "./UsernameSaver/UsernameSaverPlugin";
|
import { UsernameSaverPlugin } from "./UsernameSaver/UsernameSaverPlugin";
|
||||||
import { WelcomeMessagePlugin } from "./WelcomeMessage/WelcomeMessagePlugin";
|
import { WelcomeMessagePlugin } from "./WelcomeMessage/WelcomeMessagePlugin";
|
||||||
import { PingableRolesPlugin } from "./PingableRoles/PingableRolesPlugin";
|
import { PingableRolesPlugin } from "./PingableRoles/PingableRolesPlugin";
|
||||||
|
import { GuildConfigReloaderPlugin } from "./GuildConfigReloader/GuildConfigReloaderPlugin";
|
||||||
|
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
||||||
|
@ -24,4 +25,7 @@ export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
||||||
WelcomeMessagePlugin,
|
WelcomeMessagePlugin,
|
||||||
];
|
];
|
||||||
|
|
||||||
export const globalPlugins = [];
|
// prettier-ignore
|
||||||
|
export const globalPlugins = [
|
||||||
|
GuildConfigReloaderPlugin,
|
||||||
|
];
|
||||||
|
|
Loading…
Add table
Reference in a new issue