diff --git a/src/events/channelCreate.js b/src/events/channelCreate.js index 40db8d4..eb2b129 100644 --- a/src/events/channelCreate.js +++ b/src/events/channelCreate.js @@ -7,14 +7,18 @@ async function invoke(channel) { const allChannels = guild.channels.cache; // Check if the channel limit (500) is reached, with a margin of 2 - if (allChannels.size >= 498) { + if (allChannels.size >= 495) { const oldestArchiveCategory = channel.guild.channels.cache .filter( (channel) => channel.type === 4 && channel.name.slice(0, 11).toLowerCase() === "📁archives_" ) - .last(); + .sort( + (a, b) => + parseInt(a.name.split("_")[1]) - parseInt(b.name.split("_")[1]) + ) + .first(); if (oldestArchiveCategory) { const oldestChannel = oldestArchiveCategory.children.cache diff --git a/src/utils/archive.js b/src/utils/archive.js index c04e8ef..0ecc3ac 100644 --- a/src/utils/archive.js +++ b/src/utils/archive.js @@ -14,8 +14,18 @@ async function archive(channel) { if (latestCategory) { await channel.setParent(latestCategory); + + // Reorder channels by name + const sortedChannels = latestCategory.children.cache.sort((a, b) => + b.name.localeCompare(a.name) + ); + let position = 0; + for (const channel of sortedChannels.values()) { + await channel.setPosition(position); + position++; + } } else { - // creating a new archive folder + // Create a new archive category const count = Math.max( ...archiveCategories.map((cat) => parseInt(cat.name.split("_")[1])) ); @@ -23,6 +33,11 @@ async function archive(channel) { name: `📁ARCHIVES_${count + 1}`, type: 4, }); + + // Position this new category at the top + await newCategory.setPosition( + Math.min(...archiveCategories.map((x) => x.position)) + ); console.log(`Created new archive category ${newCategory.name}`); await channel.setParent(newCategory); }