From 74e748f9dae79f6cfe64d3ba5f5b86f9faf40bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Meunier?= Date: Tue, 12 Sep 2023 12:43:23 +0200 Subject: [PATCH 1/7] Add dedicated ping for api --- staty.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/staty.js b/staty.js index e3a079a..90f0411 100644 --- a/staty.js +++ b/staty.js @@ -41,7 +41,9 @@ const statyPing = async (apiData) => { message.edit(`🟠 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())}`); lastPingState = 1; removeMessage(downMessage); - downMessage = await channelState.send(`<@&${options.role}> \`${apiData.name.slice(8)}\` is now up !`); + if(apiData.role !== undefined) { downMessage = await channelState.send(`<@&${apiData.role}> \`${apiData.name.slice(8)}\` is now up !`); } + else { downMessage = await channelState.send(`<@&${options.role}> \`${apiData.name.slice(8)}\` is now up !`); } + } else { message.edit(`🟢 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())}`); @@ -55,18 +57,32 @@ const statyPing = async (apiData) => { } catch(error) { if(lastPingState === 2) { + if(apiData.role !== undefined) { + await message.edit(`🔥 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${apiData.role}> ➡️ See <#${channelConsole.id}> for more informations`); } + else { + await message.edit(`🔥 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); } await message.edit(`🔥 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); lastPingState = 3; downMessage.edit(`<@&${options.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 2\``); } else if(lastPingState === 3) { - await message.edit(`⚫ \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); + if(apiData.role !== undefined) { + await message.edit(`⚫ \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${apiData.role}> ➡️ See <#${channelConsole.id}> for more informations`); } + else { + await message.edit(`⚫ \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); } downMessage.edit(`<@&${options.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 3\``); } else { await message.edit(`🔴 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); logger(`An error occured on API ping for ${apiData.adress} → ${error.response.status} [${error.response.statusText}]`); lastPingState = 2; - downMessage = await channelState.send(`<@&${options.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 1\``); + if(apiData.role !== undefined) { + await message.edit(`🔴 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${apiData.role}> ➡️ See <#${channelConsole.id}> for more informations`); + downMessage = await channelState.send(`<@&${apiData.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 1\``); + } + else { + await message.edit(`🔴 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); + downMessage = await channelState.send(`<@&${options.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 1\``); + } } } }, options.wait); From bbe2a5f9fc69dd44e1f0ea24d524f8f60f2a2f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Meunier?= Date: Tue, 12 Sep 2023 12:54:42 +0200 Subject: [PATCH 2/7] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4f1886e..6d84b26 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ Staty is a discord bot that allows you to monitor the status of your APIs regula For configuration of Staty you must have these files : [data/secret.json](https://github.com/DigitalTeaCompany/staty/blob/main/config/secret.sample.json), [data/config.json](https://github.com/DigitalTeaCompany/staty/blob/main/config/global.sample.json), [data/api.json](https://github.com/DigitalTeaCompany/staty/blob/main/config/api.sample.json). +You can pass a `role_id` to ping dedicated group on an api. + #### data/secret.json To create your discord app : [Discord Developers](https://discord.com/developers/applications)
From 2a7ade684c582d36846bab80b8c586a8da4d5735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Meunier?= Date: Tue, 12 Sep 2023 12:54:46 +0200 Subject: [PATCH 3/7] Update api.sample.json --- config/api.sample.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/api.sample.json b/config/api.sample.json index 7085c10..10383f2 100644 --- a/config/api.sample.json +++ b/config/api.sample.json @@ -2,7 +2,8 @@ "api": [ { "adress": "", - "name": "" + "name": "", + "role": "" } ] } \ No newline at end of file From 8506183bc696cb5d0dae0b9a8658461b9be221fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Meunier?= Date: Wed, 13 Sep 2023 08:57:43 +0200 Subject: [PATCH 4/7] Update staty.js --- staty.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/staty.js b/staty.js index 90f0411..65290ae 100644 --- a/staty.js +++ b/staty.js @@ -61,7 +61,6 @@ const statyPing = async (apiData) => { await message.edit(`🔥 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${apiData.role}> ➡️ See <#${channelConsole.id}> for more informations`); } else { await message.edit(`🔥 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); } - await message.edit(`🔥 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); lastPingState = 3; downMessage.edit(`<@&${options.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 2\``); } else if(lastPingState === 3) { @@ -71,7 +70,6 @@ const statyPing = async (apiData) => { await message.edit(`⚫ \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); } downMessage.edit(`<@&${options.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 3\``); } else { - await message.edit(`🔴 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); logger(`An error occured on API ping for ${apiData.adress} → ${error.response.status} [${error.response.statusText}]`); lastPingState = 2; From 8154bac1488ad5e91faa1f2018a6d33d4c14b4c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Meunier?= Date: Thu, 14 Sep 2023 20:36:16 +0200 Subject: [PATCH 5/7] Delete removeMessage.js --- functions/removeMessage.js | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 functions/removeMessage.js diff --git a/functions/removeMessage.js b/functions/removeMessage.js deleted file mode 100644 index 15e5e24..0000000 --- a/functions/removeMessage.js +++ /dev/null @@ -1,5 +0,0 @@ -const removeMessage = async (message) => { - await message.delete(); -} - -module.exports = { removeMessage } \ No newline at end of file From a8341470935cab3981318d294bccad908f4456bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Meunier?= Date: Thu, 14 Sep 2023 20:36:19 +0200 Subject: [PATCH 6/7] Update staty.js --- staty.js | 134 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 91 insertions(+), 43 deletions(-) diff --git a/staty.js b/staty.js index 65290ae..bbaeb8a 100644 --- a/staty.js +++ b/staty.js @@ -1,36 +1,50 @@ const fs = require('fs'); -const { BOT_TOKEN, BOT_OWNER_ID, GUILD_ID, BOT_ID } = require('./config/secret.json'); +const { BOT_TOKEN } = require('./config/secret.json'); const { version, options, channels } = require('./config/global.json'); const apiSettings = JSON.parse(fs.readFileSync('config/api.json')); const axios = require('axios'); -const { Client, EmbedBuilder, GatewayIntentBits, Partials, time } = require('discord.js'); +const { Client, EmbedBuilder, GatewayIntentBits, Partials, ChannelType, time } = require('discord.js'); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildVoiceStates], partials: [Partials.Message, Partials.Channel, Partials.Reaction], }); const { dateParser } = require('./functions/dateParser.js'); -const { removeMessage } = require('./functions/removeMessage.js'); const { logger, loggerBoot } = require('./functions/logger.js'); let channelConsole, channelDebug, channelState; const statyPing = async (apiData) => { try { - let message = await channelState.send(`🚀 \`${apiData.name.slice(8)}\` - Launch ping at ${time(new Date())}`); let lastPingState = 0; - let messageIsHere, downMessage; + let apiIsDown = undefined; + let apiIsUp = undefined; - setInterval(async () => { - try { - messageIsHere = await channelState.messages.fetch(message.id); - } - catch(error) { logger(error); } + const pingInit = new EmbedBuilder() + .setColor(options.color) + .setTitle(`🚀 ${apiData.name.slice(8)}`) + .setDescription(`Staty now monitoring api`); + const pingEmbed = new EmbedBuilder() + .setColor(options.color) + .setDescription(time(new Date())); + const messagePingInit = await channelState.send({ embeds: [pingInit, pingEmbed] }); - if(messageIsHere === '' || messageIsHere === undefined) { - message = await channelState.send(`🚀 \`${apiData.name.slice(8)}\` - Launch ping at ${time(new Date())}`); - } + const threadPing = await messagePingInit.startThread({ + name: `🤖 ${apiData.name.slice(8)}`, + autoArchiveDuration: 60, + reason: `Dedicated thread for pinging api ${apiData.name.slice(8)}`, + type: ChannelType.PrivateThread, + }); + const initThreadEmbed = new EmbedBuilder() + .setColor(options.color) + .setTitle(apiData.name.slice(8)) + .setDescription(`You are added to this threads to monitoring api`); + const message = await threadPing.send({ + content: `<@&${apiData.role === undefined ? (options.role) : (apiData.role)}>`, + embeds: [initThreadEmbed] }); + const lastPing = await threadPing.send({ embeds: [pingEmbed] }); + setInterval(async () => { try { const request = await axios({ method: 'get', @@ -38,52 +52,81 @@ const statyPing = async (apiData) => { }); if(lastPingState > 1) { - message.edit(`🟠 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())}`); + const pingInit = new EmbedBuilder() + .setColor(options.color) + .setTitle(`🟠 ${apiData.name.slice(8)}`) + .setDescription(`Staty now monitoring api`); + const pingEmbed = new EmbedBuilder() + .setColor(options.color) + .setDescription(time(new Date())); + messagePingInit.edit({ embeds: [pingInit, pingEmbed] }); lastPingState = 1; - removeMessage(downMessage); - if(apiData.role !== undefined) { downMessage = await channelState.send(`<@&${apiData.role}> \`${apiData.name.slice(8)}\` is now up !`); } - else { downMessage = await channelState.send(`<@&${options.role}> \`${apiData.name.slice(8)}\` is now up !`); } - + + apiIsUp = await threadPing.send({ content: `@here API is now UP !` }); + + if(apiIsDown !== undefined) { + apiIsDown.delete(); + apiIsDown = undefined; + } } else { - message.edit(`🟢 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())}`); + const pingInit = new EmbedBuilder() + .setColor(options.color) + .setTitle(`🟢 ${apiData.name.slice(8)}`) + .setDescription(`Staty now monitoring api`); + const pingEmbed = new EmbedBuilder() + .setColor(options.color) + .setDescription(time(new Date())); + messagePingInit.edit({ embeds: [pingInit, pingEmbed] }); lastPingState = 1; - if(downMessage !== undefined) { - removeMessage(downMessage); - downMessage = undefined; + if(apiIsUp !== undefined) { + apiIsUp.delete(); + apiIsUp = undefined; } } } catch(error) { if(lastPingState === 2) { - if(apiData.role !== undefined) { - await message.edit(`🔥 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${apiData.role}> ➡️ See <#${channelConsole.id}> for more informations`); } - else { - await message.edit(`🔥 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); } + const pingInit = new EmbedBuilder() + .setColor(options.color) + .setTitle(`🔥 ${apiData.name.slice(8)}`) + .setDescription(`Staty now monitoring api`); + const pingEmbed = new EmbedBuilder() + .setColor(options.color) + .setDescription(time(new Date())); + messagePingInit.edit({ embeds: [pingInit, pingEmbed] }); lastPingState = 3; - downMessage.edit(`<@&${options.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 2\``); } else if(lastPingState === 3) { - if(apiData.role !== undefined) { - await message.edit(`⚫ \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${apiData.role}> ➡️ See <#${channelConsole.id}> for more informations`); } - else { - await message.edit(`⚫ \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); } - downMessage.edit(`<@&${options.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 3\``); + const pingInit = new EmbedBuilder() + .setColor(options.color) + .setTitle(`⚫ ${apiData.name.slice(8)}`) + .setDescription(`Staty now monitoring api`); + const pingEmbed = new EmbedBuilder() + .setColor(options.color) + .setDescription(time(new Date())); + messagePingInit.edit({ embeds: [pingInit, pingEmbed] }); + lastPingState = 3; } else { - logger(`An error occured on API ping for ${apiData.adress} → ${error.response.status} [${error.response.statusText}]`); + const pingInit = new EmbedBuilder() + .setColor(options.color) + .setTitle(`🔴 ${apiData.name.slice(8)}`) + .setDescription(`Staty now monitoring api`); + const pingEmbed = new EmbedBuilder() + .setColor(options.color) + .setDescription(time(new Date())); + messagePingInit.edit({ embeds: [pingInit, pingEmbed] }); lastPingState = 2; - if(apiData.role !== undefined) { - await message.edit(`🔴 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${apiData.role}> ➡️ See <#${channelConsole.id}> for more informations`); - downMessage = await channelState.send(`<@&${apiData.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 1\``); - } - else { - await message.edit(`🔴 \`${apiData.name.slice(8)}\` - Last ping at ${time(new Date())} - <@&${options.role}> ➡️ See <#${channelConsole.id}> for more informations`); - downMessage = await channelState.send(`<@&${options.role}> \`${apiData.name.slice(8)}\` is down ! \`ping 1\``); - } + apiIsDown = await threadPing.send({ content: `@here API is now down !` }); + const downConsole = new EmbedBuilder() + .setColor(options.color) + .setTitle(`API is down !`) + .setDescription(`\`\`\`An error occured on API ping for ${apiData.adress} → ${error.response.status} [${error.response.statusText}]\`\`\``); + threadPing.send({ embeds: [downConsole] }); } } - }, options.wait); + }, 60000); } catch(error) { await logger(error); } } @@ -108,8 +151,13 @@ const booter = async () => { channelDebug.send({ embeds: [bootEmbed] }); logger('Hello here ! 😊'); + const allThreads = channelState.threads.cache; + await allThreads.map(thread => { + thread.delete(); + }); + await channelState.messages.fetch().then(messages => { - messages.map(message => removeMessage(message)); + messages.map(message => { message.delete(); }); }); // Lancement de tout les pings From 5e99c44dd6ee37ff8255e6bb009d4999d2b8e4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Meunier?= Date: Thu, 14 Sep 2023 20:43:48 +0200 Subject: [PATCH 7/7] Update staty.js --- staty.js | 1 - 1 file changed, 1 deletion(-) diff --git a/staty.js b/staty.js index bbaeb8a..8d76d63 100644 --- a/staty.js +++ b/staty.js @@ -42,7 +42,6 @@ const statyPing = async (apiData) => { const message = await threadPing.send({ content: `<@&${apiData.role === undefined ? (options.role) : (apiData.role)}>`, embeds: [initThreadEmbed] }); - const lastPing = await threadPing.send({ embeds: [pingEmbed] }); setInterval(async () => { try {