81 lines
3.3 KiB
JavaScript
81 lines
3.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Find transcript channel messages whose embed "Users in transcript" lists a given member ID.
|
|
* Usage: node scripts/find-transcript-by-member.js <channelId> <memberId> [maxMessages]
|
|
* Example: node scripts/find-transcript-by-member.js 1335424071227281520 219276746153787392 500
|
|
* Fetches in pages of 100; maxMessages limits total (e.g. 500 = 5 pages). Default 100.
|
|
*/
|
|
const path = require('path');
|
|
const { Client, GatewayIntentBits } = require('discord.js');
|
|
|
|
require('dotenv').config({ path: path.join(__dirname, '../.env') });
|
|
require('dotenv').config({ path: path.join(__dirname, '../../.env') });
|
|
|
|
const TOKEN = process.env.MEMBER_BOT_TOKEN || process.env.DISCORD_BOT_TOKEN;
|
|
const channelId = process.argv[2];
|
|
const memberId = process.argv[3];
|
|
const maxMessages = parseInt(process.argv[4], 10) || 100;
|
|
const PAGE = 100;
|
|
|
|
if (!TOKEN || !channelId || !memberId) {
|
|
console.error('Usage: node scripts/find-transcript-by-member.js <channelId> <memberId> [maxMessages]');
|
|
process.exit(1);
|
|
}
|
|
|
|
const client = new Client({
|
|
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
|
|
});
|
|
|
|
client.once('ready', async () => {
|
|
try {
|
|
const channel = await client.channels.fetch(channelId).catch(() => null);
|
|
if (!channel) {
|
|
console.log('Channel not found or bot cannot access it.');
|
|
process.exit(0);
|
|
}
|
|
console.log('Channel:', channel.name, '(' + channel.id + ')');
|
|
console.log('Looking for member ID', memberId, 'in embed "Users in transcript"');
|
|
const memberRef = `<@${memberId}>`;
|
|
let totalScanned = 0;
|
|
let found = 0;
|
|
let before = undefined;
|
|
while (totalScanned < maxMessages) {
|
|
const limit = Math.min(PAGE, maxMessages - totalScanned);
|
|
const options = before ? { limit, before } : { limit };
|
|
const messages = await channel.messages.fetch(options);
|
|
if (messages.size === 0) break;
|
|
totalScanned += messages.size;
|
|
for (const [, m] of messages.sort((a, b) => b.createdTimestamp - a.createdTimestamp)) {
|
|
if (!m.embeds?.length) continue;
|
|
for (const emb of m.embeds) {
|
|
const usersField = emb.fields?.find((f) => f.name && f.name.toLowerCase().includes('users in transcript'));
|
|
if (!usersField?.value || !usersField.value.includes(memberRef)) continue;
|
|
const ticketNameField = emb.fields?.find((f) => f.name && f.name.toLowerCase().includes('ticket name'));
|
|
const ticketName = ticketNameField?.value?.trim() || '(no Ticket Name field)';
|
|
console.log('\n--- Match ---');
|
|
console.log('Message ID:', m.id);
|
|
console.log('Created:', m.createdAt.toISOString());
|
|
console.log('Ticket Name:', ticketName);
|
|
console.log('Users in transcript:\n' + usersField.value);
|
|
found++;
|
|
}
|
|
}
|
|
const oldestMsg = messages.reduce((a, m) => (m.createdTimestamp < (a?.createdTimestamp ?? Infinity) ? m : a), null);
|
|
before = oldestMsg?.id;
|
|
if (messages.size < PAGE) break;
|
|
}
|
|
console.log('\nTotal messages scanned:', totalScanned);
|
|
console.log('Total messages matching member', memberId, ':', found);
|
|
} catch (e) {
|
|
console.error(e.message || e);
|
|
} finally {
|
|
client.destroy();
|
|
process.exit(0);
|
|
}
|
|
});
|
|
|
|
client.login(TOKEN).catch((e) => {
|
|
console.error('Login failed:', e.message);
|
|
process.exit(1);
|
|
});
|