Bot.JS Dosyası
const TelegramBot = require('node-telegram-bot-api');
const { token } = require('./config');
// Botu başlat
const bot = new TelegramBot(token, { polling: true });
// Komutları içe aktar
const startCommand = require('./commands/start');
const checkCommand = require('./commands/check');
// Komutları yükle
startCommand(bot);
checkCommand(bot);
console.log('Bot is running...');İnvite.json { }InviteData.jsonconst fs = require('fs');
const path = require('path');
// Davet verilerini tutan JSON dosyasının yolu
const inviteFilePath = path.join(__dirname, '../data/invites.json');
// JSON dosyasını oku
function readInviteData() {
if (!fs.existsSync(inviteFilePath)) {
fs.writeFileSync(inviteFilePath, JSON.stringify({}), 'utf8');
}
const data = fs.readFileSync(inviteFilePath, 'utf8');
return JSON.parse(data);
}
// JSON dosyasına yaz
function writeInviteData(data) {
fs.writeFileSync(inviteFilePath, JSON.stringify(data, null, 2), 'utf8');
}
// Kullanıcının davet sayısını artır
function incrementInviteCount(userId, invitedBy) {
const data = readInviteData();
if (!data[invitedBy]) {
data[invitedBy] = {
inviteCount: 0,
invitedUsers: []
};
}
// Eğer kullanıcı daha önce davet edilmemişse, davet eden kişinin sayısını artırıyoruz
if (!data[invitedBy].invitedUsers.includes(userId)) {
data[invitedBy].invitedUsers.push(userId);
data[invitedBy].inviteCount += 1;
}
writeInviteData(data);
}
// Kullanıcının mevcut davet sayısını al
function getInviteCount(userId) {
const data = readInviteData();
return data[userId] ? data[userId].inviteCount : 0;
}
module.exports = {
incrementInviteCount,
getInviteCount
};Start.jsconst { createInviteLink } = require('../Utils/inviteLink'); // Davet linki fonksiyonunu içe aktarıyoruz
module.exports = function startCommand(bot) {
bot.onText(/\/start/, async (msg) => {
const chatId = msg.chat.id;
const username = msg.from.username || 'Guest';
try {
// Davet linki oluştur
const inviteLink = await createInviteLink();
// Eğer inviteLink undefined ya da null ise, burada hata mesajı gönderebiliriz
if (!inviteLink) {
throw new Error("Invite link could not be generated.");
}
const welcomeMessage = `
👋 Hello ${username}, welcome to the Free Access bot!
✅ Here is your Invite Link: ${inviteLink}
📤 Start sharing the link. Once people join through your invite link, you'll collect invites!
💬 Use /check to see your invite counter!
`;
bot.sendMessage(chatId, welcomeMessage);
} catch (error) {
// Hata durumunda mesaj gönder ve konsolda hata mesajını göster
console.error("Error creating invite link:", error);
bot.sendMessage(chatId, '❌ Sorry, something went wrong while creating the invite link.');
}
});
};Check.jsconst { getInviteCount } = require('../Utils/inviteData');
module.exports = function checkCommand(bot) {
bot.onText(/\/check/, (msg) => {
const chatId = msg.chat.id;
const inviteCount = getInviteCount(chatId);
bot.sendMessage(chatId, `You have ${inviteCount} invites so far! Keep sharing!`);
});
};InvıteLink.jsconst axios = require('axios');
const { token, channelId } = require('../config'); // API token ve kanal ID
const telegramApiUrl = `https://api.telegram.org/bot${token}/exportChatInviteLink`;
async function createInviteLink() {
try {
const response = await axios.post(telegramApiUrl, {
chat_id: channelId
});
if (response.data.ok) {
return response.data.result; // Davet linki döner
} else {
console.error('Error creating invite link:', response.data);
return null; // Eğer bir hata oluşursa null döndür
}
} catch (error) {
console.error('API Error while creating invite link:', error);
return null; // Hata durumunda null döner
}
}
module.exports = { createInviteLink };Config.jsmodule.exports = {
token: '7431393421:xxxxxx', // Bot tokeni buraya ekle
channelId: '-100xxxxxx' // Kanal ID buraya ekle
};