Skip to content

Commit

Permalink
Merge pull request #5 from DigitalTeaCompany/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jeremiemeunier authored Sep 14, 2023
2 parents 9a28cfc + 5e99c44 commit c9fad2f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 35 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)<br />
Expand Down
3 changes: 2 additions & 1 deletion config/api.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"api": [
{
"adress": "",
"name": ""
"name": "",
"role": ""
}
]
}
5 changes: 0 additions & 5 deletions functions/removeMessage.js

This file was deleted.

119 changes: 90 additions & 29 deletions staty.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,131 @@
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] });

setInterval(async () => {
try {
const request = await axios({
method: 'get',
url: apiData.adress
});

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);
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) {
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) {
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 {
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}]`);
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;

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); }
}
Expand All @@ -94,8 +150,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
Expand Down

0 comments on commit c9fad2f

Please sign in to comment.