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:
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!
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!