From f80440d87b84aaf8b60f986ca3f979c078a5ed64 Mon Sep 17 00:00:00 2001 From: bodombeach Date: Thu, 25 Jul 2024 09:28:57 +0200 Subject: [PATCH] improve log readability --- src/events/commands/archive.js | 25 +++++++++++------- src/events/commands/balise.js | 47 ++++++++++++++++++++++------------ src/jobs/channelCleanup.js | 9 ++++--- src/jobs/serverStats.js | 2 -- 4 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/events/commands/archive.js b/src/events/commands/archive.js index 48262e6..6fceb77 100644 --- a/src/events/commands/archive.js +++ b/src/events/commands/archive.js @@ -1,30 +1,37 @@ // TODO -> Archive by transforming channel into a thread in the 'archives' channel -const { SlashCommandBuilder } = require('discord.js'); -const { archive } = require('../../utils/archive.js'); +const { SlashCommandBuilder } = require("discord.js"); +const { archive } = require("../../utils/archive.js"); // Creates an Object in JSON with the data required by Discord's API to create a SlashCommand const create = () => { const command = new SlashCommandBuilder() - .setName('archive') - .setDescription('Archive ce salon') + .setName("archive") + .setDescription("Archive ce salon"); return command.toJSON(); }; // Called by the interactionCreate event listener when the corresponding command is invoked const invoke = async (interaction) => { - console.log(`${interaction.user.username} used /archive`); + console.log( + `${interaction.user.username} used /archive on ${interaction.channel.name}` + ); - const allowedCategories = ['🪂 SORTIES', '🏃Sorties pas rapente', '🏆 Compétitions']; + const allowedCategories = [ + "🪂 SORTIES", + "🏃Sorties pas rapente", + "🏆 Compétitions", + ]; if (!allowedCategories.includes(interaction.channel.parent.name)) { return interaction.reply({ - content: 'Seuls les salons sorties, évenements et compétitions peuvent être archivés !', + content: + "Seuls les salons sorties, évenements et compétitions peuvent être archivés !", ephemeral: true, }); } else { archive(interaction.channel); - await interaction.reply({ content: 'Salon archivé !', ephemeral: true }); - }; + await interaction.reply({ content: "Salon archivé !", ephemeral: true }); + } }; module.exports = { create, invoke }; diff --git a/src/events/commands/balise.js b/src/events/commands/balise.js index 0982fcf..1a63bf2 100644 --- a/src/events/commands/balise.js +++ b/src/events/commands/balise.js @@ -1,12 +1,18 @@ -const { SlashCommandBuilder, AttachmentBuilder, EmbedBuilder } = require('discord.js'); -const puppeteer = require('puppeteer'); +const { + SlashCommandBuilder, + AttachmentBuilder, + EmbedBuilder, +} = require("discord.js"); +const puppeteer = require("puppeteer"); -const url = 'http://www.murblanc.org/sthil'; +const url = "http://www.murblanc.org/sthil"; const create = () => { const command = new SlashCommandBuilder() - .setName('balise') - .setDescription(`Affiche les valeurs des balises autour de Grenoble (source : ${url})`); + .setName("balise") + .setDescription( + `Affiche les valeurs des balises autour de Grenoble (source : ${url})` + ); return command.toJSON(); }; @@ -14,24 +20,29 @@ const invoke = async (interaction) => { console.log(`${interaction.user.username} used /balise`); interaction.deferReply(); - const image = await getScreenhot('table table tbody'); + const image = await getScreenhot("table table tbody"); const embed = new EmbedBuilder() - .setDescription(`${interaction.user.username} a utilisé la commande **/balise**`) + .setDescription( + `${interaction.user.username} a utilisé la commande **/balise**` + ) .setImage(`attachment://dashboard.png`); - const attachment = new AttachmentBuilder(image, 'screenshot.png'); + const attachment = new AttachmentBuilder(image, "screenshot.png"); interaction.editReply({ embeds: [embed], files: [attachment] }); }; const getScreenhot = async (selector) => { // no-sandbox args are necessary to launch puppeteer as root (docker) - const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', args: ['--no-sandbox', '--disable-setuid-sandbox'] }); + const browser = await puppeteer.launch({ + executablePath: "/usr/bin/google-chrome", + args: ["--no-sandbox", "--disable-setuid-sandbox"], + }); const page = await browser.newPage(); await page.setViewport({ width: 1920, height: 1080, - deviceScaleFactor: 1 + deviceScaleFactor: 1, }); try { @@ -40,14 +51,18 @@ const getScreenhot = async (selector) => { await page.waitForSelector(selector); } catch (error) { console.error(error); - }; + } const nodeHandle = await page.$(selector); // Some css changes to the element await nodeHandle.evaluate((node) => { - node.querySelectorAll('tr td:first-child').forEach((td) => td.style.fontSize = '38px'); - node.querySelectorAll('tr td:last-child').forEach((td) => td.style.fontSize = '20px'); + node + .querySelectorAll("tr td:first-child") + .forEach((td) => (td.style.fontSize = "38px")); + node + .querySelectorAll("tr td:last-child") + .forEach((td) => (td.style.fontSize = "20px")); }); // Take a screenshot of the element @@ -57,12 +72,12 @@ const getScreenhot = async (selector) => { x: boundingBox.x, y: boundingBox.y, width: boundingBox.width, - height: boundingBox.height - } + height: boundingBox.height, + }, }); await browser.close(); return screenshot; -} +}; module.exports = { create, invoke }; diff --git a/src/jobs/channelCleanup.js b/src/jobs/channelCleanup.js index 696859f..a6b8522 100644 --- a/src/jobs/channelCleanup.js +++ b/src/jobs/channelCleanup.js @@ -1,14 +1,14 @@ const { archive } = require("../utils/archive.js"); async function channelCleanup(client) { - console.log("===== starting scheduled channel cleanup ====="); - const allowedCategories = ["🪂 SORTIES", "🏃Sorties pas rapente"]; const guild = client.guilds.cache.get(process.env.GUILD_ID); const categories = guild.channels.cache.filter((channel) => allowedCategories.includes(channel.name) ); + let invalidChannelsCount = 0; + const allowed = /(\d{1,2})-(\d{1,2})/; // do nothing for these format for now, too risky @@ -29,7 +29,7 @@ async function channelCleanup(client) { forbidden1.exec(channel.name) || forbidden2.exec(channel.name) ) { - console.log(`format invalide ${channel.name}`); + invalidChannelsCount++; continue; } @@ -48,6 +48,9 @@ async function channelCleanup(client) { } } } + console.log( + `${invalidChannelsCount} channels ignored (invalid title, cannot archive)` + ); } module.exports = { channelCleanup }; diff --git a/src/jobs/serverStats.js b/src/jobs/serverStats.js index 7fc8516..3062c5c 100644 --- a/src/jobs/serverStats.js +++ b/src/jobs/serverStats.js @@ -1,7 +1,6 @@ const { EmbedBuilder } = require("discord.js"); async function serverStats(client) { - console.log("===== starting scheduled server stats update ====="); const guild = client.guilds.cache.get(process.env.GUILD_ID); // fetch members first, otherwise guild.roles.cache.get('1232623387965132820').members.size will be empty @@ -70,7 +69,6 @@ async function serverStats(client) { } else { messages.first().edit({ embeds: [embed] }); } - console.log("Server stats updated"); } module.exports = { serverStats };