mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-17 18:10:47 -04:00
fix discord bot pings and some of the session spam
This commit is contained in:
@@ -91,11 +91,12 @@ async function fetchChannel(id) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendToChannel(id, content, options = {}, allowedMentions = { parse: [] }) {
|
async function sendToChannel(id, content, options = {}, allowedMentions = { parse: [] }, sanitizeContent = true) {
|
||||||
const channel = await fetchChannel(id);
|
const channel = await fetchChannel(id);
|
||||||
if (!channel) return;
|
if (!channel) return;
|
||||||
try {
|
try {
|
||||||
await channel.send({ content: sanitizeMentions(content), allowedMentions, ...options });
|
const messageContent = sanitizeContent ? sanitizeMentions(content) : content;
|
||||||
|
await channel.send({ content: messageContent, allowedMentions, ...options });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn('Failed to send Discord message', { id, error: err.message });
|
logger.warn('Failed to send Discord message', { id, error: err.message });
|
||||||
}
|
}
|
||||||
@@ -260,7 +261,13 @@ async function announce({ channelId, content, pingRoleId, color, title, descript
|
|||||||
const prefix = pingRoleId ? `<@&${pingRoleId}> ` : '';
|
const prefix = pingRoleId ? `<@&${pingRoleId}> ` : '';
|
||||||
const embed = buildEmbed({ title, description, color });
|
const embed = buildEmbed({ title, description, color });
|
||||||
const allowedMentions = pingRoleId ? { roles: [pingRoleId], parse: [] } : { parse: [] };
|
const allowedMentions = pingRoleId ? { roles: [pingRoleId], parse: [] } : { parse: [] };
|
||||||
await sendToChannel(channelId, `${prefix}${content || ''}`.trim(), { embeds: [embed] }, allowedMentions);
|
await sendToChannel(
|
||||||
|
channelId,
|
||||||
|
`${prefix}${content || ''}`.trim(),
|
||||||
|
{ embeds: [embed] },
|
||||||
|
allowedMentions,
|
||||||
|
!pingRoleId, // keep role mention intact when pinging
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleBusEvent(event) {
|
function handleBusEvent(event) {
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ const { loadConfig } = require('../helpers/configLoader');
|
|||||||
const discordInvite = loadConfig().discord?.invite || null;
|
const discordInvite = loadConfig().discord?.invite || null;
|
||||||
logger.info('Discord invite loaded:', discordInvite ? 'present' : 'not configured');
|
logger.info('Discord invite loaded:', discordInvite ? 'present' : 'not configured');
|
||||||
|
|
||||||
|
const ACTIVITY_SYNC_COOLDOWN_MS = 3000;
|
||||||
|
let lastActivitySync = 0;
|
||||||
|
let pendingActivitySync = null;
|
||||||
|
|
||||||
function buildUserEntry(socket) {
|
function buildUserEntry(socket) {
|
||||||
if (!socket) return null;
|
if (!socket) return null;
|
||||||
const role = getRole(socket);
|
const role = getRole(socket);
|
||||||
@@ -107,7 +111,32 @@ turnEvents.on('activeDriver', () => {
|
|||||||
logger.info('Active driver change; syncing all clients');
|
logger.info('Active driver change; syncing all clients');
|
||||||
syncAll();
|
syncAll();
|
||||||
});
|
});
|
||||||
turnEvents.on('queue', () => {
|
turnEvents.on('queue', (event = {}) => {
|
||||||
|
const { reason } = event;
|
||||||
|
if (reason === 'activity') {
|
||||||
|
const now = Date.now();
|
||||||
|
const elapsed = now - lastActivitySync;
|
||||||
|
if (elapsed >= ACTIVITY_SYNC_COOLDOWN_MS) {
|
||||||
|
lastActivitySync = now;
|
||||||
|
logger.info('Turn activity; syncing all clients (immediate)');
|
||||||
|
syncAll();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!pendingActivitySync) {
|
||||||
|
const delay = ACTIVITY_SYNC_COOLDOWN_MS - elapsed;
|
||||||
|
pendingActivitySync = setTimeout(() => {
|
||||||
|
lastActivitySync = Date.now();
|
||||||
|
pendingActivitySync = null;
|
||||||
|
logger.info('Turn activity; syncing all clients (delayed)');
|
||||||
|
syncAll();
|
||||||
|
}, delay);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pendingActivitySync) {
|
||||||
|
clearTimeout(pendingActivitySync);
|
||||||
|
pendingActivitySync = null;
|
||||||
|
}
|
||||||
logger.info('Turn queue change; syncing all clients');
|
logger.info('Turn queue change; syncing all clients');
|
||||||
syncAll();
|
syncAll();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ function recordActivity(roverId, socketId) {
|
|||||||
idleDisarmed.set(roverId, true);
|
idleDisarmed.set(roverId, true);
|
||||||
clearTimeout(idleTimers.get(roverId));
|
clearTimeout(idleTimers.get(roverId));
|
||||||
idleDeadlines.delete(roverId);
|
idleDeadlines.delete(roverId);
|
||||||
turnEvents.emit('queue', { roverId });
|
turnEvents.emit('queue', { roverId, reason: 'activity' });
|
||||||
}
|
}
|
||||||
|
|
||||||
modeEvents.on('change', (mode) => {
|
modeEvents.on('change', (mode) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user