Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while running a bot #1246

Open
Menilj opened this issue Feb 6, 2025 · 1 comment
Open

Error while running a bot #1246

Menilj opened this issue Feb 6, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@Menilj
Copy link

Menilj commented Feb 6, 2025

✅ Intelligent WhatsApp Bot is running...
{"level":30,"time":"2025-02-06T15:39:31.830Z","pid":4424,"hostname":"DESKTOP-Q6DH6MA","class":"baileys","trace":"Error: WebSocket Error (Opening handshake has timed out)\n at WebSocketClient. (/mnt/c/Users/USER/Antilink-bot/node_modules/@whiskeysockets/baileys/lib/Socket/socket.js:620:17)\n at WebSocketClient.emit (node:events:517:28)\n at WebSocket. (/mnt/c/Users/USER/Antilink-bot/node_modules/@whiskeysockets/baileys/lib/Socket/Client/websocket.js:46:100)\n at WebSocket.emit (node:events:517:28)\n at emitErrorAndClose (/mnt/c/Users/USER/Antilink-bot/node_modules/ws/lib/websocket.js:1041:13)\n at process.processTicksAndRejections (node:internal/process/task_queues:82:21)","msg":"connection errored"}

const { makeWASocket, useMultiFileAuthState } = `require("@whiskeysockets/baileys");`
// Admin phone number (replace with yours)
const ADMIN_PHONE = "@s.whatsapp.net";

// List of approved group admins
const ADMIN_LIST = [ADMIN_PHONE];

// Track warnings for anti-spam
const WARNING_COUNT = {};
const MAX_WARNINGS = 2; // Ban user after exceeding warnings

(async () => {
    const { state, saveCreds } = await useMultiFileAuthState("./auth_info");
    const sock = makeWASocket({ auth: state });

    sock.ev.on("creds.update", saveCreds);

    // Handle incoming messages
    sock.ev.on("messages.upsert", async ({ messages }) => {
        const msg = messages[0];
        if (!msg.message || !msg.key.remoteJid || msg.key.fromMe) return;

        const chatId = msg.key.remoteJid;
        const sender = msg.key.participant || msg.key.remoteJid;
        const messageText = msg.message.conversation || msg.message.extendedTextMessage?.text;

        if (!messageText) return;

        console.log(`📩 New message from ${sender}: ${messageText}`);

    

        // Bot Commands
        const args = messageText.slice(1).trim().split(" ");
        const command = args.shift().toLowerCase();

        switch (command) {
            case "help":
                await sock.sendMessage(chatId, { text: "🛠️ Commands: \n!help - Show commands\n!news - Latest news\n!weather Nairobi - Get weather\n!kick @user - Remove user" });
                break;

            case "news":
                const news = await getNews();
                await sock.sendMessage(chatId, { text: news });
                break;

            case "weather":
                const city = args.join(" ");
                const weather = await getWeather(city);
                await sock.sendMessage(chatId, { text: weather });
                break;

            case "kick":
                if (ADMIN_LIST.includes(sender)) {
                    const userToKick = args[0]?.replace("@", "") + "@s.whatsapp.net";
                    await sock.groupParticipantsUpdate(chatId, [userToKick], "remove");
                    await sock.sendMessage(chatId, { text: `❌ @${userToKick.split("@")[0]} has been removed.`, mentions: [userToKick] });
                } else {
                    await sock.sendMessage(chatId, { text: "🚫 Only admins can use this command!" });
                }
                break;

            default:
                await sock.sendMessage(chatId, { text: "⚠️ Unknown command! Type !help for a list of commands." });
        }
    });

    console.log("✅ Intelligent WhatsApp Bot is running...");
})();



// Get Latest News
async function getNews() {
    try {
        const response = await axios.get("https://newsapi.org/v2/top-headlines?country=ke&apiKey=your_newsapi_key");
        return `📰 Latest News: ${response.data.articles[0].title}\nRead more: ${response.data.articles[0].url}`;
    } catch (error) {
        console.error("News API Error:", error);
        return "⚠️ Could not fetch news.";
    }
}

// Get Weather Info
async function getWeather(city) {
    try {
        const response = await axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=your_weatherapi_key&units=metric`);
        return `🌤️ Weather in ${city}: ${response.data.weather[0].description}, Temp: ${response.data.main.temp}°C`;
    } catch (error) {
        console.error("Weather API Error:", error);
        return "⚠️ Could not fetch weather info.";
    }
}



@Menilj Menilj added the bug Something isn't working label Feb 6, 2025
@AstroX11
Copy link
Contributor

AstroX11 commented Feb 6, 2025

not from baileys it's from you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants