First upload
This commit is contained in:
57
scripts/fetch-channel-messages.js
Normal file
57
scripts/fetch-channel-messages.js
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Fetch recent messages from a Discord channel.
|
||||
* Usage: node scripts/fetch-channel-messages.js <channelId> [limit]
|
||||
* Default limit: 10
|
||||
*/
|
||||
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 limit = Math.min(parseInt(process.argv[3], 10) || 10, 100);
|
||||
|
||||
if (!TOKEN || !channelId) {
|
||||
console.error('Usage: node scripts/fetch-channel-messages.js <channelId> [limit]');
|
||||
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);
|
||||
}
|
||||
const messages = await channel.messages.fetch({ limit });
|
||||
console.log('Channel:', channel.name, '(' + channel.id + ')');
|
||||
console.log('Messages fetched:', messages.size, '(requested', limit + ')');
|
||||
if (messages.size === 0) {
|
||||
console.log('No messages visible (empty channel or no Read Message History permission).');
|
||||
process.exit(0);
|
||||
}
|
||||
for (const [, m] of messages.sort((a, b) => b.createdTimestamp - a.createdTimestamp)) {
|
||||
const preview = (m.content || '(embed/attachment only)').slice(0, 80);
|
||||
console.log('---');
|
||||
console.log('ID:', m.id, '| Author:', m.author.tag, '|', m.createdAt.toISOString());
|
||||
console.log(preview + (m.content && m.content.length > 80 ? '...' : ''));
|
||||
}
|
||||
} 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);
|
||||
});
|
||||
Reference in New Issue
Block a user