notification changes
This commit is contained in:
@@ -3,11 +3,17 @@
|
||||
* and alerts staff when thresholds are crossed.
|
||||
*/
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const { CONFIG } = require('../config');
|
||||
const { setCooldown, isOnCooldown } = require('./patternStore');
|
||||
const { CONFIG, parseThresholdString } = require('../config');
|
||||
const { shouldFireCooldownEscalating, clearEscalating } = require('./patternStore');
|
||||
|
||||
// channelId → { lastStaffMessageAt, unrespondedCount, lastAlertAt }
|
||||
const chatState = new Map();
|
||||
const chatMessageThresholdsMs = (CONFIG.NOTIFICATION_THRESHOLDS?.chat_messages || [])
|
||||
.map(parseThresholdString)
|
||||
.filter(n => Number.isFinite(n) && n > 0);
|
||||
const chatTimeThresholdsMs = (CONFIG.NOTIFICATION_THRESHOLDS?.chat_time || [])
|
||||
.map(parseThresholdString)
|
||||
.filter(n => Number.isFinite(n) && n > 0);
|
||||
|
||||
function initChatMonitoring(client) {
|
||||
for (const channelId of CONFIG.CHAT_ALERT_CHANNEL_IDS) {
|
||||
@@ -34,6 +40,8 @@ async function handleChatMessage(msg, client) {
|
||||
if (isStaff(msg.member)) {
|
||||
state.lastStaffMessageAt = new Date();
|
||||
state.unrespondedCount = 0;
|
||||
clearEscalating(`chat:messages:${msg.channel.id}`);
|
||||
clearEscalating(`chat:time:${msg.channel.id}`);
|
||||
} else {
|
||||
state.unrespondedCount++;
|
||||
}
|
||||
@@ -47,8 +55,7 @@ async function runChatAlertChecks(client) {
|
||||
// Message count threshold
|
||||
if (state.unrespondedCount >= CONFIG.CHAT_ALERT_MESSAGE_COUNT) {
|
||||
const cooldownKey = `chat:messages:${channelId}`;
|
||||
if (!isOnCooldown(cooldownKey, CONFIG.CHAT_ALERT_COOLDOWN_MINUTES)) {
|
||||
setCooldown(cooldownKey);
|
||||
if (shouldFireCooldownEscalating(cooldownKey, chatMessageThresholdsMs) !== null) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle('Chat needs attention')
|
||||
.setDescription(`<#${channelId}> has ${state.unrespondedCount} unresponded messages.`)
|
||||
@@ -66,8 +73,7 @@ async function runChatAlertChecks(client) {
|
||||
const hoursSinceStaff = (Date.now() - state.lastStaffMessageAt.getTime()) / 3600000;
|
||||
if (hoursSinceStaff >= CONFIG.CHAT_ALERT_HOURS_WITHOUT_RESPONSE && state.unrespondedCount > 0) {
|
||||
const cooldownKey = `chat:time:${channelId}`;
|
||||
if (!isOnCooldown(cooldownKey, CONFIG.CHAT_ALERT_COOLDOWN_MINUTES)) {
|
||||
setCooldown(cooldownKey);
|
||||
if (shouldFireCooldownEscalating(cooldownKey, chatTimeThresholdsMs) !== null) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle('Chat without staff response')
|
||||
.setDescription(`<#${channelId}> has had no staff response for ${Math.floor(hoursSinceStaff)} hour(s) with ${state.unrespondedCount} pending message(s).`)
|
||||
|
||||
Reference in New Issue
Block a user