diff --git a/src/modules/info/stats.ts b/src/modules/info/stats.ts index 77cae05..254ad1a 100644 --- a/src/modules/info/stats.ts +++ b/src/modules/info/stats.ts @@ -3,6 +3,19 @@ import { Context, Usage } from '../../utils/types'; import lingua from '../../utils/lingua'; import { MessageEmbed } from 'discord.js'; import CommandUsage from '../../utils/command.usage'; +import { commitHash } from "../../utils/git" + +function uptime(ms: number) { + function pad(s: number) { + return (s < 10 ? '0' : '') + s; + } + var days = Math.floor(ms / (24 * ((60 * 60)))); + var hours = Math.floor(ms / (60 * 60)); + var minutes = Math.floor((ms % (60 * 60)) / 60); + var seconds = Math.floor(ms % 60); + return `${pad(days)}d ${pad(hours)}h ${pad(minutes)}m ${pad(seconds)}s` + // return pad(hours) + 'h ' + pad(minutes) + 'm ' + pad(seconds) + 's'; +} export = class Stats extends Command { constructor() { @@ -15,8 +28,24 @@ export = class Stats extends Command { } async command(ctx: Context) { - let Stats = await CommandUsage(ctx.client.commands) - console.log(Stats) + // @ts-ignore + const guilds = (await ctx.client.shard?.fetchClientValues(`guilds.cache.size`)).reduce((a, b) => a + b, 0) + const shards = ctx.client.shard?.count + const users = (await ctx.client.shard?.fetchClientValues(`users.cache.size`))?.reduce((a, b) => a + b, 0) + let MiscValues = [ + `Uptime: **${uptime(process.uptime())}**`, + `Guilds: **${guilds}**`, + `Shards: **${shards}**`, + `Users: **${users}**` + ] + + let embed = new MessageEmbed().setColor(ctx.config.variables.color).setFooter(`${ctx.config.variables.name} v${ctx.config.pkg.version} [${commitHash}]`, ctx.config.variables.avatar) + .setTitle(`Statistics`) + .setDescription(`Visit [our System Dashboard](https://system.thaldr.in) for more in-depth stats`) + .addField(`Misc`, MiscValues.map((value) => `${value}`)) + + + ctx.channel.send(embed) } } \ No newline at end of file diff --git a/src/utils/git.ts b/src/utils/git.ts new file mode 100644 index 0000000..8389004 --- /dev/null +++ b/src/utils/git.ts @@ -0,0 +1,3 @@ +import { execSync } from 'child_process'; + +export const commitHash = execSync('git rev-parse HEAD', { encoding: 'utf8' }).slice(0, 8); \ No newline at end of file diff --git a/src/utils/prometheus.ts b/src/utils/prometheus.ts index 0567f00..d3969f7 100644 --- a/src/utils/prometheus.ts +++ b/src/utils/prometheus.ts @@ -9,6 +9,7 @@ export default class Prometheus { public messagesSeen!: prom.Counter public guildCount!: prom.Gauge public totalGuilds!: prom.Gauge + public uptime!: prom.Gauge // public commmandsRan: prom.Counter #server!: ReturnType @@ -32,6 +33,10 @@ export default class Prometheus { name: "thaldrin_guilds_total", help: "Total Number of Guilds Thaldrin is in" }) + this.uptime = new prom.Gauge({ + name: "thaldrin_uptime", + help: "Thaldrin's Uptime" + }) this.#server = createServer(this.onRequest.bind(this)); this.#server.once('listening', () => Logger.info({ type: 'event:prometheusStart', message: `Prometheus: Listening at http://localhost:${vars.prometheus.port}` })); @@ -39,6 +44,7 @@ export default class Prometheus { this.#server.listen(vars.prometheus.port); } private async onRequest(req: IncomingMessage, res: ServerResponse) { + this.uptime.set(process.uptime()) if (req.url! === '/metrics') { res.writeHead(200, { 'Content-Type': prom.register.contentType }); res.write(await prom.register.metrics());