Files
Yumi/src/discord/Ping.ts
T

136 lines
4.9 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-06-27 20:49:20 +07:00
import { I18n } from 'i18n';
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-06-27 20:49:20 +07:00
import Locale from '../services/Locale';
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 = {
2022-06-27 20:49:20 +07:00
PING_INFO: (data: HybridInteractionMessage, locale: I18n, description: string) => {
2022-01-25 19:55:26 +07:00
return makeSuccessEmbed({
icon: '🌎',
2022-06-27 20:49:20 +07:00
title: locale.__('ping.title'),
2022-01-25 19:55:26 +07:00
description: description,
2022-06-27 20:49:20 +07:00
user: data.getUser()
2022-01-25 19:55:26 +07:00
});
2022-03-03 20:56:49 +07:00
},
2022-06-27 20:49:20 +07:00
PINGING: (data: HybridInteractionMessage, locale: I18n) => {
2022-03-03 20:56:49 +07:00
return makeProcessingEmbed({
icon: data.isMessage() ? undefined : '⌛',
2022-06-27 20:49:20 +07:00
title: locale.__('ping.title_processing'),
user: data.getUser()
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-27 20:49:20 +07:00
const guild = data.getGuild();
const member = data.getMember();
if(!guild || !member) return;
const locale = await Locale.getGuildLocale(guild.id);
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-06-06 14:59:53 +07:00
let _placeholder = await sendHybridInteractionMessageResponse(data, {
2022-06-27 20:49:20 +07:00
embeds: [EMBEDS.PINGING(data, locale)],
2022-06-06 14:59:53 +07:00
});
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(
2022-06-27 20:49:20 +07:00
`💻 ${locale.__('ping.running_on', {HOST: os.hostname()})} ` +
`${
Environment.get().NODE_ENV === 'development' ? ` / ${locale.__('ping.development_environment')}` : ''
2022-06-06 14:59:53 +07:00
}`
);
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()
2022-06-27 20:49:20 +07:00
.editReply({ embeds: [EMBEDS.PING_INFO(data, locale, 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()
2022-06-27 20:49:20 +07:00
.edit({ embeds: [EMBEDS.PING_INFO(data, locale, finalString.join('\n'))] });
2022-01-25 19:55:26 +07:00
}
2022-06-06 14:59:53 +07:00
}