Killmails are being posted to the wrong guildId/channelId despite the filter matching correctly.
- poll-r2.js polls for new killmails
- For each killmail, it queries MongoDB for matching subscriptions
- Each subscription document contains:
{ guildId, channelId, entityIds, labels, iskValue, advanced } - When a match is found, it extracts
channelIdandguildIdfrom the matched document - These are pushed to
discord_posts_queuealong with the killmail data - discord-post.js processes the queue and posts to the extracted
channelId
Multiple subscriptions can match the same killmail! This is normal and by design.
For example:
- Channel A (guildId: AAA, channelId: CCC1) subscribes to entity 12345
- Channel B (guildId: BBB, channelId: CCC2) subscribes to entity 12345
- When killmail with entity 12345 arrives, BOTH subscriptions match
- Killmail gets posted to BOTH channels
Different channels subscribed to the same entity. This is expected behavior.
A channel was deleted or the bot was removed from a guild, but the subscription record still exists in MongoDB.
Detection: Run node scripts/check-duplicates.js
Multiple subscription documents exist for the same guildId+channelId combination. This shouldn't happen due to upsert logic but could occur from race conditions or migration issues.
Detection: Run node scripts/check-duplicates.js
The match object from the database query contains wrong guildId/channelId values. This could happen if:
- Database corruption
- Migration/import error
- Manual database modification
Detection: Run node scripts/check-sent-history.js <channelId>
Checks for:
- Duplicate subscription records (same guildId+channelId)
- Entities with multiple subscribers
- Database integrity issues
node scripts/check-duplicates.jsSimulates the matching logic for a specific killmail ID:
- Shows which subscriptions would match
- Lists all guildId/channelId pairs that would receive the post
- Helps identify if multiple channels are subscribed to the same entity
node scripts/trace-killmail.js <killmail_id>Example:
node scripts/trace-killmail.js 119889062Checks the sentHistory for a specific channel:
- Shows recent posts and which subscription matched
- Detects if wrong subscription data was used
- Identifies mismatches between expected and actual guildId/channelId
node scripts/check-sent-history.js <channelId>The match object is never mutated after being retrieved from MongoDB:
- ✅ No assignments to
match.channelIdormatch.guildIdfound - ✅ Each loop iteration gets a fresh
matchobject from the array - ✅ Queue items are immutable after creation
// poll-r2.js - pushing to queue
const channelId = match.channelId;
const guildId = match.guildId;
discord_posts_queue.push({ db, match, guildId, channelId, killmail, zkb, colorCode, matchType });
// discord-post.js - processing queue
const { db, match, guildId, channelId, killmail, zkb, colorCode, matchType } = discord_posts_queue.shift();The values are extracted once and preserved through the queue.
The match object is stored in sentHistory:
await db.sentHistory.insertOne({
guildId: guildId,
channelId: channelId,
killmail_id: killmail.killmail_id,
createdAt: new Date(),
match: match, // <-- Full subscription document stored here
matchType: matchType
});This allows us to audit which subscription matched and verify if the wrong one was used.
The code already handles this in discord-post.js:
if (channelErr.status >= 400 && channelErr.status <= 499) {
await removeSubscriptions(db, channelId);
}When a channel is not found (404) or forbidden (403), subscriptions are removed.
Add a unique index to prevent duplicates:
await subsCollection.createIndex(
{ guildId: 1, channelId: 1 },
{ unique: true }
);This is already in the codebase in util/mongo.js, so duplicates should be prevented.
Query the subscription that's causing issues:
await db.subsCollection.findOne({ channelId: "WRONG_CHANNEL_ID" });Check if it contains correct guildId/channelId or if data is corrupted.
- Run
check-duplicates.jsto verify database integrity - When you see a wrong post, note the killmail ID
- Run
trace-killmail.js <killmail_id>to see all matching subscriptions - Run
check-sent-history.js <channelId>to check what was stored in the database - Compare the subscription data to understand why the wrong channel matched
Killmail 123 matches entity 456
- Channel A subscribed to entity 456 → Gets the killmail ✅
- Channel B subscribed to entity 456 → Gets the killmail ✅
Killmail 123 matches entity 456
- Channel A subscribed to entity 456 → Should get the killmail
- Channel B NOT subscribed to entity 456 → Gets the killmail anyway ❌
If Channel B is receiving killmails it shouldn't, check:
- Does Channel B have a subscription for that entity?
- Is there an orphaned subscription with Channel B's ID but different entityIds?
- Is the match object in sentHistory showing the wrong subscription data?