From 417e24c8a1811ebcf31f540c66ce74be183fa822 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Tue, 21 Jul 2020 04:12:56 -0400 Subject: [PATCH] Add stats cmd --- src/class/Client.ts | 24 +++++++++++++++++++++--- src/class/Route.ts | 1 + src/commands/page.ts | 1 + src/commands/stats.ts | 36 ++++++++++++++++++++++++++++++++++++ src/events/CommandHandler.ts | 2 ++ src/models/Stat.ts | 13 +++++++++++++ src/models/index.ts | 1 + 7 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/commands/stats.ts create mode 100644 src/models/Stat.ts diff --git a/src/class/Client.ts b/src/class/Client.ts index 28470cc..d117ff0 100644 --- a/src/class/Client.ts +++ b/src/class/Client.ts @@ -2,7 +2,7 @@ import eris from 'eris'; import mongoose from 'mongoose'; import { promises as fs } from 'fs'; import { Collection, Command, LocalStorage, Util, ServerManagement, Event } from '.'; -import { File, FileInterface, Member, MemberInterface, Moderation, ModerationInterface, PagerNumber, PagerNumberInterface, Rank, RankInterface, Redirect, RedirectInterface } from '../models'; +import { File, FileInterface, Member, MemberInterface, Moderation, ModerationInterface, PagerNumber, PagerNumberInterface, Rank, RankInterface, Redirect, RedirectInterface, Stat, StatInterface } from '../models'; import { Config } from '../../types'; // eslint-disable-line export default class Client extends eris.Client { @@ -18,18 +18,36 @@ export default class Client extends eris.Client { public serverManagement: ServerManagement; - public db: { File: mongoose.Model, Member: mongoose.Model, Moderation: mongoose.Model, PagerNumber: mongoose.Model, Rank: mongoose.Model, Redirect: mongoose.Model, local: { muted: LocalStorage } }; + public db: { File: mongoose.Model, Member: mongoose.Model, Moderation: mongoose.Model, PagerNumber: mongoose.Model, Rank: mongoose.Model, Redirect: mongoose.Model, Stat: mongoose.Model, local: { muted: LocalStorage } }; constructor(token: string, options?: eris.ClientOptions) { super(token, options); this.commands = new Collection(); this.events = new Collection(); this.intervals = new Collection(); - this.db = { File, Member, Moderation, PagerNumber, Rank, Redirect, local: { muted: new LocalStorage('muted') } }; + this.db = { File, Member, Moderation, PagerNumber, Rank, Redirect, Stat, local: { muted: new LocalStorage('muted') } }; } public async loadDatabase() { await mongoose.connect(this.config.mongoDB, { useNewUrlParser: true, useUnifiedTopology: true, poolSize: 50 }); + + const statMessages = await this.db.Stat.findOne({ name: 'messages' }); + const statCommands = await this.db.Stat.findOne({ name: 'commands' }); + const statPages = await this.db.Stat.findOne({ name: 'pages' }); + const statRequests = await this.db.Stat.findOne({ name: 'requests' }); + + if (!statMessages) { + await (new this.db.Stat({ name: 'messages', value: 0 }).save()); + } + if (!statCommands) { + await (new this.db.Stat({ name: 'commands', value: 0 }).save()); + } + if (!statPages) { + await (new this.db.Stat({ name: 'pages', value: 0 }).save()); + } + if (!statRequests) { + await (new this.db.Stat({ name: 'requests' }).save()); + } } public loadPlugins() { diff --git a/src/class/Route.ts b/src/class/Route.ts index 29423e5..97a24d0 100644 --- a/src/class/Route.ts +++ b/src/class/Route.ts @@ -20,6 +20,7 @@ export default class Route { public init() { this.router.all('*', (req, res, next) => { this.server.client.util.signale.log(`'${req.method}' request from '${req.ip}' to '${req.hostname}${req.path}'.`); + this.server.client.db.Stat.updateOne({ name: 'requests' }, { $inc: { value: 1 } }); if (this.conf.maintenance === true) res.status(503).json({ code: this.constants.codes.MAINTENANCE_OR_UNAVAILABLE, message: this.constants.messages.MAINTENANCE_OR_UNAVAILABLE }); else if (this.conf.deprecated === true) res.status(501).json({ code: this.constants.codes.DEPRECATED, message: this.constants.messages.DEPRECATED }); else next(); diff --git a/src/commands/page.ts b/src/commands/page.ts index bcb32bc..1f5e398 100644 --- a/src/commands/page.ts +++ b/src/commands/page.ts @@ -146,6 +146,7 @@ export default class Page extends Command { html: `

Page

${options?.emergencyNumber ? `

[SEN#${options.emergencyNumber}]` : ''}Recipient PN: ${recipientNumber}
Sender PN: ${senderNumber} (${sender ? `${sender.username}#${sender.discriminator}` : ''})
Initial Command: https://discordapp.com/channels/${message.guild.id}/${message.channel.id}/${message.id} (<#${message.channel.id}>)

Pager Code: ${code} (${this.local.codeDict.get(code)})${txt ? `
Message: ${txt}` : ''}`, }); } + this.client.db.Stat.updateOne({ name: 'pages' }, { $inc: { value: 1 } }); return { status: true, message: `Page to \`${recipientNumber}\` sent.` }; } catch (err) { this.client.util.signale.error(err); diff --git a/src/commands/stats.ts b/src/commands/stats.ts new file mode 100644 index 0000000..ba3b28c --- /dev/null +++ b/src/commands/stats.ts @@ -0,0 +1,36 @@ +import { Message } from 'eris'; +import { Client, Command, RichEmbed } from '../class'; + +export default class Stats extends Command { + constructor(client: Client) { + super(client); + this.name = 'stats'; + this.description = 'Provides system statistics.'; + this.usage = `${this.client.config.prefix}stats`; + this.permissions = 0; + this.guildOnly = false; + this.enabled = true; + } + + public async run(message: Message) { + try { + const messages = await this.client.db.Stat.findOne({ name: 'messages' }); + const commands = await this.client.db.Stat.findOne({ name: 'commands' }); + const pages = await this.client.db.Stat.findOne({ name: 'pages' }); + const requests = await this.client.db.Stat.findOne({ name: 'requests' }); + + const embed = new RichEmbed(); + embed.setTitle('Statistics'); + embed.setThumbnail(this.client.user.avatarURL); + embed.addField('Messages Seen', `${messages.value}`, true); + embed.addField('Command Executed', `${commands.value}`, true); + embed.addField('HTTP Requests Served', `${requests.value}`, true); + embed.addField('Pages Sent', `${pages.value}`, true); + embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setTimestamp(); + return message.channel.createMessage({ embed }); + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +} diff --git a/src/events/CommandHandler.ts b/src/events/CommandHandler.ts index 2c7b792..176b0ac 100644 --- a/src/events/CommandHandler.ts +++ b/src/events/CommandHandler.ts @@ -12,6 +12,7 @@ export default class CommandHandler extends Event { public async run(message: Message) { try { + this.client.db.Stat.updateOne({ name: 'messages' }, { $inc: { value: 1 } }); if (message.author.bot) return; if (message.content.indexOf(this.client.config.prefix) !== 0) return; const noPrefix: string[] = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g); @@ -25,6 +26,7 @@ export default class CommandHandler extends Event { } this.client.util.signale.log(`User '${message.author.username}#${message.author.discriminator}' ran command '${resolved.cmd.name}' in '${message.channel.id}'.`); await resolved.cmd.run(message, resolved.args); + this.client.db.Stat.updateOne({ name: 'commands' }, { $inc: { value: 1 } }); } catch (err) { this.client.util.handleError(err, message); } diff --git a/src/models/Stat.ts b/src/models/Stat.ts new file mode 100644 index 0000000..7109699 --- /dev/null +++ b/src/models/Stat.ts @@ -0,0 +1,13 @@ +import { Document, Schema, model } from 'mongoose'; + +export interface StatInterface extends Document { + name: 'messages' | 'commands' | 'pages' | 'requests', + value: number, +} + +const Stat: Schema = new Schema({ + name: String, + value: Number, +}); + +export default model('Stat', Stat); diff --git a/src/models/index.ts b/src/models/index.ts index 8bf8622..fe3f79d 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -4,3 +4,4 @@ export { default as Moderation, ModerationInterface } from './Moderation'; export { default as PagerNumber, PagerNumberInterface, PagerNumberRaw } from './PagerNumber'; export { default as Rank, RankInterface } from './Rank'; export { default as Redirect, RedirectInterface, RedirectRaw } from './Redirect'; +export { default as Stat, StatInterface } from './Stat';