mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2025-09-01 08:21:27 +03:00
"stats" command
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const Command = require('../modules/commands/command');
|
||||
const Keyv = require('keyv');
|
||||
const { MessageEmbed } = require('discord.js');
|
||||
|
||||
module.exports = class StatsCommand extends Command {
|
||||
@@ -13,10 +14,70 @@ module.exports = class StatsCommand extends Command {
|
||||
args: [],
|
||||
staff_only: true
|
||||
});
|
||||
|
||||
this.cache = new Keyv({
|
||||
namespace: 'cache.commands.stats'
|
||||
});
|
||||
}
|
||||
|
||||
async execute(message, args) {
|
||||
async execute(message) {
|
||||
let settings = await message.guild.settings;
|
||||
const i18n = this.client.i18n.getLocale(settings.locale);
|
||||
|
||||
const messages = await this.client.db.models.Message.findAndCountAll();
|
||||
|
||||
let stats = await this.cache.get(message.guild.id);
|
||||
|
||||
if (!stats) {
|
||||
let tickets = await this.client.db.models.Ticket.findAndCountAll({
|
||||
where: {
|
||||
guild: message.guild.id
|
||||
}
|
||||
});
|
||||
|
||||
stats = { // maths
|
||||
tickets: tickets.count,
|
||||
messages: settings.log_messages
|
||||
? await messages.rows
|
||||
.reduce(async (acc, row) => (await this.client.db.models.Ticket.findOne({
|
||||
where: {
|
||||
id: row.ticket
|
||||
}
|
||||
})).guild === message.guild.id
|
||||
? await acc + 1
|
||||
: await acc, 0)
|
||||
: null,
|
||||
|
||||
response_time: Math.floor(tickets.rows.reduce((acc, row) => row.first_response
|
||||
? acc + ((Math.abs(new Date(row.createdAt) - new Date(row.first_response)) / 1000) / 60)
|
||||
: acc, 0) / tickets.count)
|
||||
};
|
||||
await this.cache.set(message.guild.id, stats, 60 * 60 * 1000); // cache for an hour
|
||||
}
|
||||
|
||||
let guild_embed = new MessageEmbed()
|
||||
.setColor(settings.colour)
|
||||
.setTitle(i18n('commands.stats.response.guild.title'))
|
||||
.setDescription(i18n('commands.stats.response.guild.description'))
|
||||
.addField(i18n('commands.stats.fields.tickets'), stats.tickets, true)
|
||||
.addField(i18n('commands.stats.fields.response_time.title'), i18n('commands.stats.fields.response_time.minutes', stats.response_time), true)
|
||||
.setFooter(settings.footer, message.guild.iconURL());
|
||||
|
||||
if (stats.messages) guild_embed.addField(i18n('commands.stats.fields.messages'), stats.messages, true);
|
||||
|
||||
await message.channel.send(guild_embed);
|
||||
|
||||
if (this.client.guilds.cache.size > 1) {
|
||||
await message.channel.send(
|
||||
new MessageEmbed()
|
||||
.setColor(settings.colour)
|
||||
.setTitle(i18n('commands.stats.response.global.title'))
|
||||
.setDescription(i18n('commands.stats.response.global.description'))
|
||||
.addField(i18n('commands.stats.fields.tickets'), stats.tickets, true)
|
||||
.addField(i18n('commands.stats.fields.response_time.title'), i18n('commands.stats.fields.response_time.minutes', stats.response_time), true)
|
||||
.addField(i18n('commands.stats.fields.messages'), stats.messages, true)
|
||||
.setFooter(settings.footer, message.guild.iconURL())
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
@@ -330,8 +330,25 @@
|
||||
"aliases": {},
|
||||
"args": {},
|
||||
"description": "Display ticket statistics",
|
||||
"fields": {
|
||||
"messages": "Messages",
|
||||
"response_time": {
|
||||
"minutes": "%s minutes",
|
||||
"title": "Avg. response time"
|
||||
},
|
||||
"tickets": "Tickets"
|
||||
},
|
||||
"name": "stats",
|
||||
"response": {}
|
||||
"response": {
|
||||
"global": {
|
||||
"description": "Statistics about tickets across all guilds where this Discord TIckets instance is used.",
|
||||
"title": "📊 Global stats"
|
||||
},
|
||||
"guild": {
|
||||
"description": "Statistics about tickets within this guild. This data is cached for an hour.",
|
||||
"title": "📊 This server's stats"
|
||||
}
|
||||
}
|
||||
},
|
||||
"topic": {
|
||||
"aliases": {},
|
||||
|
Reference in New Issue
Block a user