command stats

This commit is contained in:
derpystuff 2023-01-10 20:03:30 +01:00
parent 54c788f9f4
commit 8bf8516f84
3 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,54 @@
const { codeblock } = require('../../../labscore/utils/markdown')
const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed')
const { paginator } = require('../../../labscore/client');
const { editOrReply } = require('../../../labscore/utils/message');
const { getCommandStatistics } = require('../../../labscore/analytics');
module.exports = {
name: 'stats',
metadata: {
description: 'Lists command usage statistics.',
description_short: 'Shows command usage stats.',
examples: ['stats'],
category: 'core',
usage: 'stats'
},
run: async (context) => {
try{
let stats = await getCommandStatistics();
let pages = [];
num = 0;
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 = `${num}`
if(`${num}`.length == 1){dispnum = ` ${num}`}
list.push(`${dispnum} | ${elem} - ${stats[elem]}`)
num++
})
pages.push({embeds:[
createEmbed("default", context, {
description: codeblock("autohotkey", list)
})
]})
}
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.")]})
}
}
};

34
labscore/analytics.js Normal file
View file

@ -0,0 +1,34 @@
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."
}
}

View file

@ -55,6 +55,10 @@ const { maintower } = require('./logging');
const { icon } = require('./utils/markdown'); const { icon } = require('./utils/markdown');
const { editOrReply } = require('./utils/message'); 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 // Delete command responses if the user chooses to delete their trigger or edits the command away
commandClient.on('commandDelete', async ({context, reply}) => { commandClient.on('commandDelete', async ({context, reply}) => {
if(context.message?.deleted && !reply.deleted || !context.message.content.startsWith(commandPrefix)) reply.delete(); if(context.message?.deleted && !reply.deleted || !context.message.content.startsWith(commandPrefix)) reply.delete();