diff --git a/src/bot/bot.ts b/src/bot/bot.ts index c8682e0..fe55c7b 100644 --- a/src/bot/bot.ts +++ b/src/bot/bot.ts @@ -5,6 +5,8 @@ import { startCleanupJob } from '../jobs/cleanupJob'; export const linkedGroups: Set = new Set(); export async function startBot() { + console.log('Initializing bot...'); + const communityId = process.env.COMMUNITY_ID; const webhookUrl = process.env.WEBHOOK_URL; @@ -12,15 +14,45 @@ export async function startBot() { throw new Error('Required environment variables are not defined.'); } - // Load linked groups from the database - const groups = query('SELECT id FROM groups WHERE linked_parent = ?', [communityId]); - for (const group of groups) { - linkedGroups.add(group.id); + console.log('Loading linked groups from database...'); + try { + const groups = query('SELECT id FROM groups WHERE linked_parent = ?', [communityId]); + console.log(`Found ${groups.length} linked groups`); + + for (const group of groups) { + linkedGroups.add(group.id); + } + } catch (error) { + console.error('Failed to load linked groups:', error); + throw error; } - await fetchGroups(communityId); - await configureWebhook(webhookUrl); - - // Start the cleanup job - startCleanupJob(); + console.log('Fetching groups from API...'); + try { + await fetchGroups(communityId); + console.log('Successfully fetched groups'); + } catch (error) { + console.error('Failed to fetch groups:', error); + throw error; + } + + console.log('Configuring webhook...'); + try { + await configureWebhook(webhookUrl); + console.log('Webhook configured successfully'); + } catch (error) { + console.error('Failed to configure webhook:', error); + throw error; + } + + console.log('Starting cleanup job...'); + try { + startCleanupJob(); + console.log('Cleanup job started'); + } catch (error) { + console.error('Failed to start cleanup job:', error); + throw error; + } + + console.log('✅ Bot initialization complete'); }