3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-23 17:45:03 +00:00
This commit is contained in:
metal 2021-09-21 10:45:49 +00:00 committed by GitHub
parent 744b9273bb
commit 00bc3ca275
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 99 additions and 99 deletions

View file

@ -16,7 +16,8 @@ import { readContactMethodsFromArgs } from "../functions/readContactMethodsFromA
import { modActionsCmd } from "../types";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import moment from "moment";
import { addTimer, removeTimer, removeTimerByUserId } from "../functions/outdatedTempbansLoop";
import { clearTempBan } from "../functions/outdatedTempbansLoop";
import { addTimer, removeTimer, removeTimerByUserId } from "src/utils/timers";
const opts = {
mod: ct.member({ option: true }),
@ -96,13 +97,18 @@ export const BanCmd = modActionsCmd({
if (existingTempban) {
pluginData.state.tempbans.updateExpiryTime(user.id, time, mod.id);
removeTimer(pluginData, existingTempban);
addTimer(pluginData, {
const newBanObj = {
...existingTempban,
expires_at: moment().utc().add(time, "ms").format("YYYY-MM-DD HH:mm:ss"),
};
addTimer(pluginData, newBanObj, async () => {
await clearTempBan(pluginData, newBanObj);
});
} else {
const tempban = await pluginData.state.tempbans.addTempban(user.id, time, mod.id);
addTimer(pluginData, tempban);
addTimer(pluginData, tempban, async () => {
await clearTempBan(pluginData, tempban);
});
}
} else if (existingTempban) {
pluginData.state.tempbans.clear(user.id);

View file

@ -10,7 +10,7 @@ import { formatReasonWithAttachments } from "../functions/formatReasonWithAttach
import { ignoreEvent } from "../functions/ignoreEvent";
import { IgnoredEventType, modActionsCmd } from "../types";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { removeTimerByUserId } from "../functions/outdatedTempbansLoop";
import { removeTimerByUserId } from "src/utils/timers";
const opts = {
mod: ct.member({ option: true }),

View file

@ -6,21 +6,15 @@ import { CaseTypes } from "../../../data/CaseTypes";
import { LogType } from "../../../data/LogType";
import { logger } from "../../../logger";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
import {
createUserNotificationError,
notifyUser,
resolveUser,
stripObjectToScalars,
ucfirst,
UserNotificationResult,
} from "../../../utils";
import { createUserNotificationError, notifyUser, resolveUser, ucfirst, UserNotificationResult } from "../../../utils";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { BanOptions, BanResult, IgnoredEventType, ModActionsPluginType } from "../types";
import { getDefaultContactMethods } from "./getDefaultContactMethods";
import { ignoreEvent } from "./ignoreEvent";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { addTimer, removeTimer } from "./outdatedTempbansLoop";
import { clearTempBan } from "./outdatedTempbansLoop";
import moment from "moment";
import { addTimer, removeTimer } from "src/utils/timers";
/**
* Ban the specified user id, whether or not they're actually on the server at the time. Generates a case.
@ -112,13 +106,18 @@ export async function banUserId(
if (existingTempban) {
pluginData.state.tempbans.updateExpiryTime(user.id, banTime, banOptions.modId ?? selfId);
removeTimer(pluginData, existingTempban);
addTimer(pluginData, {
const newBanObj = {
...existingTempban,
expires_at: moment().utc().add(banTime, "ms").format("YYYY-MM-DD HH:mm:ss"),
};
addTimer(pluginData, newBanObj, async () => {
await clearTempBan(pluginData, newBanObj);
});
} else {
const tempban = await pluginData.state.tempbans.addTempban(user.id, banTime, banOptions.modId ?? selfId);
addTimer(pluginData, tempban);
addTimer(pluginData, tempban, async () => {
await clearTempBan(pluginData, tempban);
});
}
}

View file

@ -13,51 +13,10 @@ import { ignoreEvent } from "./ignoreEvent";
import { isBanned } from "./isBanned";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { Tempban } from "src/data/entities/Tempban";
import { ExpiringTimer } from "src/utils/timers";
import { addTimer } from "src/utils/timers";
const LOAD_LESS_THAN_MIN_COUNT = 60 * MINUTES;
export function addTimer(pluginData: GuildPluginData<ModActionsPluginType>, tempban: Tempban) {
const existingMute = pluginData.state.timers.find(
(tm) => tm.options.key === tempban.user_id && tm.options.guildId === tempban.guild_id && !tm.done,
); // for future-proof when you do global events
if (!existingMute && tempban.expires_at) {
const exp = moment(tempban.expires_at!).toDate().getTime() - moment.utc().toDate().getTime();
const newTimer = new ExpiringTimer({
key: tempban.user_id,
guildId: tempban.guild_id,
plugin: "tempban",
expiry: exp,
callback: async () => {
await clearTempBan(pluginData, tempban);
},
});
pluginData.state.timers.push(newTimer);
}
}
export function removeTimer(pluginData: GuildPluginData<ModActionsPluginType>, tempban: Tempban) {
const existingMute = pluginData.state.timers.findIndex(
(tm) => tm.options.key === tempban.user_id && tm.options.guildId === tempban.guild_id && !tm.done,
);
if (existingMute) {
const tm = pluginData.state.timers[existingMute];
tm.clear();
tm.done = true;
pluginData.state.timers.splice(existingMute, 1);
}
}
export function removeTimerByUserId(pluginData: GuildPluginData<ModActionsPluginType>, user_id: Snowflake) {
const existingMute = pluginData.state.timers.findIndex((tm) => tm.options.key === user_id && !tm.done);
if (existingMute) {
const tm = pluginData.state.timers[existingMute];
tm.clear();
tm.done = true;
pluginData.state.timers.splice(existingMute, 1);
}
}
export async function loadExpiringTimers(pluginData: GuildPluginData<ModActionsPluginType>) {
const now = moment.utc().toDate().getTime();
pluginData.state.timers = pluginData.state.timers.filter((tm) => !tm.done || !tm.timeout);
@ -72,7 +31,9 @@ export async function loadExpiringTimers(pluginData: GuildPluginData<ModActionsP
if (expires <= now) continue; // exclude expired mutes, just in case
if (expires > now + LOAD_LESS_THAN_MIN_COUNT) continue; // exclude timers that are expiring in over 180 mins
addTimer(pluginData, tempban);
addTimer(pluginData, tempban, async () => {
await clearTempBan(pluginData, tempban);
});
}
for (const tempban of expiredBans) {

View file

@ -3,43 +3,12 @@ import { MINUTES, resolveMember, UnknownUser, verboseUserMention } from "../../.
import { memberRolesLock } from "../../../utils/lockNameHelpers";
import { MutesPluginType } from "../types";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { ExpiringTimer } from "src/utils/timers";
import { Mute } from "src/data/entities/Mute";
import moment from "moment";
import { addTimer } from "src/utils/timers";
const LOAD_LESS_THAN_MIN_COUNT = 60 * MINUTES;
export function addTimer(pluginData: GuildPluginData<MutesPluginType>, mute: Mute) {
const existingMute = pluginData.state.timers.find(
(tm) => tm.options.key === mute.user_id && tm.options.guildId === mute.guild_id && !tm.done,
); // for future-proof when you do global events
if (!existingMute && mute.expires_at) {
const exp = moment(mute.expires_at!).toDate().getTime() - moment.utc().toDate().getTime();
const newTimer = new ExpiringTimer({
key: mute.user_id,
guildId: mute.guild_id,
plugin: "mutes",
expiry: exp,
callback: async () => {
await clearExpiredMute(pluginData, mute);
},
});
pluginData.state.timers.push(newTimer);
}
}
export function removeTimer(pluginData: GuildPluginData<MutesPluginType>, mute: Mute) {
const existingMute = pluginData.state.timers.findIndex(
(tm) => tm.options.key === mute.user_id && tm.options.guildId === mute.guild_id && !tm.done,
);
if (existingMute) {
const tm = pluginData.state.timers[existingMute];
tm.clear();
tm.done = true;
pluginData.state.timers.splice(existingMute, 1);
}
}
export async function loadExpiringTimers(pluginData: GuildPluginData<MutesPluginType>) {
const now = moment.utc().toDate().getTime();
pluginData.state.timers = pluginData.state.timers.filter((tm) => !tm.done || !tm.timeout);
@ -54,7 +23,9 @@ export async function loadExpiringTimers(pluginData: GuildPluginData<MutesPlugin
if (expires <= now) continue; // exclude expired mutes, just in case
if (expires > now + LOAD_LESS_THAN_MIN_COUNT) continue; // exclude timers that are expiring in over 180 mins
addTimer(pluginData, mute);
addTimer(pluginData, mute, async () => {
await clearExpiredMute(pluginData, mute);
});
}
for (const mute of expiredMutes) {

View file

@ -19,8 +19,9 @@ import {
import { muteLock } from "../../../utils/lockNameHelpers";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { MuteOptions, MutesPluginType } from "../types";
import { addTimer, removeTimer } from "./clearExpiredMutes";
import { clearExpiredMute } from "./clearExpiredMutes";
import moment from "moment";
import { addTimer, removeTimer } from "src/utils/timers";
export async function muteUser(
pluginData: GuildPluginData<MutesPluginType>,
@ -142,13 +143,18 @@ export async function muteUser(
}
await pluginData.state.mutes.updateExpiryTime(user.id, muteTime, rolesToRestore);
removeTimer(pluginData, existingMute);
addTimer(pluginData, {
const newMuteObj = {
...existingMute,
expires_at: moment().utc().add(muteTime, "ms").format("YYYY-MM-DD HH:mm:ss"),
};
addTimer(pluginData, newMuteObj, async () => {
await clearExpiredMute(pluginData, newMuteObj);
});
} else {
const mute = await pluginData.state.mutes.addMute(user.id, muteTime, rolesToRestore);
addTimer(pluginData, mute);
addTimer(pluginData, mute, async () => {
await clearExpiredMute(pluginData, mute);
});
}
const template = existingMute

View file

@ -1,9 +1,7 @@
import { Snowflake } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import { userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
import { CaseTypes } from "../../../data/CaseTypes";
import { LogType } from "../../../data/LogType";
import { resolveMember, resolveUser } from "../../../utils";
import { memberRolesLock } from "../../../utils/lockNameHelpers";
import { CasesPlugin } from "../../Cases/CasesPlugin";
@ -11,8 +9,9 @@ import { CaseArgs } from "../../Cases/types";
import { MutesPluginType, UnmuteResult } from "../types";
import { memberHasMutedRole } from "./memberHasMutedRole";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { addTimer, removeTimer } from "./clearExpiredMutes";
import { clearExpiredMute } from "./clearExpiredMutes";
import moment from "moment";
import { addTimer, removeTimer } from "src/utils/timers";
export async function unmuteUser(
pluginData: GuildPluginData<MutesPluginType>,
@ -31,13 +30,18 @@ export async function unmuteUser(
// Schedule timed unmute (= just set the mute's duration)
if (!existingMute) {
const mute = await pluginData.state.mutes.addMute(userId, unmuteTime);
addTimer(pluginData, mute);
addTimer(pluginData, mute, async () => {
await clearExpiredMute(pluginData, mute);
});
} else {
await pluginData.state.mutes.updateExpiryTime(userId, unmuteTime);
removeTimer(pluginData, existingMute);
addTimer(pluginData, {
const newMuteObj = {
...existingMute,
expires_at: moment().utc().add(unmuteTime, "ms").format("YYYY-MM-DD HH:mm:ss"),
};
addTimer(pluginData, newMuteObj, async () => {
await clearExpiredMute(pluginData, newMuteObj);
});
}
} else {

View file

@ -1,4 +1,11 @@
import { Snowflake } from "discord-api-types";
import { GuildPluginData } from "knub";
import moment from "moment";
import { Mute } from "src/data/entities/Mute";
import { Tempban } from "src/data/entities/Tempban";
import { ModActionsPluginType } from "src/plugins/ModActions/types";
import { MutesPluginType } from "src/plugins/Mutes/types";
import { RemindersPluginType } from "src/plugins/Reminders/types";
type TimerCallback = (key: string, expiry: number) => void;
@ -38,3 +45,49 @@ export class ExpiringTimer {
this.init();
}
}
export function addTimer(
pluginData: GuildPluginData<MutesPluginType | ModActionsPluginType>,
obj: Mute | Tempban,
callback: () => void,
) {
const existing = pluginData.state.timers.find(
(tm) => tm.options.key === obj.user_id && tm.options.guildId === obj.guild_id && !tm.done,
); // for future-proof when you do global events
if (!existing && obj.expires_at) {
const exp = moment(obj.expires_at!).toDate().getTime() - moment.utc().toDate().getTime();
const newTimer = new ExpiringTimer({
key: obj.user_id,
guildId: obj.guild_id,
plugin: "mutes",
expiry: exp,
callback,
});
pluginData.state.timers.push(newTimer);
}
}
export function removeTimer(pluginData: GuildPluginData<MutesPluginType | ModActionsPluginType>, obj: Mute | Tempban) {
const existing = pluginData.state.timers.findIndex(
(tm) => tm.options.key === obj.user_id && tm.options.guildId === obj.guild_id && !tm.done,
);
if (existing) {
const tm = pluginData.state.timers[existing];
tm.clear();
tm.done = true;
pluginData.state.timers.splice(existing, 1);
}
}
export function removeTimerByUserId(
pluginData: GuildPluginData<MutesPluginType | ModActionsPluginType>,
user_id: Snowflake,
) {
const existing = pluginData.state.timers.findIndex((tm) => tm.options.key === user_id && !tm.done);
if (existing) {
const tm = pluginData.state.timers[existing];
tm.clear();
tm.done = true;
pluginData.state.timers.splice(existing, 1);
}
}