Update index.js
Browse files
index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const bodyParser = require('body-parser');
|
| 3 |
const axios = require('axios');
|
|
|
|
| 4 |
const fs = require('fs');
|
| 5 |
const path = require('path');
|
| 6 |
const WebSocket = require('ws');
|
|
@@ -156,75 +157,12 @@ function updateLastSeen(category, items) {
|
|
| 156 |
saveLastSeen(lastSeen);
|
| 157 |
}
|
| 158 |
|
| 159 |
-
async function sendWebSocketMessageToActiveSessions(stockData, weatherData) {
|
| 160 |
-
const favorites = loadFavorites();
|
| 161 |
-
for (const [senderId] of activeSessions.entries()) {
|
| 162 |
-
const userFavorites = favorites[senderId] || [];
|
| 163 |
-
let sections = [];
|
| 164 |
-
let matchCount = 0;
|
| 165 |
-
|
| 166 |
-
const checkAndAdd = (label, section, useEmoji) => {
|
| 167 |
-
const items = section.items || [];
|
| 168 |
-
const matchedItems = userFavorites.length > 0
|
| 169 |
-
? items.filter(i => userFavorites.includes(cleanText(i.name)))
|
| 170 |
-
: items;
|
| 171 |
-
|
| 172 |
-
if (userFavorites.length > 0 && matchedItems.length === 0) return false;
|
| 173 |
-
|
| 174 |
-
matchCount += matchedItems.length;
|
| 175 |
-
const formattedItems = matchedItems.map(i =>
|
| 176 |
-
`- ${useEmoji ? addEmoji(i.name) : i.name}: ${formatValue(i.quantity)}`
|
| 177 |
-
).join("\n");
|
| 178 |
-
|
| 179 |
-
sections.push(`${label}:\n${formattedItems}\nโณ Restock In: ${section.countdown}`);
|
| 180 |
-
return true;
|
| 181 |
-
};
|
| 182 |
-
|
| 183 |
-
checkAndAdd("๐ ๏ธ ๐๐ฒ๐ฎ๐ฟ", stockData.gear, true);
|
| 184 |
-
checkAndAdd("๐ฑ ๐ฆ๐ฒ๐ฒ๐ฑ๐", stockData.seed, true);
|
| 185 |
-
checkAndAdd("๐ฅ ๐๐ด๐ด๐", stockData.egg, true);
|
| 186 |
-
checkAndAdd("๐จ ๐๐ผ๐๐บ๐ฒ๐๐ถ๐ฐ๐", stockData.cosmetics, false);
|
| 187 |
-
checkAndAdd("๐ ๐๐๐ฒ๐ป๐", stockData.honey, false);
|
| 188 |
-
|
| 189 |
-
if (userFavorites.length > 0 && matchCount === 0) continue;
|
| 190 |
-
if (sections.length === 0) continue;
|
| 191 |
-
|
| 192 |
-
const updatedAt = getPHTime().toLocaleString("en-PH", {
|
| 193 |
-
hour: "numeric", minute: "numeric", second: "numeric",
|
| 194 |
-
hour12: true, day: "2-digit", month: "short", year: "numeric"
|
| 195 |
-
});
|
| 196 |
-
|
| 197 |
-
const weatherInfo = weatherData
|
| 198 |
-
? `๐ค๏ธ ๐ช๐ฒ๐ฎ๐๐ต๐ฒ๐ฟ: ${weatherData.icon} ${weatherData.weatherType}\n๐ ${weatherData.description}\n๐ฏ ${weatherData.cropBonuses}\n`
|
| 199 |
-
: "";
|
| 200 |
-
|
| 201 |
-
const title = userFavorites.length > 0
|
| 202 |
-
? `โฅ๏ธ ${matchCount} ๐๐ฎ๐๐ผ๐ฟ๐ถ๐๐ฒ ๐ถ๐๐ฒ๐บ${matchCount > 1 ? "s" : ""} ๐๐ผ๐๐ป๐ฑ!`
|
| 203 |
-
: "๐พ ๐๐ฟ๐ผ๐ ๐ ๐๐๐ฅ๐๐๐ก โ ๐ง๐ฟ๐ฎ๐ฐ๐ธ๐ฒ๐ฟ";
|
| 204 |
-
|
| 205 |
-
const messageKey = JSON.stringify({ title, sections, weatherInfo, updatedAt });
|
| 206 |
-
const lastSent = lastSentCache.get(senderId);
|
| 207 |
-
if (lastSent === messageKey) continue;
|
| 208 |
-
|
| 209 |
-
lastSentCache.set(senderId, messageKey);
|
| 210 |
-
|
| 211 |
-
await sendMessage(senderId,
|
| 212 |
-
`${title}\n\n${sections.join("\n\n")}\n\n${weatherInfo}๐
Updated at (PH): ${updatedAt}`
|
| 213 |
-
);
|
| 214 |
-
}
|
| 215 |
-
}
|
| 216 |
-
|
| 217 |
function ensureWebSocketConnection() {
|
| 218 |
if (sharedWebSocket && sharedWebSocket.readyState === WebSocket.OPEN) return;
|
| 219 |
|
| 220 |
-
if (sharedWebSocket) {
|
| 221 |
-
sharedWebSocket.close();
|
| 222 |
-
}
|
| 223 |
-
|
| 224 |
sharedWebSocket = new WebSocket("wss://gagstock.gleeze.com");
|
| 225 |
|
| 226 |
sharedWebSocket.on("open", () => {
|
| 227 |
-
clearInterval(keepAliveInterval);
|
| 228 |
keepAliveInterval = setInterval(() => {
|
| 229 |
if (sharedWebSocket.readyState === WebSocket.OPEN) {
|
| 230 |
sharedWebSocket.send("ping");
|
|
@@ -243,38 +181,88 @@ function ensureWebSocketConnection() {
|
|
| 243 |
seed: stock.seed,
|
| 244 |
egg: stock.egg,
|
| 245 |
cosmetics: stock.cosmetics,
|
| 246 |
-
|
| 247 |
};
|
| 248 |
|
| 249 |
updateLastSeen("gear", stockData.gear.items);
|
| 250 |
updateLastSeen("seed", stockData.seed.items);
|
| 251 |
updateLastSeen("egg", stockData.egg.items);
|
| 252 |
updateLastSeen("cosmetics", stockData.cosmetics.items);
|
| 253 |
-
updateLastSeen("event", stockData.
|
| 254 |
-
|
| 255 |
-
const
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
} catch (error) {
|
| 262 |
console.error('Error processing WebSocket message:', error);
|
| 263 |
}
|
| 264 |
});
|
| 265 |
|
| 266 |
-
sharedWebSocket.on("close", (
|
| 267 |
-
console.log(`WebSocket closed: Code ${code}, Reason: ${reason}`);
|
| 268 |
clearInterval(keepAliveInterval);
|
| 269 |
sharedWebSocket = null;
|
| 270 |
setTimeout(ensureWebSocketConnection, 3000);
|
| 271 |
});
|
| 272 |
|
| 273 |
-
sharedWebSocket.on("error", (
|
| 274 |
-
|
| 275 |
-
if (sharedWebSocket) {
|
| 276 |
-
sharedWebSocket.close();
|
| 277 |
-
}
|
| 278 |
});
|
| 279 |
}
|
| 280 |
|
|
@@ -289,101 +277,91 @@ async function sendTyping(recipientId) {
|
|
| 289 |
}
|
| 290 |
}
|
| 291 |
|
| 292 |
-
async function
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
}
|
| 305 |
-
};
|
| 306 |
-
sharedWebSocket.on('message', messageHandler);
|
| 307 |
-
sharedWebSocket.send(JSON.stringify({ request: 'stock' }));
|
| 308 |
-
setTimeout(() => {
|
| 309 |
-
sharedWebSocket.off('message', messageHandler);
|
| 310 |
-
reject(new Error('WebSocket stock data request timed out'));
|
| 311 |
-
}, 5000);
|
| 312 |
-
} else {
|
| 313 |
-
reject(new Error('WebSocket not open for stock data.'));
|
| 314 |
-
}
|
| 315 |
-
}).catch(error => {
|
| 316 |
-
console.error('Failed to get stock data via WebSocket:', error.message);
|
| 317 |
return null;
|
| 318 |
-
}
|
| 319 |
}
|
| 320 |
|
| 321 |
-
async function
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
if (payload.status === "success" && payload.data) {
|
| 328 |
-
sharedWebSocket.off('message', messageHandler);
|
| 329 |
-
resolve(payload.data);
|
| 330 |
-
}
|
| 331 |
-
} catch (e) {
|
| 332 |
-
console.error('Error parsing WebSocket response:', e);
|
| 333 |
-
}
|
| 334 |
-
};
|
| 335 |
-
sharedWebSocket.on('message', messageHandler);
|
| 336 |
-
sharedWebSocket.send(JSON.stringify({ request: 'weather' }));
|
| 337 |
-
setTimeout(() => {
|
| 338 |
-
sharedWebSocket.off('message', messageHandler);
|
| 339 |
-
reject(new Error('WebSocket weather data request timed out'));
|
| 340 |
-
}, 5000);
|
| 341 |
-
} else {
|
| 342 |
-
reject(new Error('WebSocket not open for weather data.'));
|
| 343 |
-
}
|
| 344 |
-
}).catch(error => {
|
| 345 |
-
console.error('Failed to get weather data via WebSocket:', error.message);
|
| 346 |
return null;
|
| 347 |
-
}
|
| 348 |
}
|
| 349 |
|
| 350 |
-
function formatStockMessage(
|
| 351 |
-
if (!stockData) return "โ ๏ธ Could not fetch stock data";
|
| 352 |
|
| 353 |
let message = `๐ ๐๐จ๐ฅ๐ฅ๐๐ก๐ง ๐ฆ๐ง๐ข๐๐ ๐จ๐ฃ๐๐๐ง๐๐ฆ ๐\n`;
|
| 354 |
-
message += `๐ LAST UPDATED: ${
|
| 355 |
-
hour: "numeric", minute: "numeric", second: "numeric",
|
| 356 |
-
hour12: true, day: "2-digit", month: "short", year: "numeric"
|
| 357 |
-
})}\n\n`;
|
| 358 |
|
|
|
|
| 359 |
const categories = {
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
|
|
|
|
|
|
| 365 |
};
|
| 366 |
|
| 367 |
for (const [categoryKey, categoryName] of Object.entries(categories)) {
|
| 368 |
-
if (stockData[categoryKey] && stockData[categoryKey].
|
| 369 |
message += `${categoryName}:\n`;
|
| 370 |
-
stockData[categoryKey].
|
| 371 |
-
message += `โข ${
|
| 372 |
});
|
| 373 |
-
message +=
|
| 374 |
}
|
| 375 |
}
|
| 376 |
return message;
|
| 377 |
}
|
| 378 |
|
| 379 |
function formatWeatherMessage(weatherData) {
|
| 380 |
-
if (!weatherData) return "โ ๏ธ Could not fetch weather data";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
let message = 'โ
๐๐จ๐ฅ๐ฅ๐๐ก๐ง ๐ช๐๐๐ง๐๐๐ฅ ๐๐ข๐ก๐๐๐ง๐๐ข๐ก๐ฆ\n\n';
|
|
|
|
| 383 |
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
return message;
|
| 389 |
}
|
|
@@ -418,13 +396,9 @@ function setupUserNotification(recipientId, intervalMinutes = 5) {
|
|
| 418 |
}
|
| 419 |
|
| 420 |
const intervalId = setInterval(async () => {
|
| 421 |
-
const
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
await sendMessage(recipientId, formatStockMessage(stockData));
|
| 425 |
-
}
|
| 426 |
-
if (weatherData) {
|
| 427 |
-
await sendMessage(recipientId, formatWeatherMessage(weatherData));
|
| 428 |
}
|
| 429 |
}, intervalMinutes * 60 * 1000);
|
| 430 |
|
|
@@ -566,13 +540,11 @@ async function handleMessage(senderId, messageText) {
|
|
| 566 |
await sendMessage(senderId, helpMessage);
|
| 567 |
}
|
| 568 |
else if (command === '/stock') {
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
await sendMessage(senderId, stockData ? formatStockMessage(stockData) : 'โ ๏ธ Could not fetch stock data');
|
| 572 |
}
|
| 573 |
else if (command === '/weather') {
|
| 574 |
-
|
| 575 |
-
const weatherData = await getWeatherDataViaWebSocket();
|
| 576 |
await sendMessage(senderId, weatherData ? formatWeatherMessage(weatherData) : 'โ ๏ธ Could not fetch weather data');
|
| 577 |
}
|
| 578 |
else if (command.startsWith('/gagstock')) {
|
|
@@ -587,7 +559,6 @@ async function handleMessage(senderId, messageText) {
|
|
| 587 |
await sendMessage(senderId, 'โ ๏ธ Please provide valid minutes (e.g., /noti on 5)');
|
| 588 |
return;
|
| 589 |
}
|
| 590 |
-
ensureWebSocketConnection();
|
| 591 |
setupUserNotification(senderId, interval);
|
| 592 |
await sendMessage(senderId, `๐ Notifications enabled! Updates every ${interval} minutes.`);
|
| 593 |
}
|
|
@@ -605,7 +576,6 @@ async function handleMessage(senderId, messageText) {
|
|
| 605 |
}
|
| 606 |
|
| 607 |
loadUserPreferences();
|
| 608 |
-
ensureWebSocketConnection();
|
| 609 |
const PORT = process.env.PORT || 7860;
|
| 610 |
app.listen(PORT, () => {
|
| 611 |
console.log(`Bot running on port ${PORT}`);
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const bodyParser = require('body-parser');
|
| 3 |
const axios = require('axios');
|
| 4 |
+
const cron = require('node-cron');
|
| 5 |
const fs = require('fs');
|
| 6 |
const path = require('path');
|
| 7 |
const WebSocket = require('ws');
|
|
|
|
| 157 |
saveLastSeen(lastSeen);
|
| 158 |
}
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
function ensureWebSocketConnection() {
|
| 161 |
if (sharedWebSocket && sharedWebSocket.readyState === WebSocket.OPEN) return;
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
sharedWebSocket = new WebSocket("wss://gagstock.gleeze.com");
|
| 164 |
|
| 165 |
sharedWebSocket.on("open", () => {
|
|
|
|
| 166 |
keepAliveInterval = setInterval(() => {
|
| 167 |
if (sharedWebSocket.readyState === WebSocket.OPEN) {
|
| 168 |
sharedWebSocket.send("ping");
|
|
|
|
| 181 |
seed: stock.seed,
|
| 182 |
egg: stock.egg,
|
| 183 |
cosmetics: stock.cosmetics,
|
| 184 |
+
event: stock.honey,
|
| 185 |
};
|
| 186 |
|
| 187 |
updateLastSeen("gear", stockData.gear.items);
|
| 188 |
updateLastSeen("seed", stockData.seed.items);
|
| 189 |
updateLastSeen("egg", stockData.egg.items);
|
| 190 |
updateLastSeen("cosmetics", stockData.cosmetics.items);
|
| 191 |
+
updateLastSeen("event", stockData.event.items);
|
| 192 |
+
|
| 193 |
+
const favorites = loadFavorites();
|
| 194 |
+
|
| 195 |
+
for (const [senderId] of activeSessions.entries()) {
|
| 196 |
+
const userFavorites = favorites[senderId] || [];
|
| 197 |
+
let sections = [];
|
| 198 |
+
let matchCount = 0;
|
| 199 |
+
|
| 200 |
+
const checkAndAdd = (label, section, useEmoji) => {
|
| 201 |
+
const items = section.items || [];
|
| 202 |
+
const matchedItems = userFavorites.length > 0
|
| 203 |
+
? items.filter(i => userFavorites.includes(cleanText(i.name)))
|
| 204 |
+
: items;
|
| 205 |
+
|
| 206 |
+
if (userFavorites.length > 0 && matchedItems.length === 0) return false;
|
| 207 |
+
|
| 208 |
+
matchCount += matchedItems.length;
|
| 209 |
+
const formattedItems = matchedItems.map(i =>
|
| 210 |
+
`- ${useEmoji ? addEmoji(i.name) : i.name}: ${formatValue(i.quantity)}`
|
| 211 |
+
).join("\n");
|
| 212 |
+
|
| 213 |
+
sections.push(`${label}:\n${formattedItems}\nโณ Restock In: ${section.countdown}`);
|
| 214 |
+
return true;
|
| 215 |
+
};
|
| 216 |
+
|
| 217 |
+
checkAndAdd("๐ ๏ธ ๐๐ฒ๐ฎ๐ฟ", stockData.gear, true);
|
| 218 |
+
checkAndAdd("๐ฑ ๐ฆ๐ฒ๐ฒ๐ฑ๐", stockData.seed, true);
|
| 219 |
+
checkAndAdd("๐ฅ ๐๐ด๐ด๐", stockData.egg, true);
|
| 220 |
+
checkAndAdd("๐จ ๐๐ผ๐๐บ๐ฒ๐๐ถ๐ฐ๐", stockData.cosmetics, false);
|
| 221 |
+
checkAndAdd("๐ ๐๐๐ฒ๐ป๐", stockData.event, false);
|
| 222 |
+
|
| 223 |
+
if (userFavorites.length > 0 && matchCount === 0) continue;
|
| 224 |
+
if (sections.length === 0) continue;
|
| 225 |
+
|
| 226 |
+
const updatedAt = getPHTime().toLocaleString("en-PH", {
|
| 227 |
+
hour: "numeric", minute: "numeric", second: "numeric",
|
| 228 |
+
hour12: true, day: "2-digit", month: "short", year: "numeric"
|
| 229 |
+
});
|
| 230 |
+
|
| 231 |
+
const weather = await axios.get("https://growagardenstock.com/api/stock/weather")
|
| 232 |
+
.then(res => res.data)
|
| 233 |
+
.catch(() => null);
|
| 234 |
+
|
| 235 |
+
const weatherInfo = weather
|
| 236 |
+
? `๐ค๏ธ ๐ช๐ฒ๐ฎ๐๐ต๐ฒ๐ฟ: ${weather.icon} ${weather.weatherType}\n๐ ${weather.description}\n๐ฏ ${weather.cropBonuses}\n`
|
| 237 |
+
: "";
|
| 238 |
+
|
| 239 |
+
const title = userFavorites.length > 0
|
| 240 |
+
? `โฅ๏ธ ${matchCount} ๐๐ฎ๐๐ผ๐ฟ๐ถ๐๐ฒ ๐ถ๐๐ฒ๐บ${matchCount > 1 ? "s" : ""} ๐๐ผ๐๐ป๐ฑ!`
|
| 241 |
+
: "๐พ ๐๐ฟ๐ผ๐ ๐ ๐๐ฎ๐ฟ๐ฑ๐ฒ๐ป โ ๐ง๐ฟ๐ฎ๐ฐ๐ธ๐ฒ๐ฟ";
|
| 242 |
+
|
| 243 |
+
const messageKey = JSON.stringify({ title, sections, weatherInfo, updatedAt });
|
| 244 |
+
const lastSent = lastSentCache.get(senderId);
|
| 245 |
+
if (lastSent === messageKey) continue;
|
| 246 |
+
|
| 247 |
+
lastSentCache.set(senderId, messageKey);
|
| 248 |
+
|
| 249 |
+
await sendMessage(senderId,
|
| 250 |
+
`${title}\n\n${sections.join("\n\n")}\n\n${weatherInfo}๐
Updated at (PH): ${updatedAt}`
|
| 251 |
+
);
|
| 252 |
+
}
|
| 253 |
} catch (error) {
|
| 254 |
console.error('Error processing WebSocket message:', error);
|
| 255 |
}
|
| 256 |
});
|
| 257 |
|
| 258 |
+
sharedWebSocket.on("close", () => {
|
|
|
|
| 259 |
clearInterval(keepAliveInterval);
|
| 260 |
sharedWebSocket = null;
|
| 261 |
setTimeout(ensureWebSocketConnection, 3000);
|
| 262 |
});
|
| 263 |
|
| 264 |
+
sharedWebSocket.on("error", () => {
|
| 265 |
+
sharedWebSocket?.close();
|
|
|
|
|
|
|
|
|
|
| 266 |
});
|
| 267 |
}
|
| 268 |
|
|
|
|
| 277 |
}
|
| 278 |
}
|
| 279 |
|
| 280 |
+
async function getStockData() {
|
| 281 |
+
try {
|
| 282 |
+
const [stockResponse, updateResponse] = await Promise.all([
|
| 283 |
+
axios.get('https://kenlie.top/api/gag/stocks/'),
|
| 284 |
+
axios.get('https://jonell01-reuploadotherhruhh.hf.space/grow')
|
| 285 |
+
]);
|
| 286 |
+
return {
|
| 287 |
+
stockData: stockResponse.data,
|
| 288 |
+
lastUpdated: updateResponse.data.lastUpdated
|
| 289 |
+
};
|
| 290 |
+
} catch (error) {
|
| 291 |
+
console.error('Error fetching stock data:', error.message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
return null;
|
| 293 |
+
}
|
| 294 |
}
|
| 295 |
|
| 296 |
+
async function getWeatherData() {
|
| 297 |
+
try {
|
| 298 |
+
const response = await axios.get('https://kenlie.top/api/gag/weather/');
|
| 299 |
+
return response.data;
|
| 300 |
+
} catch (error) {
|
| 301 |
+
console.error('Error fetching weather data:', error.message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
return null;
|
| 303 |
+
}
|
| 304 |
}
|
| 305 |
|
| 306 |
+
function formatStockMessage(data) {
|
| 307 |
+
if (!data || !data.stockData.status) return "โ ๏ธ Could not fetch stock data";
|
| 308 |
|
| 309 |
let message = `๐ ๐๐จ๐ฅ๐ฅ๐๐ก๐ง ๐ฆ๐ง๐ข๐๐ ๐จ๐ฃ๐๐๐ง๐๐ฆ ๐\n`;
|
| 310 |
+
message += `๐ LAST UPDATED: ${new Date(data.lastUpdated).toLocaleString()}\n\n`;
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
const stockData = data.stockData.response;
|
| 313 |
const categories = {
|
| 314 |
+
gearStock: '๐ ๏ธ ๐๐๐๐ฅ',
|
| 315 |
+
eggStock: '๐ฅ ๐๐๐๐ฆ',
|
| 316 |
+
seedsStock: '๐ฑ ๐ฆ๐๐๐๐ฆ',
|
| 317 |
+
easterStock: '๐ฐ ๐๐๐ฆ๐ง๐๐ฅ',
|
| 318 |
+
nightStock: '๐ ๐ก๐๐๐๐ง',
|
| 319 |
+
honeyStock: '๐ฏ ๐๐ข๐ก๐๐ฌ',
|
| 320 |
+
cosmeticsStock: '๐ ๐๐ข๐ฆ๐ ๐๐ง๐๐๐ฆ '
|
| 321 |
};
|
| 322 |
|
| 323 |
for (const [categoryKey, categoryName] of Object.entries(categories)) {
|
| 324 |
+
if (stockData[categoryKey] && stockData[categoryKey].length > 0) {
|
| 325 |
message += `${categoryName}:\n`;
|
| 326 |
+
stockData[categoryKey].forEach(item => {
|
| 327 |
+
message += `โข ${item.name}: ${item.value}\n`;
|
| 328 |
});
|
| 329 |
+
message += '\n';
|
| 330 |
}
|
| 331 |
}
|
| 332 |
return message;
|
| 333 |
}
|
| 334 |
|
| 335 |
function formatWeatherMessage(weatherData) {
|
| 336 |
+
if (!weatherData || !weatherData.status) return "โ ๏ธ Could not fetch weather data";
|
| 337 |
+
|
| 338 |
+
const weatherEmojis = {
|
| 339 |
+
rain: '๐ง๏ธ ๐ฅ๐๐๐ก',
|
| 340 |
+
snow: 'โ๏ธ ๐ฆ๐ก๐ข๐ช',
|
| 341 |
+
thunderstorm: 'โก ๐ง๐๐จ๐ก๐๐๐ฅ๐ฆ๐ง๐ข๐ฅ๐ ',
|
| 342 |
+
bloodnight: '๐ฉธ ๐๐๐ข๐ข๐๐ก๐๐๐๐ง',
|
| 343 |
+
meteorshower: 'โ๏ธ ๐ ๐๐ง๐๐ข๐ฅ ๐ฆ๐๐ข๐ช๐๐ฅ',
|
| 344 |
+
disco: '๐ชฉ ๐๐๐ฆ๐๐ข',
|
| 345 |
+
jandelstorm: '๐ฏ๏ธ ๐๐๐ก๐๐๐๐ฆ๐ง๐ข๐ฅ๐ ',
|
| 346 |
+
blackhole: '๐ณ๏ธ ๐๐๐๐๐๐๐ข๐๐',
|
| 347 |
+
frost: 'โ๏ธ ๐๐ฅ๐ข๐ฆ๐ง '
|
| 348 |
+
};
|
| 349 |
|
| 350 |
let message = 'โ
๐๐จ๐ฅ๐ฅ๐๐ก๐ง ๐ช๐๐๐ง๐๐๐ฅ ๐๐ข๐ก๐๐๐ง๐๐ข๐ก๐ฆ\n\n';
|
| 351 |
+
const activeEvents = [];
|
| 352 |
|
| 353 |
+
for (const [event, data] of Object.entries(weatherData.response)) {
|
| 354 |
+
if (data.active) {
|
| 355 |
+
activeEvents.push(`${weatherEmojis[event] || '๐'} ${event.toUpperCase()}`);
|
| 356 |
+
}
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
if (activeEvents.length === 0) {
|
| 360 |
+
message += "โ๏ธ NO ACTIVE WEATHER EVENTS\n";
|
| 361 |
+
} else {
|
| 362 |
+
message += "๐ข ๐๐๐ง๐๐ฉ๐ ๐ช๐๐๐ง๐๐๐ฅ ๐๐ฉ๐๐ก๐ง๐ฆ:\n";
|
| 363 |
+
message += activeEvents.join('\n');
|
| 364 |
+
}
|
| 365 |
|
| 366 |
return message;
|
| 367 |
}
|
|
|
|
| 396 |
}
|
| 397 |
|
| 398 |
const intervalId = setInterval(async () => {
|
| 399 |
+
const data = await getStockData();
|
| 400 |
+
if (data) {
|
| 401 |
+
await sendMessage(recipientId, formatStockMessage(data));
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
}
|
| 403 |
}, intervalMinutes * 60 * 1000);
|
| 404 |
|
|
|
|
| 540 |
await sendMessage(senderId, helpMessage);
|
| 541 |
}
|
| 542 |
else if (command === '/stock') {
|
| 543 |
+
const data = await getStockData();
|
| 544 |
+
await sendMessage(senderId, data ? formatStockMessage(data) : 'โ ๏ธ Could not fetch stock data');
|
|
|
|
| 545 |
}
|
| 546 |
else if (command === '/weather') {
|
| 547 |
+
const weatherData = await getWeatherData();
|
|
|
|
| 548 |
await sendMessage(senderId, weatherData ? formatWeatherMessage(weatherData) : 'โ ๏ธ Could not fetch weather data');
|
| 549 |
}
|
| 550 |
else if (command.startsWith('/gagstock')) {
|
|
|
|
| 559 |
await sendMessage(senderId, 'โ ๏ธ Please provide valid minutes (e.g., /noti on 5)');
|
| 560 |
return;
|
| 561 |
}
|
|
|
|
| 562 |
setupUserNotification(senderId, interval);
|
| 563 |
await sendMessage(senderId, `๐ Notifications enabled! Updates every ${interval} minutes.`);
|
| 564 |
}
|
|
|
|
| 576 |
}
|
| 577 |
|
| 578 |
loadUserPreferences();
|
|
|
|
| 579 |
const PORT = process.env.PORT || 7860;
|
| 580 |
app.listen(PORT, () => {
|
| 581 |
console.log(`Bot running on port ${PORT}`);
|