/[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 284 - (show annotations)
Mon Jul 29 17:29:21 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 3958 byte(s)
fix: system command not showing correct uptime
1 import { CommandInteraction, GuildMember, Interaction, 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 { timeProcess } from '../../utils/util';
9 import { formatDistanceStrict, formatDistanceToNowStrict, formatDuration, intervalToDuration } from 'date-fns';
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 memoryFree = Math.round((process.memoryUsage().heapUsed / 1024 / 1024 * 100) / 100);
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: 'Available Memory',
90 value: `${1024 - memoryFree}MB / 1.0GB`
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