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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26