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