Skip to content

Commit

Permalink
Merge pull request #2321 from Exirel/join-channels-on-modt
Browse files Browse the repository at this point in the history
coretasks: join channels on MOTD
  • Loading branch information
dgw authored Jul 25, 2022
2 parents 0f7bcdd + 031a778 commit 20f8c51
Showing 1 changed file with 40 additions and 33 deletions.
73 changes: 40 additions & 33 deletions sopel/coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 20f8c51

Please sign in to comment.