Centralize periodic checks for mutes, tempbans, vcalerts, reminders, and scheduled posts
This should result in a significant performance improvement. The new method is also more precise than the old one, allowing the aforementioned checks to be performed with second-precision.
This commit is contained in:
parent
c84d1a0be1
commit
c7751a9da1
55 changed files with 883 additions and 366 deletions
|
@ -9,8 +9,8 @@ import { GuildBanRemoveAlertsEvt } from "./events/BanRemoveAlertsEvt";
|
|||
import { VoiceStateUpdateAlertEvt } from "./events/SendAlertsEvts";
|
||||
import { ConfigSchema, LocateUserPluginType } from "./types";
|
||||
import { fillActiveAlertsList } from "./utils/fillAlertsList";
|
||||
import { outdatedAlertsLoop } from "./utils/outdatedLoop";
|
||||
import Timeout = NodeJS.Timeout;
|
||||
import { onGuildEvent } from "../../data/GuildEvents";
|
||||
import { clearExpiredAlert } from "./utils/clearExpiredAlert";
|
||||
|
||||
const defaultOptions: PluginOptions<LocateUserPluginType> = {
|
||||
config: {
|
||||
|
@ -61,18 +61,17 @@ export const LocateUserPlugin = zeppelinGuildPlugin<LocateUserPluginType>()({
|
|||
const { state, guild } = pluginData;
|
||||
|
||||
state.alerts = GuildVCAlerts.getGuildInstance(guild.id);
|
||||
state.outdatedAlertsTimeout = null;
|
||||
state.usersWithAlerts = [];
|
||||
state.unloaded = false;
|
||||
},
|
||||
|
||||
afterLoad(pluginData) {
|
||||
outdatedAlertsLoop(pluginData);
|
||||
pluginData.state.unregisterGuildEventListener = onGuildEvent(pluginData.guild.id, "expiredVCAlert", (alert) =>
|
||||
clearExpiredAlert(pluginData, alert),
|
||||
);
|
||||
fillActiveAlertsList(pluginData);
|
||||
},
|
||||
|
||||
beforeUnload(pluginData) {
|
||||
clearTimeout(pluginData.state.outdatedAlertsTimeout as Timeout);
|
||||
pluginData.state.unloaded = true;
|
||||
pluginData.state.unregisterGuildEventListener();
|
||||
},
|
||||
});
|
||||
|
|
|
@ -4,6 +4,7 @@ import { commandTypeHelpers as ct } from "../../../commandTypes";
|
|||
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
|
||||
import { MINUTES, SECONDS } from "../../../utils";
|
||||
import { locateUserCmd } from "../types";
|
||||
import { registerExpiringVCAlert } from "../../../data/loops/expiringVCAlertsLoop";
|
||||
|
||||
export const FollowCmd = locateUserCmd({
|
||||
trigger: ["follow", "f"],
|
||||
|
@ -30,7 +31,7 @@ export const FollowCmd = locateUserCmd({
|
|||
return;
|
||||
}
|
||||
|
||||
await pluginData.state.alerts.add(
|
||||
const alert = await pluginData.state.alerts.add(
|
||||
msg.author.id,
|
||||
args.member.id,
|
||||
msg.channel.id,
|
||||
|
@ -38,6 +39,8 @@ export const FollowCmd = locateUserCmd({
|
|||
body,
|
||||
active,
|
||||
);
|
||||
registerExpiringVCAlert(alert);
|
||||
|
||||
if (!pluginData.state.usersWithAlerts.includes(args.member.id)) {
|
||||
pluginData.state.usersWithAlerts.push(args.member.id);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import { commandTypeHelpers as ct } from "../../../commandTypes";
|
|||
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
|
||||
import { createChunkedMessage, sorter } from "../../../utils";
|
||||
import { locateUserCmd } from "../types";
|
||||
import { clearExpiringVCAlert } from "../../../data/loops/expiringVCAlertsLoop";
|
||||
|
||||
export const ListFollowCmd = locateUserCmd({
|
||||
trigger: ["follows", "fs"],
|
||||
|
@ -50,6 +51,7 @@ export const DeleteFollowCmd = locateUserCmd({
|
|||
}
|
||||
|
||||
const toDelete = alerts[args.num - 1];
|
||||
clearExpiringVCAlert(toDelete);
|
||||
await pluginData.state.alerts.delete(toDelete.id);
|
||||
|
||||
sendSuccessMessage(pluginData, msg.channel, "Alert deleted");
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { locateUserEvt } from "../types";
|
||||
import { clearExpiringVCAlert } from "../../../data/loops/expiringVCAlertsLoop";
|
||||
|
||||
export const GuildBanRemoveAlertsEvt = locateUserEvt({
|
||||
event: "guildBanAdd",
|
||||
|
@ -6,6 +7,7 @@ export const GuildBanRemoveAlertsEvt = locateUserEvt({
|
|||
async listener(meta) {
|
||||
const alerts = await meta.pluginData.state.alerts.getAlertsByUserId(meta.args.ban.user.id);
|
||||
alerts.forEach((alert) => {
|
||||
clearExpiringVCAlert(alert);
|
||||
meta.pluginData.state.alerts.delete(alert.id);
|
||||
});
|
||||
},
|
||||
|
|
|
@ -13,9 +13,8 @@ export interface LocateUserPluginType extends BasePluginType {
|
|||
config: TConfigSchema;
|
||||
state: {
|
||||
alerts: GuildVCAlerts;
|
||||
outdatedAlertsTimeout: Timeout | null;
|
||||
usersWithAlerts: string[];
|
||||
unloaded: boolean;
|
||||
unregisterGuildEventListener: () => void;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { LocateUserPluginType } from "../types";
|
||||
import { removeUserIdFromActiveAlerts } from "./removeUserIdFromActiveAlerts";
|
||||
import { VCAlert } from "../../../data/entities/VCAlert";
|
||||
|
||||
export async function clearExpiredAlert(pluginData: GuildPluginData<LocateUserPluginType>, alert: VCAlert) {
|
||||
await pluginData.state.alerts.delete(alert.id);
|
||||
await removeUserIdFromActiveAlerts(pluginData, alert.user_id);
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { SECONDS } from "../../../utils";
|
||||
import { LocateUserPluginType } from "../types";
|
||||
import { removeUserIdFromActiveAlerts } from "./removeUserIdFromActiveAlerts";
|
||||
|
||||
const ALERT_LOOP_TIME = 30 * SECONDS;
|
||||
|
||||
export async function outdatedAlertsLoop(pluginData: GuildPluginData<LocateUserPluginType>) {
|
||||
const outdatedAlerts = await pluginData.state.alerts.getOutdatedAlerts();
|
||||
|
||||
for (const alert of outdatedAlerts) {
|
||||
await pluginData.state.alerts.delete(alert.id);
|
||||
await removeUserIdFromActiveAlerts(pluginData, alert.user_id);
|
||||
}
|
||||
|
||||
if (!pluginData.state.unloaded) {
|
||||
pluginData.state.outdatedAlertsTimeout = setTimeout(() => outdatedAlertsLoop(pluginData), ALERT_LOOP_TIME);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue