• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Issue with Detecting User Presence in Roblox Game

zw1k1

New Coder
Hi everyone,

I'm facing an issue with my script that's designed to detect user presence in a Roblox game and notify a Discord channel (by using a Discord bot). The problem is that even though my script executes without errors, it doesn't detect users in the game properly. This is my script:

JavaScript:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages
    ]
});
const axios = require('axios');
const fs = require('fs');

const GAME_ID = "7239319209";
const USERNAME_FILE_PATH = __dirname + "/usernames.txt";
const DISCORD_BOT_TOKEN = "(my bot token)";
const MAX_RETRIES = 3; // Maximum number of retry attempts
const RETRY_INTERVAL = 5000; // Retry interval in milliseconds

function checkUserPresence(userId, retryCount = 0) {
    axios.post(`https://presence.roblox.com/v1/presence/users/${userId}/game`)
        .then(gamePresenceResponse => {
            if (gamePresenceResponse.data.placeId === GAME_ID) {
                console.log(`${userId} is in the game!`);

                // Send message to Discord channel
                const userTag = `${userId}#${gamePresenceResponse.data.userPresence.userPlatform}`;
                const channel = client.channels.cache.get("(discord channel ID)");
                if (channel) {
                    channel.send(`${userTag} is in the game!`);
                }
            }
        })
        .catch(error => {
            if (error.response && error.response.status === 404) {
                console.log(`${userId} is not in the game.`);
            } else {
                console.error('Error checking user presence:', error.message);

                // Retry after a connection timeout
                if (retryCount < MAX_RETRIES) {
                    console.log(`Retrying in ${RETRY_INTERVAL / 1000} seconds...`);
                    setTimeout(() => {
                        checkUserPresence(userId, retryCount + 1);
                    }, RETRY_INTERVAL);
                }
            }
        });
}





function getUserIdByUsername(username) {
    return axios.post(`https://users.roblox.com/v1/usernames/users`, { usernames: [username] }, {
        headers: { "User-Agent": "OhioAlertBot/1.0" }
    })
        .then(response => {
            if (response.data.data.length > 0) {
                return response.data.data[0].id;
            } else {
                console.error('User not found:', username);
                return null;
            }
        })
        .catch(error => {
            console.error('Error getting user ID:', error.message);
            return null;
        });
}


function refreshUsernames() {
    try {
        const usernames = fs.readFileSync(USERNAME_FILE_PATH, 'utf-8').split('\n');
        usernames.forEach(async username => {
            const userId = await getUserIdByUsername(username.trim());
            if (userId) {
                checkUserPresence(userId);
            }
        });
    } catch (error) {
        console.error('Error reading usernames file:', error.message);
    }
}

// Refresh usernames every 2 minutes
setInterval(() => {
    refreshUsernames();
    console.log('Usernames refreshed.');
}, 2 * 60 * 1000);


refreshUsernames();

client.login(DISCORD_BOT_TOKEN);


I've tried different approaches and even tested the same API requests using Swagger, where they work fine. However, when integrated into my script, it's not detecting users who are in the game. I've double-checked the game ID, the usernames file, and other variables, but still no luck.

Any insights or suggestions would be greatly appreciated. I'm open to sharing more specific details if needed.

Thank you in advance for your help!
 
Hi everyone,

I'm facing an issue with my script that's designed to detect user presence in a Roblox game and notify a Discord channel (by using a Discord bot). The problem is that even though my script executes without errors, it doesn't detect users in the game properly. This is my script:

JavaScript:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages
    ]
});
const axios = require('axios');
const fs = require('fs');

const GAME_ID = "7239319209";
const USERNAME_FILE_PATH = __dirname + "/usernames.txt";
const DISCORD_BOT_TOKEN = "(my bot token)";
const MAX_RETRIES = 3; // Maximum number of retry attempts
const RETRY_INTERVAL = 5000; // Retry interval in milliseconds

function checkUserPresence(userId, retryCount = 0) {
    axios.post(`https://presence.roblox.com/v1/presence/users/${userId}/game`)
        .then(gamePresenceResponse => {
            if (gamePresenceResponse.data.placeId === GAME_ID) {
                console.log(`${userId} is in the game!`);

                // Send message to Discord channel
                const userTag = `${userId}#${gamePresenceResponse.data.userPresence.userPlatform}`;
                const channel = client.channels.cache.get("(discord channel ID)");
                if (channel) {
                    channel.send(`${userTag} is in the game!`);
                }
            }
        })
        .catch(error => {
            if (error.response && error.response.status === 404) {
                console.log(`${userId} is not in the game.`);
            } else {
                console.error('Error checking user presence:', error.message);

                // Retry after a connection timeout
                if (retryCount < MAX_RETRIES) {
                    console.log(`Retrying in ${RETRY_INTERVAL / 1000} seconds...`);
                    setTimeout(() => {
                        checkUserPresence(userId, retryCount + 1);
                    }, RETRY_INTERVAL);
                }
            }
        });
}





function getUserIdByUsername(username) {
    return axios.post(`https://users.roblox.com/v1/usernames/users`, { usernames: [username] }, {
        headers: { "User-Agent": "OhioAlertBot/1.0" }
    })
        .then(response => {
            if (response.data.data.length > 0) {
                return response.data.data[0].id;
            } else {
                console.error('User not found:', username);
                return null;
            }
        })
        .catch(error => {
            console.error('Error getting user ID:', error.message);
            return null;
        });
}


function refreshUsernames() {
    try {
        const usernames = fs.readFileSync(USERNAME_FILE_PATH, 'utf-8').split('\n');
        usernames.forEach(async username => {
            const userId = await getUserIdByUsername(username.trim());
            if (userId) {
                checkUserPresence(userId);
            }
        });
    } catch (error) {
        console.error('Error reading usernames file:', error.message);
    }
}

// Refresh usernames every 2 minutes
setInterval(() => {
    refreshUsernames();
    console.log('Usernames refreshed.');
}, 2 * 60 * 1000);


refreshUsernames();

client.login(DISCORD_BOT_TOKEN);


I've tried different approaches and even tested the same API requests using Swagger, where they work fine. However, when integrated into my script, it's not detecting users who are in the game. I've double-checked the game ID, the usernames file, and other variables, but still no luck.

Any insights or suggestions would be greatly appreciated. I'm open to sharing more specific details if needed.

Thank you in advance for your help!
HI there,
Hope you don't mind me asking, but just curious...what is the purpose for this bot?
 

New Threads

Buy us a coffee!

300x250
Top Bottom