55from utilities .setup_logger import log_exception
66
77wall_e_category_name = "WALL-E LOGS"
8+ wall_e_category_name_contd = "WALL-E LOGS Contd"
89
910
1011class BotChannelManager :
@@ -116,8 +117,17 @@ def __init__(self, config, bot):
116117 "member_update_listener_discordpy_warn" ,
117118 "member_update_listener_discordpy_error" ,
118119 ]
119- for index , channel_name in enumerate (log_names ):
120- BotChannelManager .log_positioning [channel_name ] = index
120+ index = 0
121+ category = wall_e_category_name
122+ for channel_name in log_names :
123+ BotChannelManager .log_positioning [channel_name ] = {
124+ "category" : category ,
125+ "index" : index
126+ }
127+ index += 1
128+ if index == 50 :
129+ index = 0
130+ category = wall_e_category_name_contd
121131
122132 async def create_or_get_channel_id_for_service_logs (self , logger , guild , config , service ):
123133 """
@@ -131,7 +141,9 @@ async def create_or_get_channel_id_for_service_logs(self, logger, guild, config,
131141 await self .bot .wait_until_ready ()
132142 service = service .lower ()
133143 environment = config .get_config_value ("basic_config" , "ENVIRONMENT" )
134- text_channel_position = BotChannelManager .log_positioning [service ]
144+ text_channel_position_info = BotChannelManager .log_positioning [service ]
145+ category_name = text_channel_position_info ['category' ]
146+ text_channel_position = text_channel_position_info ['index' ]
135147 logger .debug (
136148 f"[BotChannelManager create_or_get_channel_id_for_service_logs()] getting channel { service } for"
137149 f" { environment } "
@@ -141,20 +153,20 @@ async def create_or_get_channel_id_for_service_logs(self, logger, guild, config,
141153 f" for { environment } "
142154 )
143155 bot_chan : discord .channel .CategoryChannel = discord .utils .get (guild .channels , name = service )
144- if wall_e_category_name not in self .channel_obtained :
145- self .channel_obtained [wall_e_category_name ] = None
146- logs_category = discord .utils .get (guild .channels , name = wall_e_category_name )
156+ if category_name not in self .channel_obtained :
157+ self .channel_obtained [category_name ] = None
158+ logs_category = discord .utils .get (guild .channels , name = category_name )
147159 if logs_category is None :
148- logs_category = await guild .create_category (name = wall_e_category_name )
149- self .channel_obtained [wall_e_category_name ] = logs_category .id
160+ logs_category = await guild .create_category (name = category_name )
161+ self .channel_obtained [category_name ] = logs_category .id
150162 else :
151- while self .channel_obtained [wall_e_category_name ] is None :
163+ while self .channel_obtained [category_name ] is None :
152164 logger .debug (
153165 f"[BotChannelManager create_or_get_channel_id_for_service_logs()] waiting to get category "
154166 f"WALL-E Logs for in { environment } ."
155167 )
156168 await asyncio .sleep (8 )
157- logs_category = discord .utils .get (guild .channels , id = int (self .channel_obtained [wall_e_category_name ]))
169+ logs_category = discord .utils .get (guild .channels , id = int (self .channel_obtained [category_name ]))
158170 if bot_chan is None :
159171 logger .debug (
160172 f"[BotChannelManager create_or_get_channel_id_for_service_logs()] channel \" { service } \" for "
@@ -283,7 +295,7 @@ def text_log_channel(channel): return (
283295
284296 def log_category (channel ): return (
285297 type (channel ) == discord .channel .CategoryChannel and
286- channel .name == wall_e_category_name
298+ channel .name in [ wall_e_category_name , wall_e_category_name_contd ]
287299 )
288300
289301 log_channels = [
@@ -297,21 +309,68 @@ def log_category(channel): return (
297309 async def fix_text_channel_positioning (cls , logger , guild ):
298310 duplicate_channels = {} # used to find and report text log channels that share the same name
299311
300- logs_category = discord .utils .get (guild .channels , name = wall_e_category_name )
301- channels_under_category = [
302- channel for channel in guild .channels
303- if type (channel ) == discord .channel .TextChannel and channel .category == logs_category
304- ]
305-
306- for channel_under_category in channels_under_category :
307- if channel_under_category .name not in duplicate_channels :
308- duplicate_channels [channel_under_category .name ] = 1
309- else :
310- duplicate_channels [channel_under_category .name ] += 1
312+ category_names = [wall_e_category_name , wall_e_category_name_contd ]
313+ for category_name in category_names :
314+ logger .debug (
315+ "[BotChannelManager fix_text_channel_positioning()] going through any logs under channel "
316+ f"{ category_name } "
317+ )
318+ logs_category = discord .utils .get (guild .channels , name = category_name )
319+ logger .debug (f"[BotChannelManager fix_text_channel_positioning()] got category { logs_category } " )
320+ channels_under_category = [
321+ channel for channel in guild .channels
322+ if type (channel ) == discord .channel .TextChannel and channel .category == logs_category
323+ ]
324+ for channel_under_category in channels_under_category :
325+ if channel_under_category .name not in duplicate_channels :
326+ duplicate_channels [channel_under_category .name ] = 1
327+ else :
328+ duplicate_channels [channel_under_category .name ] += 1
311329
312- # ensure that there are no channels that should not exist
313- if channel_under_category .name not in BotChannelManager .log_positioning :
314- await channel_under_category .delete ()
330+ # ensure that there are no channels that should not exist
331+ if channel_under_category .name not in BotChannelManager .log_positioning :
332+ await channel_under_category .delete ()
333+ number_of_clean_passes = 0
334+ while number_of_clean_passes < 3 :
335+ # encapsulating the whole thing in a while loop cause sometimes a channel might erroneously get pushed
336+ # to the bottom while the repositioning is happening, which necessitates another sweep-through
337+ # as such, I am going to make the code keep going over the channels until all the positions are
338+ # verified as what they should be
339+ position_edited = False
340+ for text_channel_name , text_channel_position_info in BotChannelManager .log_positioning .items ():
341+ if text_channel_position_info ['category' ] != category_name :
342+ continue
343+ index = text_channel_position_info ['index' ]
344+ text_channel = discord .utils .get (guild .channels , name = text_channel_name )
345+ while text_channel is None :
346+ logger .warn (
347+ f"[BotChannelManager fix_text_channel_positioning()] unable to get channel "
348+ f"[{ text_channel_name } ], retrying in 5 seconds"
349+ )
350+ await asyncio .sleep (5 )
351+ text_channel = discord .utils .get (guild .channels , name = text_channel_name )
352+ if text_channel .category != logs_category :
353+ logger .debug (
354+ f"[BotChannelManager fix_text_channel_positioning()] fixing the category for "
355+ f"{ text_channel_name } { logs_category } "
356+ )
357+ position_edited = True
358+ await text_channel .edit (category = logs_category )
359+ if text_channel .position != index :
360+ logger .debug (
361+ f"[BotChannelManager fix_text_channel_positioning()] changing the position for "
362+ f"{ text_channel_name } from { text_channel .position } to { index } "
363+ )
364+ position_edited = True
365+ await text_channel .edit (position = index )
366+ if position_edited :
367+ number_of_clean_passes = 0
368+ logger .warn (
369+ "[BotChannelManager fix_text_channel_positioning()] doing another sweep of the log text "
370+ "channels positioning"
371+ )
372+ else :
373+ number_of_clean_passes += 1
315374 duplicate_channels = [
316375 channel_name
317376 for channel_name , number_of_occurrences in duplicate_channels .items ()
@@ -321,48 +380,10 @@ async def fix_text_channel_positioning(cls, logger, guild):
321380 duplicate_channels = ", " .join (duplicate_channels )
322381 log_exception (
323382 logger ,
324- f"[bot_channel_manager.py fix_text_channel_positioning()] following duplicate"
383+ f"[BotChannelManager fix_text_channel_positioning()] following duplicate"
325384 f" text log channels detected: { duplicate_channels } "
326385 )
327- number_of_clean_passes = 0
328- while number_of_clean_passes < 3 :
329- # encapsulating the whole thing in a while loop cause sometimes a channel might erroneously get pushed to
330- # the bottom while the repositioning is happening, which necessitates another sweep-through
331- # as such, I am going to make the code keep going over the channels until all the positions are verified
332- # as what they should be
333- position_edited = False
334- for text_channel_name , index in BotChannelManager .log_positioning .items ():
335- text_channel = discord .utils .get (guild .channels , name = text_channel_name )
336- while text_channel is None :
337- logger .warn (
338- f"[bot_channel_manager.py fix_text_channel_positioning()] unable to get channel "
339- f"[{ text_channel_name } ], retrying in 5 seconds"
340- )
341- await asyncio .sleep (5 )
342- text_channel = discord .utils .get (guild .channels , name = text_channel_name )
343- if text_channel .category != logs_category :
344- logger .debug (
345- f"[bot_channel_manager.py fix_text_channel_positioning()] fixing the category for "
346- f"{ text_channel_name } { logs_category } "
347- )
348- position_edited = True
349- await text_channel .edit (category = logs_category )
350- if text_channel .position != index :
351- logger .debug (
352- f"[bot_channel_manager.py fix_text_channel_positioning()] changing the position for "
353- f"{ text_channel_name } from { text_channel .position } to { index } "
354- )
355- position_edited = True
356- await text_channel .edit (position = index )
357- if position_edited :
358- number_of_clean_passes = 0
359- logger .warn (
360- "[bot_channel_manager.py fix_text_channel_positioning()] doing another sweep of the log text "
361- "channels positioning"
362- )
363- else :
364- number_of_clean_passes += 1
365386 logger .debug (
366- "[bot_channel_manager.py fix_text_channel_positioning()] done with sweep of the log text channels "
387+ "[BotChannelManager fix_text_channel_positioning()] done with sweep of the log text channels "
367388 "positioning"
368389 )
0 commit comments