remod hook now persists through restarts

This commit is contained in:
2025-09-09 00:06:10 +02:00
parent ba2a520369
commit 61da0fd6e0
2 changed files with 20 additions and 5 deletions

View File

@@ -5,6 +5,9 @@ import { addAdmin } from "lib/admins";
import logger from "lib/logger";
import { addInvuln } from "lib/invuln";
import { redis } from "bun";
import { remodMod, timeoutDuration } from "lib/timeout";
import User from "user";
import { buildTimeString } from "lib/dateManager";
const CHATTERINTENTS = ["user:read:chat", "user:write:chat", "user:bot"];
const STREAMERINTENTS = ["channel:bot", "user:read:chat", "moderation:read", "channel:manage:moderators", "moderator:manage:banned_users", "bits:read", "channel:moderate"];
@@ -33,20 +36,30 @@ export const streamerUsers = [chatterId, streamerId];
streamerUsers.forEach(async id => await Promise.all([addAdmin(id), addInvuln(id)]));
const banned = await streamerApi.moderation.getBannedUsers(streamerId).then(a => a.data);
banned.forEach(async ban => {
for (const ban of banned) {
await redis.set(`user:${ban.userId}:timeout`, '1');
const banlength = ban.expiryDate;
if (banlength) {
redis.expire(`user:${ban.userId}:timeout`, Math.floor((ban.expiryDate.getTime() - Date.now()) / 1000) + 1);
logger.info(`Set the timeout of ${ban.userDisplayName} in the Redis/Valkey database.`);
};
});
};
const mods = await streamerApi.moderation.getModerators(streamerId).then(a => a.data);
mods.forEach(async mod => {
for (const mod of mods) {
await redis.set(`user:${mod.userId}:mod`, '1');
logger.info(`Set the mod status of ${mod.userDisplayName} in the Redis/Valkey database.`);
});
};
const bannedmods = await redis.keys('user:*:remod').then(a => Array.from(a).map(b => b.slice(5, -6)));
for (const remod of bannedmods) {
const target = await User.initUserId(remod);
const durationdata = await timeoutDuration(target!);
let duration = 0;
if (durationdata) duration = Math.floor((durationdata * 1000 - Date.now()) / 1000);
remodMod(target!, duration);
logger.info(`Set the remod timer for ${target?.displayName} to ${duration} seconds.`);
};
const streamdata = await streamerApi.streams.getStreamByUserId(streamerId);
if (streamdata) await redis.set('streamIsLive', '1');

View File

@@ -25,6 +25,7 @@ export const timeout = async (user: User, reason: string, duration?: number): Pr
if (await redis.exists(`user:${user.id}:mod`)) {
if (!duration) duration = 60; // make sure that mods don't get perma-banned
await redis.set(`user:${user.id}:remod`, '1');
remodMod(user, duration);
await streamerApi.moderation.removeModerator(streamerId, user.id!);
};
@@ -43,7 +44,7 @@ export const timeout = async (user: User, reason: string, duration?: number): Pr
};
/** Give the target mod status back after timeout */
function remodMod(target: User, duration: number) {
export function remodMod(target: User, duration: number) {
setTimeout(async () => {
const bandata = await timeoutDuration(target);
if (bandata) { // If the target is still timed out, try again when new timeout expires
@@ -52,6 +53,7 @@ function remodMod(target: User, duration: number) {
} else {
try {
await streamerApi.moderation.addModerator(streamerId, target.id);
await redis.del(`user:${target.id}:remod`);
} catch (err) { }; // This triggers when the timeout got shortened. try/catch so no runtime error
};
}, duration + 3000); // callback gets called after duration of timeout + 3 seconds