Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Adds the botcommand module (#17)
Browse files Browse the repository at this point in the history
* Bot info: Added the bot info command

* formatting

* removed version info

* Patched some docstrings up
  • Loading branch information
Cpt-Dingus authored Aug 26, 2023
1 parent de3f238 commit f7d8233
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config.default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
"youtube": {
"enabled": true
},
"bot": {
"enabled": true
},
"autopaste": {
"enabled": true,
// Make sure the ids are all strings, not numbers
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"fix": "gts fix"
},
"dependencies": {
"@types/node-os-utils": "^1.3.1",
"bufferutil": "^4.0.7",
"chalk": "^4.1.2",
"discord.js": "^14.11.0",
"googleapis": "^124.0.0",
"jsonc-parser": "^3.2.0",
"mongodb": "^5.6.0",
"node": "^19.8.1",
"node-os-utils": "^1.3.7",
"typescript": "^5.0.2",
"undici": "^5.22.1",
"utf-8-validate": "^6.0.3",
Expand Down
136 changes: 136 additions & 0 deletions src/modules/meta/bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* @file
* Modules:
* - {@link bot} - Info
*/

import {Colors, EmbedBuilder, EmbedField} from 'discord.js';
import process from 'node:process';
import osutils from 'node-os-utils';
import * as util from '../../core/util.js';

/** Function that returns an array of the latest 5 ticks' latencies */
async function getTickLatency(): Promise<number[]> {
return new Promise<number[]>(resolve => {
let iteration = 0;
const tickDelay: number[] = [];

/** Function to push the tick latency to tickDelay, resolve on the fifth tick */
function measureIteration() {
// Unix timestamp of start in ms
const startRaw = process.hrtime();
const start = startRaw[0] * 1_000 + startRaw[1] / 1_000_000;

process.nextTick(() => {
// Unix timestamp of iteration in ms
const lagTime = process.hrtime();
const lag = lagTime[0] * 1_000 + lagTime[1] / 1_000_000;

tickDelay.push(lag - start);
iteration++;

if (iteration < 5) {
measureIteration();
} else {
resolve(tickDelay);
}
});
}

measureIteration();
});
}

/** The root bot command definition */
const bot = new util.RootModule(
'bot',
'Bot info and management command group',
[],
[]
);

bot.registerSubModule(
new util.SubModule(
'info',
'Prints information about the bot',
[],
async (_, interaction) => {
const embed: EmbedBuilder = new EmbedBuilder()
.setColor(Colors.Blurple)
.setTitle(util.client.user!.username)
.setThumbnail(util.client.user!.displayAvatarURL());

// Gets the Average tick delay
const tickDelay = await getTickLatency();

// Averages the delays out and rounds it to 4 decimal points
const sum: number = tickDelay.reduce((a, b) => a + b, 0);
const averageDelay: string = (sum / tickDelay.length).toFixed(4);

// Gets the CPU usage
let cpuUsage = 'Unable to get the cpu usage';

await osutils.cpu.usage().then(cpuPercentage => {
cpuUsage = cpuPercentage.toString();
});

const memoryUsage: number = process.memoryUsage.rss() / 1_000_000; // MB

const fields: EmbedField[] = [
{
name: 'Started',
value: util.client.readyAt!.toString(),
inline: true,
},
{
name: 'IRC',
value: 'IRC is not implemented yet you dingus',
inline: true,
},
// Line break
{
name: '\u200B',
value: '\u200B',
inline: false,
},
{
name: 'Bot latency',
value: `${Date.now() - interaction.createdTimestamp} ms`,
inline: true,
},
{
name: 'API latency',
value: `${util.client.ws.ping} ms`,
inline: true,
},
// Line break
{
name: '\u200B',
value: '\u200B',
inline: false,
},
{
name: 'Average tick delay',
value: `${averageDelay} ms`,
inline: true,
},
{
name: 'CPU Usage',
value: `${cpuUsage}%`,
inline: true,
},
{
name: 'Memory usage',
value: `${memoryUsage.toFixed(2)} MB`,
inline: true,
},
];

embed.setFields(fields);

await util.replyToInteraction(interaction, {embeds: [embed]});
}
)
);

export default bot;
9 changes: 9 additions & 0 deletions src/modules/meta/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @file This file exports all meta modules
* (This has to be done because of how modules are resolved - only the file with the same name as
* its folder will get resolved)
*/

import bot from './bot.js';

export default bot;

0 comments on commit f7d8233

Please sign in to comment.