Note N2 · NyxBot
BOT · PANEL SYNC
Discord bot and web panel in sync: Redis pub/sub and targeted cache invalidation
- Sheet
- NYXBOT
- Topic
- Node.js · Redis pub/sub · discord.js
- Source
- utils/eventBus.js
- Date
- 2026-11-12
- Reading
- 3 min
Short answer: if the bot and the panel share a database, the problem is not the data but the cache. When a setting is saved in the panel, send the bot a "this server's this module changed" event over Redis pub/sub; the bot clears only that module's cache and reads fresh from the database next time. Open two separate Redis connections for publishing and subscribing, and stamp every event with a process id so a process never handles its own broadcast twice.
01Problem
NyxBot's settings are managed from a web panel: welcome message, auto-moderation, registration channels, economy, filtered words... If the bot reads them from the database on every message it slows down; if it keeps them in memory, changes made in the panel never reach it. The bot running on old settings after an admin hits "save" is the most common complaint.
02The event path
After the panel's API writes a setting to the database, it publishes a single event. The bot and the panel are separate processes; the channel between them is Redis pub/sub.
this.pubClient = new Redis(redisOptions);
this.subClient = new Redis(redisOptions);
this.originId = randomUUID();
broadcast(type, data = {}) {
const payload = { type, data, timestamp: Date.now(), origin: this.originId };
this.emit(type, data); // immediately, in the same process
this.pubClient.publish(this.channelName, JSON.stringify(payload));
}
- Two connections: a Redis connection in subscriber mode can't run other commands; publishing needs its own connection.
- Origin id: the event is handled in the same process right away and also goes to Redis. When its own event comes back from Redis, the
originfield gives it away and it is skipped. - Local event emitter: the class extends Node's
EventEmitter; the rest of the code listens witheventBus.on('config_updated', ...)without knowing about Redis.
03Targeted invalidation
The event says which module changed. The bot doesn't wipe its whole cache, only that module's:
eventBus.on('config_updated', ({ guildId, module }) => {
if (module === 'automod' || !module) db.clearAutoModConfigCache(guildId);
if (module === 'welcome' || !module) db.clearWelcomeConfigCache(guildId);
if (module === 'words' || !module) db.clearFilteredWordsCache(guildId);
if (module === 'prefix' || !module) invalidatePrefixCache(guildId);
// ... economy, forms, automations, achievements, anti-nuke ...
});
If no module is given, all of that server's caches are cleared. Some modules need more than invalidation: when stat channels or tag roles change, the bot updates their Discord counterparts right away.
The same event also triggers command sync: if a module was turned on or off, that server's slash command list is rewritten with the 5-second batching window (see N1).
04The other direction: bot to panel
The panel keeps caches too: member counts, open tickets, moderation records. The rule is the same in reverse: the panel's cache refreshes only when data actually changes. Every bot flow that writes to the database gets one line at the end; this helper never throws, so a failed broadcast doesn't break the real operation.
function dataChanged(type, guildId) {
try { broadcastEvent(type, guildId ? { guild_id: String(guildId) } : {}); } catch {}
}
// moderation_action, ticket_opened, member_join, member_leave, guild_list_changed ...
05When Redis is down
The connection is made with lazyConnect and retried with growing intervals (at most 3 seconds) when it drops; the subscribe command waits in the queue and
runs once the connection is up. If Redis can't be reached, the bot keeps working and logs once that "changes made in the panel won't reach the bot until Redis is back";
the same warning isn't printed on every retry.
→More notes from this project
—End of notewritten by MUHAMMEDPYZ