If you're aiming to do it the right way, consider fetching all the channels within a guild first. Eliminate any channels that are not text-based. Then, further filter out the channels that do not have the name support-(number)
. Among the remaining set, identify the channel with the highest number and proceed to create a new channel with the increment of that number.
For demonstration purposes, here is a sample code snippet. Feel free to test it for functionality.
bot.on("message", async message => {
if (message.author.bot) return;
if (message.content.startsWith('!new')) {
// Fetching all guild channels.
let allChannels = message.guild.channels;
// Filtering non-text channels.
let textChannels = allChannels.filter((channel) => {
return channel.type === "text";
});
// Filtering text channels without 'support-(number)' in their names.
let supportChannels = textChannels.filter((textChannel) => {
// Verifying if the channel name follows the format 'support-(number)'. Refer to Regex for more details.
return textChannel.name.match(/^(support-)\d+$/g);
});
// Checking existence of support channels.
if (supportChannels.length) {
// Extracting numbers from channel names.
let numbers = supportChannels.map((supportChannel) => {
return parseInt(supportChannel.name.split('-')[1]);
});
// Finding the maximum number.
let highestNumber = Math.max(...numbers);
// Creating a new support channel with an incremented number.
message.guild.createChannel(`support-${highestNumber+1}`, 'text');
} else {
// In case there are no support channels, creating the initial one.
message.guild.createChannel('support-1', 'text');
}
}
});