From 8f48b7deda90371505bdc4bb10883a09942da1f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20R=C3=B6mer?= Date: Mon, 23 Mar 2020 16:08:26 +0100 Subject: [PATCH 1/3] add feature autostop --- configuration.default.ini | 3 +++ configuration.example.ini | 3 +++ mumbleBot.py | 15 +++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/configuration.default.ini b/configuration.default.ini index 79e0a21..53f3dc7 100644 --- a/configuration.default.ini +++ b/configuration.default.ini @@ -85,6 +85,9 @@ ducking = False ducking_volume = 0.05 ducking_threshold = 3000 +# if auto_stop is enabled and a user leaves and the bot is left alone, stop and clear the playlist +auto_stop = False + [webinterface] # Set enabled to True if you'd like to use the web interface to manage your playlist, upload files, etc. enabled = False diff --git a/configuration.example.ini b/configuration.example.ini index 27b3343..3ad7490 100644 --- a/configuration.example.ini +++ b/configuration.example.ini @@ -98,6 +98,9 @@ port = 64738 #ducking_volume = 0.05 #ducking_threshold = 3000 +# 'auto_stop': If a user leaves and the bot is left alone, stop and clear the playlist +#auto_stop = False + # [webinterface] stores settings related to the web interface. [webinterface] # 'enable': Set 'enabled' to True if you'd like to use the web interface to manage diff --git a/mumbleBot.py b/mumbleBot.py index 2bec6fc..39cb012 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -149,6 +149,10 @@ class MumbleBot: self.ducking_sound_received) self.mumble.set_receive_sound(True) + if not var.db.has_option("bot", "auto_stop") and var.config.getboolean("bot", "auto_stop", fallback=False)\ + or var.config.getboolean("bot", "auto_stop"): + self.mumble.callbacks.set_callback(pymumble.constants.PYMUMBLE_CLBK_USERREMOVED, self.users_changed) + self.mumble.callbacks.set_callback(pymumble.constants.PYMUMBLE_CLBK_USERUPDATED, self.users_changed) # Debug use self._loop_status = 'Idle' self._display_rms = False @@ -314,6 +318,17 @@ class MumbleBot: else: return False + # ======================= + # Users changed + # ======================= + + def users_changed(self, user, message): + own_channel = self.mumble.channels[self.mumble.users.myself['channel_id']] + # if the bot is the only user left in the channel + if len(own_channel.get_users()) == 1: + self.log.info('Other users in the channel left. Stopping music now.') + self.clear() + # ======================= # Launch and Download # ======================= From b6ea3c5bfff1f04a6df3696a5dde834be1c12d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20R=C3=B6mer?= Date: Mon, 23 Mar 2020 18:05:38 +0100 Subject: [PATCH 2/3] autostop now supports pause/stop --- configuration.example.ini | 6 ++++-- mumbleBot.py | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/configuration.example.ini b/configuration.example.ini index 3ad7490..66302fd 100644 --- a/configuration.example.ini +++ b/configuration.example.ini @@ -98,8 +98,10 @@ port = 64738 #ducking_volume = 0.05 #ducking_threshold = 3000 -# 'auto_stop': If a user leaves and the bot is left alone, stop and clear the playlist -#auto_stop = False +# 'when_nobody_in_channel': should the music stop playing when everybody left the channel +# it should be one of "pause" (pause current song), "stop" (also clears playlist) or "nothing" (keep playing music) +# If a user leaves and the bot is left alone, stop and clear the playlist +#when_nobody_in_channel = nothing # [webinterface] stores settings related to the web interface. [webinterface] diff --git a/mumbleBot.py b/mumbleBot.py index 39cb012..00930bc 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -149,10 +149,13 @@ class MumbleBot: self.ducking_sound_received) self.mumble.set_receive_sound(True) - if not var.db.has_option("bot", "auto_stop") and var.config.getboolean("bot", "auto_stop", fallback=False)\ - or var.config.getboolean("bot", "auto_stop"): + if var.config.get("bot", "when_nobody_in_channel") in ['pause', 'stop', 'nothing']: + self.log.warn('Config "when_nobody_in_channel" is not on of "pause", "stop" or "nothing", falling back to "nothing".') + + if var.config.get("bot", "when_nobody_in_channel", fallback='nothing') in ['pause', 'stop']: self.mumble.callbacks.set_callback(pymumble.constants.PYMUMBLE_CLBK_USERREMOVED, self.users_changed) self.mumble.callbacks.set_callback(pymumble.constants.PYMUMBLE_CLBK_USERUPDATED, self.users_changed) + # Debug use self._loop_status = 'Idle' self._display_rms = False @@ -324,9 +327,15 @@ class MumbleBot: def users_changed(self, user, message): own_channel = self.mumble.channels[self.mumble.users.myself['channel_id']] + if len(own_channel.get_users()) > 1: + return + # if the bot is the only user left in the channel - if len(own_channel.get_users()) == 1: - self.log.info('Other users in the channel left. Stopping music now.') + self.log.info('bot: Other users in the channel left. Stopping music now.') + + if var.config.get("bot", "when_nobody_in_channel") == "pause": + self.pause() + else: self.clear() # ======================= From 7b1415948ad3cb9f6719811443dd7bf680122dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20R=C3=B6mer?= Date: Mon, 23 Mar 2020 19:00:34 +0100 Subject: [PATCH 3/3] add play_resume option --- configuration.example.ini | 3 ++- mumbleBot.py | 32 +++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/configuration.example.ini b/configuration.example.ini index 66302fd..cce22f3 100644 --- a/configuration.example.ini +++ b/configuration.example.ini @@ -99,7 +99,8 @@ port = 64738 #ducking_threshold = 3000 # 'when_nobody_in_channel': should the music stop playing when everybody left the channel -# it should be one of "pause" (pause current song), "stop" (also clears playlist) or "nothing" (keep playing music) +# it should be one of "pause" (pause current song), "pause_resume" (pause current song and resume once somebody reenters the channel) +# "stop" (also clears playlist) or "nothing" (keep playing music) # If a user leaves and the bot is left alone, stop and clear the playlist #when_nobody_in_channel = nothing diff --git a/mumbleBot.py b/mumbleBot.py index 00930bc..27268fd 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -149,10 +149,10 @@ class MumbleBot: self.ducking_sound_received) self.mumble.set_receive_sound(True) - if var.config.get("bot", "when_nobody_in_channel") in ['pause', 'stop', 'nothing']: - self.log.warn('Config "when_nobody_in_channel" is not on of "pause", "stop" or "nothing", falling back to "nothing".') + if var.config.get("bot", "when_nobody_in_channel") not in ['pause', 'pause_resume', 'stop', 'nothing']: + self.log.warn('Config "when_nobody_in_channel" is not on of "pause", "pause_resume", "stop" or "nothing", falling back to "nothing".') - if var.config.get("bot", "when_nobody_in_channel", fallback='nothing') in ['pause', 'stop']: + if var.config.get("bot", "when_nobody_in_channel", fallback='nothing') in ['pause', 'pause_resume', 'stop']: self.mumble.callbacks.set_callback(pymumble.constants.PYMUMBLE_CLBK_USERREMOVED, self.users_changed) self.mumble.callbacks.set_callback(pymumble.constants.PYMUMBLE_CLBK_USERUPDATED, self.users_changed) @@ -327,16 +327,22 @@ class MumbleBot: def users_changed(self, user, message): own_channel = self.mumble.channels[self.mumble.users.myself['channel_id']] - if len(own_channel.get_users()) > 1: - return - - # if the bot is the only user left in the channel - self.log.info('bot: Other users in the channel left. Stopping music now.') - - if var.config.get("bot", "when_nobody_in_channel") == "pause": - self.pause() - else: - self.clear() + # only check if there is one more user currently in the channel + # else when the music is paused and somebody joins, music would start playing again + if len(own_channel.get_users()) == 2: + if var.config.get("bot", "when_nobody_in_channel") == "pause_resume": + self.resume() + elif var.config.get("bot", "when_nobody_in_channel") == "pause": + self.send_msg('Music was paused after everyone left. !play to resume'); + + elif len(own_channel.get_users()) == 1: + # if the bot is the only user left in the channel + self.log.info('bot: Other users in the channel left. Stopping music now.') + + if var.config.get("bot", "when_nobody_in_channel") == "stop": + self.clear() + else: + self.pause() # ======================= # Launch and Download