This repository was archived by the owner on Sep 20, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathannouncer.js
More file actions
184 lines (148 loc) · 6.69 KB
/
Copy pathannouncer.js
File metadata and controls
184 lines (148 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const { Client, GatewayIntentBits } = require("discord.js");
const axios = require("axios");
const fs = require("fs");
require("dotenv").config();
const TOKEN = process.env.TOKEN;
const MILESTONE_FREQUENCY = parseInt(process.env.FREQUENCY);
const path = './visitchecker.json';
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});
let lastAnnouncedVisits = {};
const activeChecks = new Set();
const channelCache = new Map();
function loadLastAnnouncedVisits() {
if (fs.existsSync(path)) {
lastAnnouncedVisits = JSON.parse(fs.readFileSync(path));
} else {
fs.writeFileSync(path, JSON.stringify({}));
}
}
function saveLastAnnouncedVisits() {
fs.writeFileSync(path, JSON.stringify(lastAnnouncedVisits, null, 2));
}
function getGamesFromEnv() {
const games = [];
let index = 1;
// Base game
if (process.env.UNIVERSEID && process.env.CHANNELID) {
games.push({
universeId: process.env.UNIVERSEID,
channelId: process.env.CHANNELID,
});
}
// Additional numbered games
while (true) {
index++;
const universe = process.env[`UNIVERSEID_${index}`];
const channel = process.env[`CHANNELID_${index}`];
if (!universe || !channel) break;
games.push({ universeId: universe, channelId: channel });
}
return games;
}
async function checkMilestone(game) {
if (activeChecks.has(game.universeId)) return;
activeChecks.add(game.universeId);
try {
// Fetch Game Details (Visits, Name, Last Updated)
const gameResponse = await axios.get(`https://games.roblox.com/v1/games?universeIds=${game.universeId}`, { timeout: 10000 });
const gameData = gameResponse.data.data[0];
if (!gameData) return console.error(`No game data for Universe ID ${game.universeId}`);
// Fetch Places/Subpages
// placesData contains objects like: { id: 12345, name: "Place Name" }
const placesResponse = await axios.get(`https://develop.roblox.com/v1/universes/${game.universeId}/places?sortOrder=Asc&limit=100`, { timeout: 10000 });
const placesData = placesResponse.data.data;
// Extract current Place IDs and create a map for easy lookup
const currentPlaceMap = placesData.reduce((map, place) => {
map[place.id] = place.name;
return map;
}, {});
const currentPlaceIds = Object.keys(currentPlaceMap).map(id => parseInt(id));
const visitCount = gameData.visits;
const currentName = gameData.name;
const currentUpdated = gameData.updated;
let record = lastAnnouncedVisits[game.universeId] || {};
let hasChanges = false;
// 1. GAME NAME UPDATE
if (record.name !== currentName) {
record.name = currentName;
hasChanges = true;
}
if (typeof record.lastVisit === 'undefined') {
record.lastVisit = 0;
hasChanges = true;
}
// --- UPDATED: 2. CHECK FOR NEW PLACES/SUBPAGES ---
const previousPlaceIds = record.placeIds || [];
const newPlaceIds = currentPlaceIds.filter(id => !previousPlaceIds.includes(id));
let channel = channelCache.get(game.channelId);
if (!channel) {
channel = await client.channels.fetch(game.channelId);
channelCache.set(game.channelId, channel);
}
if (newPlaceIds.length > 0) {
const placeCount = newPlaceIds.length;
// Update the record immediately to prevent duplicate announcements if send is slow
record.placeIds = currentPlaceIds;
hasChanges = true;
// Generate the list of new places with names and links
const newPlacesList = newPlaceIds.map(id => {
const name = currentPlaceMap[id] || `- Unknown Place (${id})`;
return `- [${name}](<https://www.roblox.com/games/${id}>)`;
}).join('\n');
// Construct the final announcement message
const message = `<@&1360880411114209340> **${record.name}** has ${placeCount} new subplace${placeCount > 1 ? 's' : ''}!\n` +
`${newPlacesList}`;
await channel.send(message);
} else {
// Ensure the record reflects the current list even if no new ones are found
record.placeIds = currentPlaceIds;
}
// 3. GAME UPDATE (TIMESTAMP) CHECK
const previousUpdated = record.lastUpdatedTimestamp;
const isNewUpdate = previousUpdated && currentUpdated && new Date(currentUpdated) > new Date(previousUpdated);
if (isNewUpdate) {
// New update detected!
record.lastUpdatedTimestamp = currentUpdated;
hasChanges = true;
const unixTimestamp = Math.floor(new Date(currentUpdated).getTime() / 1000);
await channel.send(`<@&1360880411114209340> **${record.name}** updated <t:${unixTimestamp}:R>!`);
} else if (typeof previousUpdated === 'undefined') {
record.lastUpdatedTimestamp = currentUpdated;
hasChanges = true;
}
// 4. VISITS MILESTONE CHECK
const lastVisit = record.lastVisit;
const nextMilestone = Math.floor(visitCount / MILESTONE_FREQUENCY) * MILESTONE_FREQUENCY;
if (visitCount >= lastVisit + MILESTONE_FREQUENCY) {
record.lastVisit = nextMilestone;
hasChanges = true;
await channel.send(`<@&1360880411114209340> **${record.name}** has reached **${nextMilestone.toLocaleString()}** visits!`);
}
// Save the updated record if any changes occurred
if (hasChanges) {
lastAnnouncedVisits[game.universeId] = record;
saveLastAnnouncedVisits();
}
const lastUpdatedReadable = new Date(currentUpdated).toLocaleString();
console.log(`${record.name}: ${visitCount} visits, Places: ${currentPlaceIds.length}, Last Updated: ${currentUpdated}`);
} catch (error) {
console.error(`Error checking Universe ID ${game.universeId}:`, error.message);
} finally {
activeChecks.delete(game.universeId);
}
}
client.once("ready", () => {
console.log(`Logged in as ${client.user.tag}`);
loadLastAnnouncedVisits();
const games = getGamesFromEnv();
if (games.length === 0) return console.error("No valid UNIVERSEID/CHANNELID pairs found in .env");
// Initial check
games.forEach(checkMilestone);
// Set up the interval for continuous checking (every 3 minutes)
setInterval(() => {
games.forEach(checkMilestone);
}, 180000);
});
client.login(TOKEN);