Skip to content

Commit

Permalink
improve log readability
Browse files Browse the repository at this point in the history
  • Loading branch information
BodomBeach committed Jul 25, 2024
1 parent 3ea9c3d commit f80440d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 30 deletions.
25 changes: 16 additions & 9 deletions src/events/commands/archive.js
Original file line number Diff line number Diff line change
@@ -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 };
47 changes: 31 additions & 16 deletions src/events/commands/balise.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
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();
};

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 {
Expand All @@ -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
Expand All @@ -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 };
9 changes: 6 additions & 3 deletions src/jobs/channelCleanup.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,7 +29,7 @@ async function channelCleanup(client) {
forbidden1.exec(channel.name) ||
forbidden2.exec(channel.name)
) {
console.log(`format invalide ${channel.name}`);
invalidChannelsCount++;
continue;
}

Expand All @@ -48,6 +48,9 @@ async function channelCleanup(client) {
}
}
}
console.log(
`${invalidChannelsCount} channels ignored (invalid title, cannot archive)`
);
}

module.exports = { channelCleanup };
2 changes: 0 additions & 2 deletions src/jobs/serverStats.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -70,7 +69,6 @@ async function serverStats(client) {
} else {
messages.first().edit({ embeds: [embed] });
}
console.log("Server stats updated");
}

module.exports = { serverStats };

0 comments on commit f80440d

Please sign in to comment.