diff --git a/Xiao.js b/Xiao.js index 31ad3e19..d86a07fb 100644 --- a/Xiao.js +++ b/Xiao.js @@ -50,16 +50,22 @@ client.registry client.on('ready', () => { client.logger.info(`[READY] Logged in as ${client.user.tag}! ID: ${client.user.id}`); + + // Push client-related activities client.activities.push( { text: () => `${formatNumber(client.guilds.cache.size)} servers`, type: 'WATCHING' }, { text: () => `with ${formatNumber(client.registry.commands.size)} commands`, type: 'PLAYING' }, { text: () => `${formatNumber(client.channels.cache.size)} channels`, type: 'WATCHING' } ); + + // Interval to change activity every minute client.setInterval(() => { const activity = client.activities[Math.floor(Math.random() * client.activities.length)]; const text = typeof activity.text === 'function' ? activity.text() : activity.text; client.user.setActivity(text, { type: activity.type }); }, 60000); + + // Set up meme poster interval if (client.memePoster) { client.setInterval(async () => { try { @@ -70,12 +76,23 @@ client.on('ready', () => { } }, client.memePoster.postInterval); } + + // Import command-leaderboard.json try { const results = client.importCommandLeaderboard(); if (!results) client.logger.error('[LEADERBOARD] command-leaderboard.json is not formatted correctly.'); } catch (err) { client.logger.error(`[LEADERBOARD] Could not parse command-leaderboard.json:\n${err.stack}`); } + + // Export command-leaderboard.json every 30 minutes + client.setInterval(() => { + try { + client.exportCommandLeaderboard(); + } catch (err) { + client.logger.error(`[LEADERBOARD] Failed to export command-leaderboard.json:\n${err.stack}`); + } + }, 1.8e+6); }); client.on('message', async msg => { @@ -113,6 +130,7 @@ client.on('guildMemberRemove', async member => { client.on('disconnect', event => { client.logger.error(`[DISCONNECT] Disconnected with code ${event.code}.`); + client.exportCommandLeaderboard(); process.exit(0); }); diff --git a/commands/util/command-leaderboard-export.js b/commands/util/command-leaderboard-export.js index 2794450a..30465e7c 100644 --- a/commands/util/command-leaderboard-export.js +++ b/commands/util/command-leaderboard-export.js @@ -25,16 +25,7 @@ module.exports = class CommandLeaderboardExportCommand extends Command { } run(msg) { - let text = '{'; - for (const command of this.client.registry.commands.values()) { - if (command.uses === undefined) continue; - text += `"${command.name}":${command.uses},`; - } - text = text.slice(0, -1); - text += '}'; - fs.writeFileSync(path.join(__dirname, '..', '..', 'command-leaderboard.json'), Buffer.from(text), { - encoding: 'utf8' - }); - return msg.say({ files: [{ attachment: Buffer.from(text), name: 'command-leaderboard.json' }] }); + const result = this.client.exportCommandLeaderboard(); + return msg.say({ files: [{ attachment: result, name: 'command-leaderboard.json' }] }); } }; diff --git a/package.json b/package.json index 58a7acae..18fae713 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "116.1.6", + "version": "116.1.7", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/Client.js b/structures/Client.js index e90d3d74..7208bcc3 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -54,4 +54,19 @@ module.exports = class XiaoClient extends CommandoClient { } return file; } + + exportCommandLeaderboard() { + let text = '{'; + for (const command of this.registry.commands.values()) { + if (command.uses === undefined) continue; + text += `"${command.name}":${command.uses},`; + } + text = text.slice(0, -1); + text += '}'; + const buf = Buffer.from(text); + fs.writeFileSync(path.join(__dirname, '..', 'command-leaderboard.json'), buf, { + encoding: 'utf8' + }); + return buf; + } };