From 031a778e8e2af07aa4f40cc793173dd1b83f2e88 Mon Sep 17 00:00:00 2001 From: Florian Strzelecki Date: Thu, 14 Jul 2022 14:17:43 +0200 Subject: [PATCH] coretasks: join channels on MOTD --- sopel/coretasks.py | 73 +++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/sopel/coretasks.py b/sopel/coretasks.py index 76c72e4369..42e06b439a 100644 --- a/sopel/coretasks.py +++ b/sopel/coretasks.py @@ -64,6 +64,7 @@ def setup(bot): The setup phase is used to activate the throttle feature to prevent a flood of JOIN commands when there are too many channels to join. """ + bot.memory['retry_join'] = SopelMemory() bot.memory['join_events_queue'] = collections.deque() # Manage JOIN flood protection @@ -82,6 +83,7 @@ def setup(bot): def shutdown(bot): """Clean up coretasks-related values in the bot's memory.""" + bot.memory['retry_join'] = SopelMemory() try: bot.memory['join_events_queue'].clear() except KeyError: @@ -286,39 +288,6 @@ def startup(bot, trigger): modes = '+' + modes bot.write(('MODE', bot.nick, modes)) - # join channels - bot.memory['retry_join'] = SopelMemory() - - channels = bot.config.core.channels - if not channels: - LOGGER.info("No initial channels to JOIN.") - elif bot.config.core.throttle_join: - throttle_rate = int(bot.config.core.throttle_join) - throttle_wait = max(bot.config.core.throttle_wait, 1) - channels_joined = 0 - - LOGGER.info( - "Joining %d channels (with JOIN throttle ON); " - "this may take a moment.", - len(channels)) - - for channel in channels: - channels_joined += 1 - if not channels_joined % throttle_rate: - LOGGER.debug( - "Waiting %ds before next JOIN batch.", - throttle_wait) - time.sleep(throttle_wait) - bot.join(channel) - else: - LOGGER.info( - "Joining %d channels (with JOIN throttle OFF); " - "this may take a moment.", - len(channels)) - - for channel in bot.config.core.channels: - bot.join(channel) - # warn for insecure auth method if necessary if (not bot.config.core.owner_account and 'account-tag' in bot.enabled_capabilities and @@ -402,6 +371,44 @@ def handle_isupport(bot, trigger): bot.write(('PROTOCTL', 'UHNAMES')) +@plugin.event(events.RPL_ENDOFMOTD, events.ERR_NOMOTD) +@plugin.thread(False) +@plugin.unblockable +@plugin.priority('medium') +def join_channels(bot, trigger): + # join channels + channels = bot.config.core.channels + if not channels: + LOGGER.info("No initial channels to JOIN.") + + elif bot.config.core.throttle_join: + throttle_rate = int(bot.config.core.throttle_join) + throttle_wait = max(bot.config.core.throttle_wait, 1) + channels_joined = 0 + + LOGGER.info( + "Joining %d channels (with JOIN throttle ON); " + "this may take a moment.", + len(channels)) + + for channel in channels: + channels_joined += 1 + if not channels_joined % throttle_rate: + LOGGER.debug( + "Waiting %ds before next JOIN batch.", + throttle_wait) + time.sleep(throttle_wait) + bot.join(channel) + else: + LOGGER.info( + "Joining %d channels (with JOIN throttle OFF); " + "this may take a moment.", + len(channels)) + + for channel in bot.config.core.channels: + bot.join(channel) + + @plugin.event(events.RPL_MYINFO) @plugin.thread(False) @plugin.unblockable