diff --git a/commands/message/core/stats.js b/commands/message/core/stats.js index 153ce77..96e044f 100644 --- a/commands/message/core/stats.js +++ b/commands/message/core/stats.js @@ -1,15 +1,26 @@ -const { codeblock } = require('../../../labscore/utils/markdown') -const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed') +const { highlight, iconPill } = require('../../../labscore/utils/markdown') +const { createEmbed } = require('../../../labscore/utils/embed') -const { paginator } = require('../../../labscore/client'); const { editOrReply } = require('../../../labscore/utils/message'); -const { getCommandStatistics } = require('../../../labscore/analytics'); + +function format(seconds){ + function pad(s){ + return (s < 10 ? '0' : '') + s; + } + var hours = Math.floor(seconds / (60*60)); + var minutes = Math.floor(seconds % (60*60) / 60); + var seconds = Math.floor(seconds % 60); + + if(hours == 0) return pad(minutes) + ' Minutes, ' + pad(seconds) + ' Seconds' + return pad(hours) + ' Hours, ' + pad(minutes) + ' Minutes, ' + pad(seconds) + ' Seconds' +} module.exports = { name: 'stats', + aliases: ['usage', 'uptime'], metadata: { - description: 'Lists command usage statistics.', - description_short: 'Shows command usage stats.', + description: 'Shows statistics about the bot.', + description_short: 'Shows bot stats.', examples: ['stats'], category: 'core', usage: 'stats' @@ -17,39 +28,51 @@ module.exports = { run: async (context) => { context.triggerTyping(); try{ - let stats = await getCommandStatistics(); - - let pages = []; - let ranking = 1; - - for (var i = 0; i < Object.keys(stats).length; i += 20) { - list = [] - if(pages.length == 0){list.push(` | Total - ${Object.values(stats).reduce((a, b) => a + b, 0)}`)} - - Object.keys(stats).forEach(function(elem){ - dispnum = `${ranking}` - if(`${ranking}`.length == 1){dispnum = ` ${ranking}`} - list.push(`${dispnum} | ${elem} - ${stats[elem]}`) - ranking++ + if(context.manager){ + const globalStats = await context.manager.broadcastEval((cluster) => { + const shardUsage = process.memoryUsage(); + + let stats = { + usage: shardUsage.heapTotal + shardUsage.external + shardUsage.arrayBuffers, + guilds: 0 + } + for (const [shardId, shard] of cluster.shards) { + stats.guilds += shard.guilds.length; + } + return stats }) - pages.push({embeds:[ - createEmbed("default", context, { - description: codeblock("autohotkey", list) - }) - ]}) + let formatted = { + guilds: 0, + usage: 0 + } + + for (let cid in globalStats) { + const cstats = globalStats[cid]; + formatted.guilds += cstats.guilds + formatted.usage += cstats.usage + } + + const display = [ + `${iconPill("house", "Servers ")} ${highlight(` ${formatted.guilds} `)}`, + `${iconPill("robot", "Shard ")} ${highlight(` ${context.shardId + 1}/${context.manager.cluster.shardCount} `)}`, + `${iconPill("connection", "Memory Usage")} ${highlight(` ${Math.round(formatted.usage / 1024 / 1024)}MB `)}`, + `${iconPill("timer", "Uptime ")} ${highlight(` ${format(process.uptime())} `)}` + ] + + return editOrReply(context, createEmbed("default", context, { + //description: codeblock("autohotkey", [`Guilds: ${formatted.guilds}`, `Shard: ${context.shardId + 1}/${context.manager.cluster.shardCount}`, `Memory Usage: ${Math.round(formatted.usage / 1024 / 1024)}MB`]) + description: display.join('\n') + })) + + } else { + return editOrReply(context, createEmbed("error", context, "Bot is not running in cluster mode.")) } - - pages = formatPaginationEmbeds(pages) - const paging = await paginator.createPaginator({ - context, - pages - }); return; }catch(e){ console.log(e) - return editOrReply(context, {embeds: [createEmbed("error", context, "Unable to fetch command statistics.")]}) + return editOrReply(context, {embeds: [createEmbed("error", context, "Unable to fetch bot statistics.")]}) } } }; \ No newline at end of file diff --git a/commands/message/dev/uptime.js b/commands/message/dev/uptime.js deleted file mode 100644 index 6314bec..0000000 --- a/commands/message/dev/uptime.js +++ /dev/null @@ -1,36 +0,0 @@ -const { editOrReply } = require('../../../labscore/utils/message') -const { highlight } = require('../../../labscore/utils/markdown') - -function format(seconds){ - function pad(s){ - return (s < 10 ? '0' : '') + s; - } - var hours = Math.floor(seconds / (60*60)); - var minutes = Math.floor(seconds % (60*60) / 60); - var seconds = Math.floor(seconds % 60); - - return pad(hours) + ' Hours, ' + pad(minutes) + ' Minutes, ' + pad(seconds) + ' Seconds' -} - -module.exports = { - name: "uptime", - metadata: { - description: 'Displays the bots uptime.', - description_short: 'Bot uptime', - examples: ['uptime'], - category: 'dev', - usage: 'uptime' - }, - onBefore: context => context.user.isClientOwner, - onCancel: context => - context.reply( - `${context.user.mention}, you are lacking the permission \`BOT_OWNER\`.` - ), - run: async (context, args) => { - await context.triggerTyping(); - - return await editOrReply(context, { - "content": `up for ${highlight(format(process.uptime()))}` - }) - } -}; \ No newline at end of file diff --git a/labscore/analytics.js b/labscore/analytics.js deleted file mode 100644 index d9a8b67..0000000 --- a/labscore/analytics.js +++ /dev/null @@ -1,34 +0,0 @@ -const superagent = require('superagent') - -const ANALYTICS_BASE_URL = process.env.ANALYTICS_SERVER - -let analyticsClient = "prod" -if(process.env.ANALYTICS_OVERRIDE) analyticsClient = process.env.ANALYTICS_OVERRIDE - -module.exports.track = async function (command){ - try{ - let res = await superagent.get(ANALYTICS_BASE_URL + '/stats/collect/' + command) - .set({ - "Authorization": process.env.ANALYTICS_KEY, - "x-nx-client": analyticsClient - }) - }catch(e){ - console.log('Analytics report failed.') - console.log(e) - } -} - -module.exports.getCommandStatistics = async function (time){ - if(!time) time = analyticsClient + '_' + new Date().getFullYear() + '/' + new Date().getMonth() - try{ - let res = await superagent.get(ANALYTICS_BASE_URL + '/stats/' + encodeURIComponent(time)) - .set({ - "Authorization": process.env.ANALYTICS_KEY, - "x-nx-client": analyticsClient - }) - return res.body.collection - }catch(e){ - console.log(e) - throw "Analytics request failed." - } -} \ No newline at end of file diff --git a/labscore/client.js b/labscore/client.js index a9bbdc0..5233754 100644 --- a/labscore/client.js +++ b/labscore/client.js @@ -55,10 +55,6 @@ const { maintower } = require('./logging'); const { icon } = require('./utils/markdown'); const { editOrReply } = require('./utils/message'); -const { track } = require('./analytics') -// analytics -commandClient.on('commandRan', async ({context, command}) => track(command.name)) - // Delete command responses if the user chooses to delete their trigger or edits the command away commandClient.on('commandDelete', async ({context, reply}) => { if(context.message?.deleted && !reply.deleted || !context.message.content.startsWith(commandPrefix)) reply.delete();