/[sudobot]/trunk/src/commands/settings/SystemCommand.ts
ViewVC logotype

Contents of /trunk/src/commands/settings/SystemCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 366 - (show annotations)
Mon Jul 29 17:29:48 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 4040 byte(s)
fix: system command memory usage
1 import { CommandInteraction, Message } from 'discord.js';
2 import BaseCommand from '../../utils/structures/BaseCommand';
3 import DiscordClient from '../../client/Client';
4 import CommandOptions from '../../types/CommandOptions';
5 import InteractionOptions from '../../types/InteractionOptions';
6 import MessageEmbed from '../../client/MessageEmbed';
7 import { fetchEmoji } from '../../utils/Emoji';
8 import { formatDuration, intervalToDuration } from 'date-fns';
9 import os from 'os';
10
11 export default class SystemCommand extends BaseCommand {
12 constructor() {
13 super('system', 'settings', []);
14 this.supportsInteractions = true;
15 }
16
17 async run(client: DiscordClient, message: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
18 let msg: Message;
19
20 if (message instanceof Message) {
21 msg = await message.reply({
22 embeds: [
23 new MessageEmbed()
24 .setColor('GOLD')
25 .setDescription('Loading data...')
26 ]
27 });
28 }
29 else {
30 await message.reply({
31 embeds: [
32 new MessageEmbed()
33 .setColor('GOLD')
34 .setDescription('Loading data...')
35 ]
36 });
37 msg = <Message> await message.fetchReply();
38 }
39
40 const latency = msg.createdTimestamp - message.createdTimestamp;
41 const apiLatency = Math.round(client.ws.ping);
42 let latencyIcon = '🟢', apiLatencyIcon = '🟢';
43
44 if (latency >= 500) {
45 latencyIcon = '🔴';
46 }
47 else if (latency >= 350) {
48 latencyIcon = '🟡';
49 }
50
51 if (apiLatency >= 400) {
52 apiLatencyIcon = '🔴';
53 }
54 else if (apiLatency >= 300) {
55 apiLatencyIcon = '🟡';
56 }
57
58 const memoryTotal = Math.round(os.totalmem() / 1024 / 1024);
59 const memoryUsed = Math.round((os.totalmem() - os.freemem()) / 1024 / 1024);
60 const memoryUsedByBot = Math.round(process.memoryUsage().heapUsed / 1024 / 1024);
61
62 const msgoptions: any = {
63 embeds: [
64 new MessageEmbed()
65 .setAuthor({
66 iconURL: client.user!.displayAvatarURL(),
67 name: 'System status'
68 })
69 .setDescription((latencyIcon !== '🔴' ? (await fetchEmoji('check'))?.toString() + ' All systems operational' : ':x: Some systems are down/slow'))
70 .addFields([
71 {
72 name: 'Command Type',
73 value: `${!options.isInteraction ? 'Legacy (Message-based)' : 'Slash Command'}`
74 },
75 {
76 name: 'Uptime',
77 value: `${formatDuration(intervalToDuration({
78 start: 0,
79 end: process.uptime() * 1000
80 }))}`
81 },
82 {
83 name: 'Latency',
84 value: `${latencyIcon} ${latency}ms`
85 },
86 {
87 name: 'API Latency',
88 value: `${apiLatencyIcon} ${apiLatency}ms`
89 },
90 {
91 name: 'Memory Usage',
92 value: `${memoryUsed}MB / ${memoryTotal}MB (${memoryUsedByBot}MB used by the bot)`
93 },
94 {
95 name: 'System Platform',
96 value: `${process.platform}`
97 },
98 {
99 name: 'NodeJS Version',
100 value: `${process.version}`
101 }
102 ])
103 ]
104 };
105
106 if (msg instanceof CommandInteraction)
107 msgoptions.content = '';
108
109 await this.deferReply(msg, msgoptions, true);
110 }
111 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26