Files

66 lines
2.3 KiB
TypeScript

/*
* Live slopbot check: login, guild presence, and one history page.
*
* Usage (from poc/):
* VESKSLOP_SLOPBOT_ENABLED=1 VESKSLOP_SLOPBOT_STATE=/path/to/slopbot.json \
* tsx ../media-scrape/test/live-slopbot.ts <channelId> [guildId]
*
* Or set VESKSLOP_TEST_CHANNEL / VESKSLOP_TEST_GUILD. The token is read from the
* environment or the state file and is never printed.
*/
import { extractMedia } from "../src/extract.js";
import {
loadSlopbotConfig,
slopbotFetcher,
slopbotStatus,
SlopbotBackend,
startSlopbot,
stopSlopbot
} from "../slopbot/index.js";
async function main() {
const channelId = process.argv[2] ?? process.env.VESKSLOP_TEST_CHANNEL;
const guildId = process.argv[3] ?? process.env.VESKSLOP_TEST_GUILD;
if (!channelId) {
console.error("usage: live-slopbot.ts <channelId> [guildId]");
process.exit(1);
}
const config = loadSlopbotConfig();
if (!config.enabled) {
console.error("slopbot disabled: set VESKSLOP_SLOPBOT_ENABLED=1 and a token");
process.exit(1);
}
const client = await startSlopbot(config);
try {
const status = slopbotStatus(client);
console.log(`logged in as ${status.user}`);
console.log(`guilds: ${status.guilds.length}`);
if (guildId) console.log(`target guild present: ${client.guilds.cache.has(guildId)}`);
const backend = new SlopbotBackend(slopbotFetcher(client));
const startedAt = Date.now();
const page = await backend.fetchPage(channelId, null, 25);
console.log(`fetched ${page.messages.length} messages in ${Date.now() - startedAt} ms`);
const withContent = page.messages.filter(message => message.content.length > 0).length;
const attachments = page.messages.reduce((sum, message) => sum + message.attachments.length, 0);
const media = page.messages.flatMap(message => extractMedia(message));
const kinds = media.reduce<Record<string, number>>((acc, item) => {
acc[item.kind] = (acc[item.kind] ?? 0) + 1;
return acc;
}, {});
console.log(`content present: ${withContent}/${page.messages.length}`);
console.log(`attachments: ${attachments}`);
console.log(`media: ${JSON.stringify(kinds)}`);
} finally {
await stopSlopbot(client);
}
}
void main();