test
This commit is contained in:
parent
a47391d7b2
commit
301803629f
@ -14,7 +14,6 @@ const warmingMessageIds = new Set();
|
||||
const warmupRefreshKeyByMessage = new Map();
|
||||
let lastKnownStatus = null;
|
||||
let imageRefreshKey = Date.now();
|
||||
const MAX_EMBEDS_PER_MESSAGE = 10;
|
||||
|
||||
const getCacheBustValue = () => {
|
||||
const bucketSeconds = Number.parseInt(process.env.PINBOARD_CACHE_BUST_SECONDS || '60', 10);
|
||||
@ -176,80 +175,6 @@ const preloadPinboardImages = async (items) => {
|
||||
}
|
||||
};
|
||||
|
||||
const buildWarmupEmbeds = (items, startIndex, refreshKey) => {
|
||||
const batchItems = items.slice(startIndex, startIndex + MAX_EMBEDS_PER_MESSAGE);
|
||||
|
||||
return batchItems.map((item, offset) => {
|
||||
const absoluteIndex = startIndex + offset;
|
||||
const warmupUrl = toRefreshKeyedUrl(toCacheBustedUrl(item.url), refreshKey);
|
||||
|
||||
return new EmbedBuilder()
|
||||
.setColor('#1f2937')
|
||||
.setTitle(`Cache image ${absoluteIndex + 1}/${items.length}`)
|
||||
.setImage(warmupUrl);
|
||||
});
|
||||
};
|
||||
|
||||
const resolveWarmupMessage = async (client) => {
|
||||
const cleanerChannelFallback = (process.env.CLEANER_CHANNEL_IDS || '')
|
||||
.split(',')
|
||||
.map(id => id.trim())
|
||||
.find(Boolean);
|
||||
const channelId = process.env.PINBOARD_WARMUP_CHANNEL_ID || cleanerChannelFallback;
|
||||
const messageId = process.env.PINBOARD_WARMUP_MESSAGE_ID;
|
||||
|
||||
if (!channelId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const channel = await client.channels.fetch(channelId);
|
||||
if (!channel || !channel.isTextBased()) {
|
||||
throw new Error(`Salon warm-up non textuel ou introuvable: ${channelId}`);
|
||||
}
|
||||
|
||||
if (messageId) {
|
||||
try {
|
||||
const message = await channel.messages.fetch(messageId);
|
||||
return { channel, message };
|
||||
} catch (error) {
|
||||
console.log(`⚠️ Message warm-up ${messageId} introuvable. Creation d'un nouveau message.`);
|
||||
}
|
||||
}
|
||||
|
||||
const newMessage = await channel.send({
|
||||
content: 'Initialisation du cache images...'
|
||||
});
|
||||
|
||||
console.log(`✅ Nouveau message warm-up cree: ID ${newMessage.id}`);
|
||||
console.log(`👉 Ajoute PINBOARD_WARMUP_MESSAGE_ID=${newMessage.id} dans ton .env pour le lier de facon persistante.`);
|
||||
|
||||
return { channel, message: newMessage };
|
||||
};
|
||||
|
||||
const runRemoteWarmup = async (client, pinboardItems, refreshKey) => {
|
||||
if (!client) return false;
|
||||
if (pinboardItems.length <= 1) return false;
|
||||
|
||||
const target = await resolveWarmupMessage(client);
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { message } = target;
|
||||
|
||||
for (let startIndex = 0; startIndex < pinboardItems.length; startIndex += MAX_EMBEDS_PER_MESSAGE) {
|
||||
const embeds = buildWarmupEmbeds(pinboardItems, startIndex, refreshKey);
|
||||
await message.edit({
|
||||
content: `Warm-up cache images (${Math.min(startIndex + MAX_EMBEDS_PER_MESSAGE, pinboardItems.length)}/${pinboardItems.length})`,
|
||||
embeds,
|
||||
components: []
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`🔥 Warm-up distant termine pour message ${message.id}`);
|
||||
return true;
|
||||
};
|
||||
|
||||
const waitMs = async (delayMs) => new Promise(resolve => {
|
||||
setTimeout(resolve, delayMs);
|
||||
});
|
||||
@ -380,26 +305,21 @@ const getMinecraftStatus = async () => {
|
||||
};
|
||||
};
|
||||
|
||||
const buildPanelEmbed = (status, imageUrl = null) => {
|
||||
const buildPanelEmbed = (status) => {
|
||||
const stateText = status.online ? "🟢 En ligne" : "🔴 Hors ligne";
|
||||
const playersText = `${status.playersOnline}/${status.playersMax}`;
|
||||
const playersListText = status.playerList.length > 0 ? status.playerList.map(element => element.name).join(", ") : "Aucun joueur connecte";
|
||||
const message = new EmbedBuilder()
|
||||
.setColor('#0099ff')
|
||||
.setDescription('# Informations sur le Serveur \n\n## :wireless: IP :\n### goofymon.louismazin.ovh\n\n## :repeat: État :\n### '+stateText+'\n\n## :busts_in_silhouette: Joueurs :\n### '+playersText+'\n\n## :video_game: Version :\n### '+status.version+'\n\n## :bust_in_silhouette: Liste des joueurs :\n### '+playersListText);
|
||||
|
||||
if (imageUrl) {
|
||||
message.setImage(imageUrl);
|
||||
}
|
||||
|
||||
return message;
|
||||
};
|
||||
|
||||
const buildPanelPayload = (status, messageId, pinboardItems = null, options = {}) => {
|
||||
const panelEmbed = buildPanelEmbed(status);
|
||||
const items = (pinboardItems || parsePinboardImageItems()).slice(0, 25);
|
||||
|
||||
if (items.length === 0) {
|
||||
const panelEmbed = buildPanelEmbed(status);
|
||||
return { content: '', embeds: [panelEmbed], components: [] };
|
||||
}
|
||||
|
||||
@ -413,8 +333,6 @@ const buildPanelPayload = (status, messageId, pinboardItems = null, options = {}
|
||||
disableCacheBust: options.disableCacheBustForSelected,
|
||||
refreshKey: options.refreshKey
|
||||
});
|
||||
const selectedImageUrl = selectedImageEmbed.data?.image?.url || null;
|
||||
const panelEmbed = buildPanelEmbed(status, selectedImageUrl);
|
||||
const selectorRow = buildImageSelectorRow(items, currentIndex);
|
||||
const navigationRow = buildImageNavigationRow(items, currentIndex);
|
||||
|
||||
@ -425,7 +343,7 @@ const buildPanelPayload = (status, messageId, pinboardItems = null, options = {}
|
||||
|
||||
return {
|
||||
content: '',
|
||||
embeds: [panelEmbed],
|
||||
embeds: [panelEmbed, selectedImageEmbed],
|
||||
components: [selectorRow, navigationRow],
|
||||
files
|
||||
};
|
||||
@ -485,15 +403,8 @@ const update = async (client) => {
|
||||
await message.edit(payload);
|
||||
|
||||
const initialIndex = clampIndex(selectedImageIndexByMessage.get(message.id) ?? 0, pinboardItems.length);
|
||||
runRemoteWarmup(client, pinboardItems, imageRefreshKey)
|
||||
.then(didUseRemoteWarmup => {
|
||||
if (!didUseRemoteWarmup) {
|
||||
return runVisualWarmup(message, status, pinboardItems, initialIndex, imageRefreshKey);
|
||||
}
|
||||
|
||||
return null;
|
||||
})
|
||||
.catch(error => console.log(`⚠️ Warm-up en erreur: ${error.message}`));
|
||||
runVisualWarmup(message, status, pinboardItems, initialIndex, imageRefreshKey)
|
||||
.catch(error => console.log(`⚠️ Warm-up visuel en erreur: ${error.message}`));
|
||||
|
||||
console.log(`📊 Panneau Minecraft mis a jour (${status.playersOnline}/${status.playersMax})`);
|
||||
} catch (error) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user