mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Add temporary config auto-reload
This commit is contained in:
parent
51a9b2bfa5
commit
a517ca3906
2 changed files with 64 additions and 0 deletions
|
@ -28,6 +28,19 @@ export class Configs extends BaseRepository {
|
|||
});
|
||||
}
|
||||
|
||||
async getHighestId(): Promise<number> {
|
||||
const rows = await connection.query("SELECT MAX(id) AS highest_id FROM configs");
|
||||
return (rows.length && rows[0].highest_id) || 0;
|
||||
}
|
||||
|
||||
getActiveLargerThanId(id) {
|
||||
return this.configs
|
||||
.createQueryBuilder()
|
||||
.where("id > :id", { id })
|
||||
.andWhere("is_active = 1")
|
||||
.getMany();
|
||||
}
|
||||
|
||||
async hasConfig(key) {
|
||||
return (await this.getActiveByKey(key)) != null;
|
||||
}
|
||||
|
|
51
src/plugins/GuildConfigReloader.ts
Normal file
51
src/plugins/GuildConfigReloader.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import moment from "moment-timezone";
|
||||
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
||||
import { Configs } from "../data/Configs";
|
||||
import { logger } from "knub";
|
||||
import { GlobalZeppelinPlugin } from "./GlobalZeppelinPlugin";
|
||||
import { DBDateFormat } from "../utils";
|
||||
|
||||
const CHECK_INTERVAL = 1000;
|
||||
|
||||
/**
|
||||
* Temporary solution to reloading guilds when their config changes
|
||||
* And you know what they say about temporary solutions...
|
||||
*/
|
||||
export class GuildConfigReloader extends GlobalZeppelinPlugin {
|
||||
public static pluginName = "guild_config_reloader";
|
||||
protected guildConfigs: Configs;
|
||||
private unloaded = false;
|
||||
private highestConfigId;
|
||||
private nextCheckTimeout;
|
||||
|
||||
async onLoad() {
|
||||
this.guildConfigs = new Configs();
|
||||
|
||||
this.highestConfigId = await this.guildConfigs.getHighestId();
|
||||
this.reloadChangedGuilds();
|
||||
}
|
||||
|
||||
onUnload() {
|
||||
clearTimeout(this.nextCheckTimeout);
|
||||
this.unloaded = true;
|
||||
}
|
||||
|
||||
protected async reloadChangedGuilds() {
|
||||
if (this.unloaded) return;
|
||||
|
||||
const changedConfigs = await this.guildConfigs.getActiveLargerThanId(this.highestConfigId);
|
||||
for (const item of changedConfigs) {
|
||||
if (!item.key.startsWith("guild-")) continue;
|
||||
|
||||
const guildId = item.key.slice("guild-".length);
|
||||
logger.info(`Config changed, reloading guild ${guildId}`);
|
||||
await this.knub.reloadGuild(guildId);
|
||||
|
||||
if (item.id > this.highestConfigId) {
|
||||
this.highestConfigId = item.id;
|
||||
}
|
||||
}
|
||||
|
||||
this.nextCheckTimeout = setTimeout(() => this.reloadChangedGuilds(), CHECK_INTERVAL);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue