Files
Yumi/src/discord/Ping.ts
T

129 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-06-06 14:59:53 +07:00
import { CommandInteraction, Message, Interaction } from 'discord.js';
import os from 'os';
import NodePing from 'ping';
import { Promise } from 'bluebird';
2022-03-03 20:56:49 +07:00
2022-06-06 14:59:53 +07:00
import Configuration from '../providers/Configuration';
import DiscordProvider from '../providers/Discord';
import Environment from '../providers/Environment';
2022-03-03 20:56:49 +07:00
2022-06-06 14:59:53 +07:00
import DiscordModule, { HybridInteractionMessage } from '../utils/DiscordModule';
import {
makeSuccessEmbed,
makeProcessingEmbed,
sendHybridInteractionMessageResponse
} from '../utils/DiscordMessage';
2022-01-25 19:55:26 +07:00
enum MeasureType {
2022-06-06 14:59:53 +07:00
Ping = 'ping',
DiscordHTTPPing = 'discordhttp',
DiscordWebsocket = 'discordwebsocket'
2022-01-25 19:55:26 +07:00
}
const EMBEDS = {
PING_INFO: (data: Message | Interaction, description: string) => {
return makeSuccessEmbed({
icon: '🌎',
title: `Network performance`,
description: description,
2022-06-06 14:59:53 +07:00
user: data instanceof Interaction ? data.user : data.author
2022-01-25 19:55:26 +07:00
});
2022-03-03 20:56:49 +07:00
},
PINGING: (data: HybridInteractionMessage) => {
return makeProcessingEmbed({
icon: data.isMessage() ? undefined : '⌛',
title: `Measuring network performance`,
2022-06-06 14:59:53 +07:00
user: data.isInteraction() ? data.getInteraction().user : data.getMessage().author
2022-03-03 20:56:49 +07:00
});
2022-01-25 19:55:26 +07:00
}
2022-06-06 14:59:53 +07:00
};
2022-01-25 19:55:26 +07:00
2022-03-03 20:56:49 +07:00
export default class Ping extends DiscordModule {
2022-06-06 14:59:53 +07:00
public id = 'Discord_Ping';
public commands = ['ping'];
public commandInteractionName = 'ping';
2022-03-03 20:56:49 +07:00
async GuildOnModuleCommand(args: any, message: Message) {
await this.run(new HybridInteractionMessage(message), args);
2022-01-25 19:55:26 +07:00
}
2022-03-03 20:56:49 +07:00
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
await this.run(new HybridInteractionMessage(interaction), interaction.options);
2022-01-25 19:55:26 +07:00
}
2022-03-03 20:56:49 +07:00
async run(data: HybridInteractionMessage, args: any) {
2022-06-06 14:59:53 +07:00
let placeholder: HybridInteractionMessage | undefined;
2022-01-25 19:55:26 +07:00
let beforeEditDate = Date.now();
2022-03-03 20:56:49 +07:00
2022-06-06 14:59:53 +07:00
let _placeholder = await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.PINGING(data)]
});
if (_placeholder) placeholder = new HybridInteractionMessage(_placeholder);
2022-03-03 20:56:49 +07:00
2022-01-25 19:55:26 +07:00
let afterEditDate = Date.now();
2022-03-03 20:56:49 +07:00
let finalString = [];
2022-01-25 19:55:26 +07:00
2022-06-06 14:59:53 +07:00
await Promise.map(
Configuration.getConfig('Ping'),
(entry: any) => {
return new Promise(async (resolve, reject) => {
let stringCurrent = `${entry.title}`;
2022-01-25 19:55:26 +07:00
2022-06-06 14:59:53 +07:00
if (entry.type === MeasureType.Ping) {
const res = await NodePing.promise.probe(entry.host!, { min_reply: 4 });
2022-01-25 19:55:26 +07:00
2022-06-06 14:59:53 +07:00
if (!res.alive) {
stringCurrent += 'Failed';
return finalString.push(stringCurrent);
}
if (
res.avg === 'unknown' ||
res.min === 'unknown' ||
res.max === 'unknown'
) {
stringCurrent += 'Failed';
return finalString.push(stringCurrent);
}
stringCurrent += `${parseFloat(res.avg).toFixed(1)}ms (${parseFloat(
res.min
).toFixed(1)}ms - ${parseFloat(res.max).toFixed(1)}ms)`;
finalString.push(stringCurrent);
} else if (entry.type === MeasureType.DiscordWebsocket) {
stringCurrent += `${Math.round(DiscordProvider.client.ws.ping).toFixed(
1
)}ms`;
finalString.push(stringCurrent);
} else if (entry.type === MeasureType.DiscordHTTPPing) {
stringCurrent += `${Math.abs(afterEditDate - beforeEditDate).toFixed(1)}ms`;
finalString.push(stringCurrent);
2022-02-27 17:32:18 +07:00
}
2022-01-25 19:55:26 +07:00
2022-06-06 14:59:53 +07:00
resolve();
});
},
{ concurrency: 5 }
);
2022-02-02 21:17:51 +07:00
2022-06-06 14:59:53 +07:00
finalString.push('');
finalString.push(
'💻 Running on ' +
`${os.hostname()}${
Environment.get().NODE_ENV === 'development' ? ' / Development Environment' : ''
}`
);
2022-01-25 19:55:26 +07:00
2022-03-03 20:56:49 +07:00
if (data.isSlashCommand())
2022-06-06 14:59:53 +07:00
return await data
.getMessageComponentInteraction()
.editReply({ embeds: [EMBEDS.PING_INFO(data.getRaw(), finalString.join('\n'))] });
2022-03-03 20:56:49 +07:00
else if (data && data.isMessage() && placeholder && placeholder.isMessage())
2022-06-06 14:59:53 +07:00
return await placeholder
.getMessage()
.edit({ embeds: [EMBEDS.PING_INFO(data.getRaw(), finalString.join('\n'))] });
2022-01-25 19:55:26 +07:00
}
2022-06-06 14:59:53 +07:00
}