Untitled

 avatar
unknown
plain_text
a year ago
2.0 kB
4
Indexable
const { MongoClient } = require('mongodb');

// MongoDB connection URL
const url = 'mongodb://localhost:27017';
const dbName = 'yourDBName';

// Connect to MongoDB
MongoClient.connect(url, async (err, client) => {
  if (err) throw err;

  const db = client.db(dbName);
  
  // Logic for /total command
  async function getTotalStats() {
    const totalUsers = await db.collection('users').countDocuments();

    // Count active users within 1 hour, 24 hours, today (you'll need timestamps in your database)
    const activeUsers1Hour = await db.collection('users').countDocuments({
      lastInteraction: { $gt: new Date(Date.now() - 60 * 60 * 1000) }
    });

    // Similar queries for 24 hours and today
  
    // Count received links
    const totalReceivedLinks = await db.collection('links').countDocuments();

    // Count processed links
    const totalProcessedLinks = await db.collection('processedLinks').countDocuments();

    // Count sent files
    const totalSentFiles = await db.collection('sentFiles').countDocuments();

    return {
      totalUsers,
      activeUsers1Hour,
      // Add other stats here
      totalReceivedLinks,
      totalProcessedLinks,
      totalSentFiles
    };
  }

  // Example: Usage of /total command within Telegram bot API
  bot.onText(/\/total/, async (msg) => {
    const chatId = msg.chat.id;
    
    try {
      const stats = await getTotalStats();
      const response = `Total Users: ${stats.totalUsers}\nActive Users (1 hour): ${stats.activeUsers1Hour}\nTotal Received Links: ${stats.totalReceivedLinks}\nTotal Processed Links: ${stats.totalProcessedLinks}\nTotal Sent Files: ${stats.totalSentFiles}`;
      
      // Send the stats as a message to the Telegram chat
      bot.sendMessage(chatId, response);
    } catch (error) {
      // Handle errors
      console.error(error);
      bot.sendMessage(chatId, 'Error fetching stats.');
    }
  });

  // Close the connection after usage
  // client.close();
});
Editor is loading...
Leave a Comment