cleaner, list of channels

This commit is contained in:
Louis Mazin 2025-12-09 13:40:53 +01:00
parent 3c57719feb
commit b801d4b6f1

View File

@ -1,25 +1,64 @@
const axios = require('axios'); const axios = require('axios');
const clean = async (client) => { const cleanChannel = async (client, channelId) => {
try { try {
client.channels.fetch(process.env.CLEANER_CHANNEL_ID) const channel = await client.channels.fetch(channelId);
.then(channel => {
channel.messages.fetch({ limit: 100 }) if (!channel) {
.then(messages => { console.log(`⚠️ Salon ${channelId} introuvable`);
// Trier les messages par date (du plus ancien au plus récent) return;
const sortedMessages = Array.from(messages.values()).sort((a, b) => a.createdTimestamp - b.createdTimestamp); }
// Exclure le premier message et les messages épinglés const messages = await channel.messages.fetch({ limit: 100 });
const messagesToDelete = sortedMessages.slice(1).filter(m => !m.pinned);
// Trier les messages par date (du plus ancien au plus récent)
if (messagesToDelete.length > 0) { const sortedMessages = Array.from(messages.values()).sort((a, b) => a.createdTimestamp - b.createdTimestamp);
channel.bulkDelete(messagesToDelete);
} // Exclure le premier message et les messages épinglés
}) const messagesToDelete = sortedMessages.slice(1).filter(m => !m.pinned);
})
if (messagesToDelete.length > 0) {
await channel.bulkDelete(messagesToDelete);
console.log(`🧹 ${messagesToDelete.length} message(s) supprimé(s) dans #${channel.name}`);
} else {
console.log(`✅ Aucun message à supprimer dans #${channel.name}`);
}
} catch (error) { } catch (error) {
console.log("Bot : "+error); console.log(`❌ Erreur lors du nettoyage du salon ${channelId}: ${error.message}`);
} }
}; };
const clean = async (client) => {
try {
const channelIds = process.env.CLEANER_CHANNEL_IDS;
if (!channelIds) {
console.log('⚠️ CLEANER_CHANNEL_IDS non défini dans .env');
return;
}
// Séparer les IDs et nettoyer les espaces
const channelIdList = channelIds.split(',').map(id => id.trim()).filter(id => id);
if (channelIdList.length === 0) {
console.log('⚠️ Aucun salon à nettoyer');
return;
}
console.log(`🧹 Nettoyage de ${channelIdList.length} salon(s)...`);
// Nettoyer chaque salon
for (const channelId of channelIdList) {
await cleanChannel(client, channelId);
}
console.log('✅ Nettoyage terminé');
} catch (error) {
console.log("❌ Erreur générale lors du nettoyage: " + error.message);
}
};
module.exports = clean; module.exports = clean;