Skip to content

Commit

Permalink
fix ChannelCreate deleting channel in wrong category
Browse files Browse the repository at this point in the history
archive.js now reorder channels and categories by name so most recent archives are at the top
  • Loading branch information
BodomBeach committed Sep 24, 2024
1 parent f80440d commit 83bf788
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/events/channelCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/utils/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,30 @@ 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]))
);
let newCategory = await channel.guild.channels.create({
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);
}
Expand Down

0 comments on commit 83bf788

Please sign in to comment.