From 85fd8397c2ceae9ff66e7aa2d2de0cef2c637496 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 19 Oct 2021 21:09:21 -0700 Subject: [PATCH] Fix auto-rejoin channels when reconnecting --- lib/PBot/Core/Channels.pm | 9 +++++++++ lib/PBot/Core/Handlers/Server.pm | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/lib/PBot/Core/Channels.pm b/lib/PBot/Core/Channels.pm index 2891a06d..da5dc849 100644 --- a/lib/PBot/Core/Channels.pm +++ b/lib/PBot/Core/Channels.pm @@ -20,6 +20,14 @@ sub initialize { ); $self->{storage}->load; + + # reset joined_channels flag when disconnected + $self->{pbot}->{event_dispatcher}->register_handler( + 'pbot.disconnect', + sub { + $self->{pbot}->{joined_channels} = 0; + } + ); } sub join { @@ -59,6 +67,7 @@ sub autojoin { $self->{pbot}->{logger}->log("Joining channels: $channels\n"); $self->join($channels); + $self->{pbot}->{joined_channels} = 1; } diff --git a/lib/PBot/Core/Handlers/Server.pm b/lib/PBot/Core/Handlers/Server.pm index acacba5f..7353fb87 100644 --- a/lib/PBot/Core/Handlers/Server.pm +++ b/lib/PBot/Core/Handlers/Server.pm @@ -60,6 +60,11 @@ sub on_disconnect { $self->{pbot}->{logger}->log("Disconnected...\n"); $self->{pbot}->{connected} = 0; + # send pbot.disconnect to notify PBot internals + $self->{pbot}->{event_dispatcher}->dispatch_event( + 'pbot.disconnect', undef + ); + # attempt to reconnect to server # TODO: maybe add a registry entry to control whether the bot auto-reconnects $self->{pbot}->connect;