This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bot info: Added the bot info command * formatting * removed version info * Patched some docstrings up
- Loading branch information
1 parent
de3f238
commit f7d8233
Showing
5 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |