diff --git a/package-lock.json b/package-lock.json index f6fe3ad..08aa8a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,10 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "@types/ping": "^0.4.4", "discord.js": "^14.24.0", - "dotenv": "^17.2.2" + "dotenv": "^17.2.2", + "ping": "^1.0.0" }, "devDependencies": { "typescript": "^5.9.2" @@ -185,6 +187,12 @@ "undici-types": "~7.16.0" } }, + "node_modules/@types/ping": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/@types/ping/-/ping-0.4.4.tgz", + "integrity": "sha512-ifvo6w2f5eJYlXm+HiVx67iJe8WZp87sfa683nlqED5Vnt9Z93onkokNoWqOG21EaE8fMxyKPobE+mkPEyxsdw==", + "license": "MIT" + }, "node_modules/@types/ws": { "version": "8.18.1", "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.1.tgz", @@ -276,6 +284,15 @@ "integrity": "sha512-ThQLOhN86ZkJ7qemtVRGYM+gRgR8GEXNli9H/PMvpnZsE44Xfh3wx9kGJaldg314v85m+bFW6WBMaVHJc/c3zA==", "license": "MIT" }, + "node_modules/ping": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/ping/-/ping-1.0.0.tgz", + "integrity": "sha512-3dxdgGtV+7P/EVo42JhkGSomeO/0GGicSz3mI/yK+AI+VGNAOfakw5XfcbGI4IjyBY+ZZwvuRXdhnNF2uliKew==", + "license": "MIT", + "engines": { + "node": ">=22.0.0" + } + }, "node_modules/ts-mixer": { "version": "6.0.4", "resolved": "https://registry.npmjs.org/ts-mixer/-/ts-mixer-6.0.4.tgz", diff --git a/package.json b/package.json index 4874c59..c994231 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,9 @@ "typescript": "^5.9.2" }, "dependencies": { + "@types/ping": "^0.4.4", "discord.js": "^14.24.0", - "dotenv": "^17.2.2" + "dotenv": "^17.2.2", + "ping": "^1.0.0" } } diff --git a/src/commands/utility/ping.ts b/src/commands/utility/ping.ts index 2bdd046..f7bb0d2 100644 --- a/src/commands/utility/ping.ts +++ b/src/commands/utility/ping.ts @@ -1,4 +1,4 @@ -import { ApplicationIntegrationType, CommandInteraction, InteractionContextType, SlashCommandBuilder } from "discord.js"; +import { ApplicationIntegrationType, ChatInputCommandInteraction, CommandInteraction, InteractionContextType, SlashCommandBuilder } from "discord.js"; export default { data: new SlashCommandBuilder() @@ -13,7 +13,7 @@ export default { InteractionContextType.Guild, InteractionContextType.PrivateChannel ), - async execute(interaction : CommandInteraction) { + async execute(interaction : ChatInputCommandInteraction) { await interaction.reply('Pong !'); } } \ No newline at end of file diff --git a/src/commands/utility/statut.ts b/src/commands/utility/statut.ts new file mode 100644 index 0000000..039b842 --- /dev/null +++ b/src/commands/utility/statut.ts @@ -0,0 +1,83 @@ +import { ApplicationIntegrationType, ChatInputCommandInteraction, CommandInteraction, EmbedBuilder, InteractionContextType, SlashCommandBuilder } from "discord.js"; +import ping from "ping"; + +export default { + data: new SlashCommandBuilder() + .setName('status') + .setDescription('Give statut of servers.') + .setIntegrationTypes( + ApplicationIntegrationType.GuildInstall, + ApplicationIntegrationType.UserInstall + ) + .setContexts( + InteractionContextType.BotDM, + InteractionContextType.Guild, + InteractionContextType.PrivateChannel + ), + async execute(interaction : ChatInputCommandInteraction) { + await interaction.deferReply(); + + const hosts : {host: string, name: string, alive: boolean, type: 'ping' | 'website'}[] = [ + { + 'host': 'https://protojx.com', + 'name': 'Protojx Website 🌐', + alive: false, + type: 'website' + }, + { + 'host': 'https://manager.protojx.com', + 'name': 'Espace Client 💻', + alive: false, + type: 'website' + }, + { + host: 'node.thedrewen.com', + name: 'Ryzen 9 (Unknow ID) 🖥️', + alive: false, + type: 'ping' + }, + { + host: 'node.under-scape.com', + name: 'Xeon (Unknow ID) 🖥️', + alive: false, + type: 'ping' + }, + { + host: 'panel.protojx.com', + name: 'Game Ryzen 👾', + alive: false, + type: 'ping' + } + ] + + async function fetchAlive(host: {host: string, name: string, alive: boolean, type: 'ping' | 'website'}) { + if(host.type === 'ping'){ + let res = await ping.promise.probe(host.host, {timeout: 3}); + host.alive = res.alive; + }else if(host.type === 'website'){ + try { + const response = await fetch(host.host, { method: 'HEAD', signal: AbortSignal.timeout(3000) }); + host.alive = response.ok; + } catch (error) { + host.alive = false; + } + } + return host; + } + + const fetchPromises = hosts.map(host => fetchAlive(host)); + const hosts_ = await Promise.all(fetchPromises); + + const embed = new EmbedBuilder(); + embed.setTitle('Status of protojx servers'); + embed.setColor(0xffffff); + embed.setTimestamp(new Date()); + embed.setThumbnail(interaction.client.user.avatarURL()) + + for(let host of hosts_){ + embed.addFields({name: host.name, value: host.alive ? ' Online' : ' Offline', inline: false}); + } + + await interaction.editReply({embeds: [embed]}); + } +} \ No newline at end of file