This commit is contained in:
Louis Mazin 2026-04-16 01:04:30 +02:00
parent 7afd322545
commit 17dbff00a8
2 changed files with 100 additions and 41 deletions

View File

@ -2,9 +2,9 @@ require('dotenv').config();
const fs = require('node:fs'); const fs = require('node:fs');
const path = require('node:path'); const path = require('node:path');
const deploy = require('./src/discord/deploy_command.js') const deploy = require('./src/discord/deploy_command.js')
const update = require('./src/pterodactyl/displayer.js'); const { update, handlePinboardSelection } = require('./src/pterodactyl/displayer.js');
const clean = require('./src/pterodactyl/cleaner.js'); const clean = require('./src/pterodactyl/cleaner.js');
const { Client, GatewayIntentBits, Collection, Events, Partials, EmbedBuilder } = require('discord.js'); const { Client, GatewayIntentBits, Collection, Events, Partials } = require('discord.js');
const intents = [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions]; const intents = [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions];
@ -55,6 +55,11 @@ client.once('clientReady', async () => {
}); });
client.on(Events.InteractionCreate, async interaction => { client.on(Events.InteractionCreate, async interaction => {
if (interaction.isStringSelectMenu()) {
await handlePinboardSelection(interaction);
return;
}
if (!interaction.isChatInputCommand()) return; if (!interaction.isChatInputCommand()) return;
// Vérifier que la commande provient du bon serveur // Vérifier que la commande provient du bon serveur
@ -90,4 +95,5 @@ client.on(Events.InteractionCreate, async interaction => {
client.login(process.env.DISCORD_TOKEN); client.login(process.env.DISCORD_TOKEN);
setInterval(() => { update(client); }, 1000); const displayerIntervalMs = Number(process.env.DISPLAYER_INTERVAL_MS || 120000);
setInterval(() => { update(client); }, displayerIntervalMs);

View File

@ -1,39 +1,44 @@
const { EmbedBuilder } = require('discord.js'); const { EmbedBuilder, ActionRowBuilder, StringSelectMenuBuilder } = require('discord.js');
let pinboardCursor = 0; const PINBOARD_SELECT_CUSTOM_ID = 'pinboard-image-select';
const selectedImageIndexByMessage = new Map();
const parsePinboardImageUrls = () => { const parsePinboardImageUrls = () => {
const raw = process.env.PINBOARD_IMAGE_URLS; const raw = process.env.PINBOARD_IMAGE_URLS || '';
console.log(`🔍 URLs brutes du pinboard: ${raw}`); return raw
console.log(raw.split(',')); .split(',')
return raw.split(',').map(url => url.trim()).filter(url => url.length > 0); .map(url => url.trim())
.filter(url => /^https?:\/\//i.test(url));
}; };
const selectPinboardWindow = (urls, maxCount) => { const clampIndex = (index, length) => {
if (urls.length === 0 || maxCount <= 0) { if (length <= 0) return 0;
return []; if (Number.isNaN(index) || index < 0) return 0;
} if (index >= length) return length - 1;
return index;
const count = Math.min(maxCount, urls.length);
const selected = [];
for (let i = 0; i < count; i += 1) {
const index = (pinboardCursor + i) % urls.length;
selected.push(urls[index]);
}
pinboardCursor = (pinboardCursor + count) % urls.length;
return selected;
}; };
const buildPinboardEmbeds = (urls) => { const buildSelectedImageEmbed = (url, index, total) => (
const selectedUrls = selectPinboardWindow(urls, 4); new EmbedBuilder()
return selectedUrls.map((url, index) => ( .setColor('#f59e0b')
new EmbedBuilder() .setTitle(`Image selectionnee (${index + 1}/${total})`)
.setColor('#f59e0b') .setImage(url)
.setTitle(`Pin board ${index + 1}`) );
.setImage(url)
)); const buildImageSelectorRow = (urls, selectedIndex) => {
const options = urls.slice(0, 25).map((url, index) => ({
label: `Image ${index + 1}`,
value: String(index),
description: url.length > 95 ? `${url.slice(0, 92)}...` : url,
default: index === selectedIndex
}));
const selectMenu = new StringSelectMenuBuilder()
.setCustomId(PINBOARD_SELECT_CUSTOM_ID)
.setPlaceholder('Choisis une image')
.addOptions(options);
return new ActionRowBuilder().addComponents(selectMenu);
}; };
const getMinecraftStatus = async () => { const getMinecraftStatus = async () => {
@ -78,6 +83,27 @@ const buildPanelEmbed = (status) => {
return message; return message;
}; };
const buildPanelPayload = (status, messageId) => {
const panelEmbed = buildPanelEmbed(status);
const pinboardUrls = parsePinboardImageUrls();
if (pinboardUrls.length === 0) {
return { content: '', embeds: [panelEmbed], components: [] };
}
const currentIndex = clampIndex(selectedImageIndexByMessage.get(messageId) ?? 0, pinboardUrls.length);
selectedImageIndexByMessage.set(messageId, currentIndex);
const selectedImageEmbed = buildSelectedImageEmbed(pinboardUrls[currentIndex], currentIndex, pinboardUrls.length);
const selectorRow = buildImageSelectorRow(pinboardUrls, currentIndex);
return {
content: '',
embeds: [panelEmbed, selectedImageEmbed],
components: [selectorRow]
};
};
const resolvePanelMessage = async (client, channelId, messageId) => { const resolvePanelMessage = async (client, channelId, messageId) => {
const channel = await client.channels.fetch(channelId); const channel = await client.channels.fetch(channelId);
if (!channel || !channel.isTextBased()) { if (!channel || !channel.isTextBased()) {
@ -114,21 +140,48 @@ const update = async (client) => {
} }
const status = await getMinecraftStatus(); const status = await getMinecraftStatus();
const panelEmbed = buildPanelEmbed(status);
const pinboardUrls = parsePinboardImageUrls();
const pinboardEmbeds = buildPinboardEmbeds(pinboardUrls);
const { message } = await resolvePanelMessage(client, channelId, messageId); const { message } = await resolvePanelMessage(client, channelId, messageId);
const payload = buildPanelPayload(status, message.id);
await message.edit({ await message.edit(payload);
content: "",
embeds: [panelEmbed, ...pinboardEmbeds]
});
console.log(`📊 Panneau Minecraft mis a jour (${status.playersOnline}/${status.playersMax}) | Images: ${pinboardEmbeds.length}`); console.log(`📊 Panneau Minecraft mis a jour (${status.playersOnline}/${status.playersMax})`);
} catch (error) { } catch (error) {
console.log(`❌ Erreur displayer: ${error.message}`); console.log(`❌ Erreur displayer: ${error.message}`);
} }
}; };
module.exports = update; const handlePinboardSelection = async (interaction) => {
if (!interaction.isStringSelectMenu() || interaction.customId !== PINBOARD_SELECT_CUSTOM_ID) {
return false;
}
try {
const pinboardUrls = parsePinboardImageUrls();
if (pinboardUrls.length === 0) {
await interaction.reply({ content: 'Aucune image configuree dans PINBOARD_IMAGE_URLS.', ephemeral: true });
return true;
}
const selectedRaw = Number.parseInt(interaction.values?.[0] || '0', 10);
const selectedIndex = clampIndex(selectedRaw, pinboardUrls.length);
selectedImageIndexByMessage.set(interaction.message.id, selectedIndex);
const status = await getMinecraftStatus();
const payload = buildPanelPayload(status, interaction.message.id);
await interaction.update(payload);
return true;
} catch (error) {
console.log(`❌ Erreur selection pinboard: ${error.message}`);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Erreur lors de la mise a jour de l image.', ephemeral: true });
} else {
await interaction.reply({ content: 'Erreur lors de la mise a jour de l image.', ephemeral: true });
}
return true;
}
};
module.exports = { update, handlePinboardSelection };