107 lines
3.5 KiB
JavaScript
107 lines
3.5 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const https = require('https');
|
|
const logger = require('../../modules/colorfulLogger');
|
|
|
|
const configPath = path.join(__dirname, '../../config/koreanbotsConfig.json');
|
|
|
|
function getConfig() {
|
|
try {
|
|
const configData = fs.readFileSync(configPath, 'utf8');
|
|
return JSON.parse(configData);
|
|
} catch (error) {
|
|
logger.error(`Error reading or parsing koreanbotsConfig.json: ${error.message}`);
|
|
|
|
if (fs.existsSync(configPath)) {
|
|
const backupPath = `${configPath}.${Date.now()}.bak`;
|
|
try {
|
|
fs.renameSync(configPath, backupPath);
|
|
logger.info(`Backed up corrupted koreanbotsConfig.json to ${backupPath}`);
|
|
} catch (renameError) {
|
|
logger.error(`Failed to backup koreanbotsConfig.json: ${renameError.message}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const defaultConfig = {
|
|
"ENABLED": true,
|
|
"KOREANBOTS_TOKEN": "your-koreanbots-token-here",
|
|
"LOG_UPDATES": true,
|
|
"UPDATE_INTERVAL_SECONDS": 300
|
|
};
|
|
|
|
try {
|
|
fs.writeFileSync(configPath, JSON.stringify(defaultConfig, null, 2), 'utf8');
|
|
logger.info('Created a new koreanbotsConfig.json with default values.');
|
|
return defaultConfig;
|
|
} catch (writeError) {
|
|
logger.error(`Failed to create new koreanbotsConfig.json: ${writeError.message}`);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
function initializeKoreanbotsUpdate(client) {
|
|
const config = getConfig();
|
|
|
|
if (!config || !config.ENABLED) {
|
|
if (config && config.LOG_UPDATES) {
|
|
logger.info('Koreanbots update is disabled.');
|
|
}
|
|
return;
|
|
}
|
|
|
|
const update = () => {
|
|
const serverCount = client.guilds.cache.size;
|
|
const postData = JSON.stringify({ servers: serverCount });
|
|
|
|
const options = {
|
|
hostname: 'koreanbots.dev',
|
|
path: `/api/v2/bots/${process.env.CLIENT_ID}/stats`,
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': config.KOREANBOTS_TOKEN,
|
|
'Content-Type': 'application/json',
|
|
'Content-Length': Buffer.byteLength(postData),
|
|
},
|
|
};
|
|
|
|
const req = https.request(options, (res) => {
|
|
let data = '';
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
if (config.LOG_UPDATES) {
|
|
let logMessage = `Koreanbots API response: ${res.statusCode}`;
|
|
try {
|
|
const responseBody = JSON.parse(data);
|
|
if (responseBody.message) {
|
|
logMessage += ` - ${responseBody.message}`;
|
|
}
|
|
} catch (e) {
|
|
// Not a json response, do nothing
|
|
}
|
|
logger.info(logMessage);
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', (e) => {
|
|
if (config.LOG_UPDATES) {
|
|
logger.error(`Koreanbots API request error: ${e.message}`);
|
|
}
|
|
});
|
|
|
|
req.write(postData);
|
|
req.end();
|
|
};
|
|
|
|
const interval = (config.UPDATE_INTERVAL_SECONDS || 300) * 1000;
|
|
|
|
setInterval(update, interval);
|
|
logger.info(`Koreanbots update is enabled and will run every ${interval / 1000} seconds.`);
|
|
update();
|
|
}
|
|
|
|
module.exports = { initializeKoreanbotsUpdate }; |