@@ -361,40 +361,67 @@ public Category createCategory() {
361361 * @throws CategoriesLimitException if categories reached the limit.
362362 * @throws ChannelsLimitException if discord reached the channels limit.
363363 */
364- public void createChannel (@ NotNull String clanTag )
365- throws InvalidChannelException , CategoriesLimitException , ChannelsLimitException , ChannelExistsException {
366- validateChannel (clanTag );
367- Map <ClanPlayer , Member > discordClanPlayers = getDiscordPlayers (clanManager .getClan (clanTag ));
368-
369- if (getChannels ().size () >= settingsManager .getInt (DISCORDCHAT_TEXT_LIMIT )) {
370- throw new ChannelsLimitException ("Discord reached the channels limit" , "discord.reached.channels.limit" );
371- }
372-
373- Category availableCategory = getCachedCategories ().stream ().
374- filter (category -> category .getTextChannels ().size () < MAX_CHANNELS_PER_CATEGORY ).
375- findAny ().orElseGet (this ::createCategory );
376-
377- if (availableCategory == null ) {
378- throw new CategoriesLimitException ("Discord reached the categories limit" , "discord.reached.category.limit" );
379- }
380-
381- try {
382- availableCategory .createTextChannel (clanTag ).complete ();
383- SimpleClans .debug (String .format ("[%s] Creating a discord text channel for %s clan" , Thread .currentThread ().getId (), clanTag ));
384- } catch (ErrorResponseException ex ) {
385- Response response = ex .getResponse ();
386- plugin .getLogger ().warning (String .format ("Could not create a channel for clan %s, error %d - %s" ,
387- clanTag , response .code , response .message ));
388- return ;
389- }
390-
391- for (Map .Entry <ClanPlayer , Member > entry : discordClanPlayers .entrySet ()) {
392- // The map is formed from clan#getMembers (so the clan exists)
393- //noinspection ConstantConditions
394- updateViewPermission (entry .getValue (), entry .getKey ().getClan (), ADD );
395- updateLeaderRole (entry .getValue (), entry .getKey (), ADD );
396- }
397- }
364+ private final Set <String > channelIsBeingCreated = java .util .concurrent .ConcurrentHashMap .newKeySet ();
365+ public void createChannel (@ NotNull String clanTag )
366+ throws InvalidChannelException , CategoriesLimitException , ChannelsLimitException , ChannelExistsException {
367+
368+ // validate it, perchance it exists dont do anything just chill
369+ if (channelExists (clanTag )) {
370+ return ;
371+ }
372+
373+ // prevent duplicate creation (this might genuinely be useless but i felt like making it)
374+ if (!channelIsBeingCreated .add (clanTag )) {
375+ return ;
376+ }
377+
378+ try {
379+
380+ // double validation!!!
381+ if (channelExists (clanTag )) {
382+ return ;
383+ }
384+
385+ validateChannel (clanTag );
386+
387+ Map <ClanPlayer , Member > discordClanPlayers = getDiscordPlayers (clanManager .getClan (clanTag ));
388+
389+ if (getChannels ().size () >= settingsManager .getInt (DISCORDCHAT_TEXT_LIMIT )) {
390+ throw new ChannelsLimitException ("Discord reached the channels limit" , "discord.reached.channels.limit" );
391+ }
392+
393+ Category availableCategory = getCachedCategories ().stream ()
394+ .filter (category -> category .getTextChannels ().size () < MAX_CHANNELS_PER_CATEGORY )
395+ .findAny ()
396+ .orElseGet (this ::createCategory );
397+
398+ if (availableCategory == null ) {
399+ throw new CategoriesLimitException ("Discord reached the categories limit" , "discord.reached.category.limit" );
400+ }
401+
402+ TextChannel createdChannel ;
403+ try {
404+ createdChannel = availableCategory .createTextChannel (clanTag ).complete ();
405+ SimpleClans .debug (String .format ("[%s] Creating a discord text channel for %s clan" ,
406+ Thread .currentThread ().getId (), clanTag ));
407+ } catch (ErrorResponseException ex ) {
408+ Response response = ex .getResponse ();
409+ plugin .getLogger ().warning (String .format (
410+ "Could not create a channel for clan %s, error %d - %s" ,
411+ clanTag , response .code , response .message ));
412+ return ;
413+ }
414+
415+ for (Map .Entry <ClanPlayer , Member > entry : discordClanPlayers .entrySet ()) {
416+ updateViewPermission (entry .getValue (), entry .getKey ().getClan (), ADD );
417+ updateLeaderRole (entry .getValue (), entry .getKey (), ADD );
418+ }
419+
420+ } finally {
421+ // unlock channel creation for this clantag, perchance.
422+ channelsBeingCreated .remove (clanTag );
423+ }
424+ }
398425
399426 /**
400427 * Retrieves channel in SimpleClans categories.
0 commit comments