Untitled

 avatar
unknown
plain_text
a year ago
2.1 kB
6
Indexable
const { MongoClient, ObjectId } = require('mongodb');
const { Telegraf, Markup } = require('telegraf');


const mongoUrl = 'mongodb://localhost:27017';

async function connectToMongoDB(database) {
    const client = new MongoClient(mongoUrl, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });
    try {
        await client.connect();
        console.log('Connected to MongoDB');
        const dbName = `${database}`;
        return client.db(dbName);
    } catch (err) {
        console.error('Error connecting to MongoDB:', err);
        throw err;
    }
}

const botToken = '6603105438:AAGerPBDETAyKehhbqwmqyawpv1iu32gKAA';
const bot = new Telegraf(botToken);

bot.command('start', (ctx) => {
    const userId = ctx.from.id;
    ctx.reply('Choose a database:', Markup.inlineKeyboard([
        Markup.button.callback('Server 1', 'choose_db_server_1'),
        Markup.button.callback('Server 2', 'choose_db_server_2'),
    ]));
});




bot.action('choose_db_server_2', async (ctx) => {
    const database = 'server_1';
    ctx.db = ctx.db || {};
    ctx.db.database = database;
    ctx.reply('Enter the user ID to search:');
});


bot.action('choose_db_server_1', async (ctx) => {
    const database = 'server_2';
    ctx.db = ctx.db || {};
    ctx.db.database = database;
    ctx.reply('Enter the user ID to search:');
});

bot.on('text', async (ctx) => {
    const database = (ctx.db && ctx.db.database) || 'lainnya';
    const db = await connectToMongoDB(database);
    const targetUserId = parseInt(ctx.message.text.trim(), 10);
    const userData = await db.collection('users').findOne({ _id: targetUserId });
    console.log(database);
    if (userData) {
        ctx.reply(`User data:\nDatabase: ${database}\nTarget User ID: ${userData._id}\nUsername: ${userData.username}\n Status Premium: ${userData.user_premium}\n`);
    } else {
        ctx.reply('User data not found.');
    }
});




bot.launch().then(() => {
    console.log('Bot is running');
}).catch((err) => {
    console.error('Error starting the bot:', err);
});
Editor is loading...
Leave a Comment