fix: 'jump' now remove files behind in one-shot mode. #79

This commit is contained in:
Terry Geng 2020-02-27 09:44:08 +08:00
parent 5491f00c34
commit 020ddbca22
5 changed files with 36 additions and 11 deletions

View File

@ -113,10 +113,13 @@ def cmd_url_unban(bot, user, text, command, parameter):
def cmd_play(bot, user, text, command, parameter): def cmd_play(bot, user, text, command, parameter):
if var.playlist.length() > 0: if var.playlist.length() > 0:
if parameter is not None and parameter.isdigit() and int(parameter) > 0 \ if parameter is not None:
and int(parameter) <= len(var.playlist): if parameter.isdigit() and int(parameter) > 0 and int(parameter) <= len(var.playlist):
bot.stop() bot.kill_ffmpeg()
bot.launch_music(int(parameter) - 1) bot.launch_music(int(parameter) - 1)
else:
bot.send_msg(constants.strings('invalid_index', index=parameter), text)
elif bot.is_pause: elif bot.is_pause:
bot.resume() bot.resume()
else: else:

View File

@ -170,6 +170,7 @@ which_command = Do you mean <br /> {commands}
multiple_matches = Track not found! Possible candidates: multiple_matches = Track not found! Possible candidates:
queue_contents = Items on the playlist: queue_contents = Items on the playlist:
queue_empty = Playlist is empty! queue_empty = Playlist is empty!
invalid_index = Invalid index <i>{index}</i>. Use '!queue' to see your playlist.
now_playing = Now playing {item}<br />{thumb} now_playing = Now playing {item}<br />{thumb}
not_in_my_channel = You're not in my channel, command refused! not_in_my_channel = You're not in my channel, command refused!
pm_not_allowed = Private message aren't allowed. pm_not_allowed = Private message aren't allowed.

View File

@ -257,7 +257,7 @@ def post():
logging.info("web: jump to: " + util.format_debug_song_string(music)) logging.info("web: jump to: " + util.format_debug_song_string(music))
if len(var.playlist) >= int(request.form['play_music']): if len(var.playlist) >= int(request.form['play_music']):
var.botamusique.stop() var.botamusique.kill_ffmpeg()
var.botamusique.launch_music(int(request.form['play_music'])) var.botamusique.launch_music(int(request.form['play_music']))
elif 'delete_music_file' in request.form and ".." not in request.form['delete_music_file']: elif 'delete_music_file' in request.form and ".." not in request.form['delete_music_file']:

View File

@ -18,6 +18,10 @@ class PlayList(list):
self.mode = mode self.mode = mode
if mode == "random": if mode == "random":
self.randomize() self.randomize()
if mode == "one-shot" and self.current_index > 0:
for i in range(self.current_index):
super().__delitem__(0)
self.current_index = 0
def append(self, item): def append(self, item):
self.version += 1 self.version += 1
@ -122,9 +126,15 @@ class PlayList(list):
return self[self.next_index()] return self[self.next_index()]
def jump(self, index): def jump(self, index):
if self.mode == "one-shot":
for i in range(index):
super().__delitem__(0)
self.current_index = 0
else:
self.current_index = index
self.version += 1 self.version += 1
self.current_index = index return self[self.current_index]
return self[index]
def randomize(self): def randomize(self):
# current_index will lose track after shuffling, thus we take current music out before shuffling # current_index will lose track after shuffling, thus we take current music out before shuffling
@ -158,6 +168,15 @@ class PlayList(list):
self.extend(list(map(lambda v: json.loads(v[1]), items))) self.extend(list(map(lambda v: json.loads(v[1]), items)))
self.current_index = current_index self.current_index = current_index
def _debug_print(self):
print("===== Playlist(%d) ====" % self.current_index)
for index, item in enumerate(self):
if index == self.current_index:
print("-> %d %s" % (index, item['title']))
else:
print("%d %s" % (index, item['title']))
print("===== End ====")
def get_playlist_info(url, start_index=0, user=""): def get_playlist_info(url, start_index=0, user=""):
items = [] items = []

View File

@ -752,6 +752,12 @@ if __name__ == '__main__':
var.botamusique = MumbleBot(args) var.botamusique = MumbleBot(args)
command.register_all_commands(var.botamusique) command.register_all_commands(var.botamusique)
# load playlist
if var.config.getboolean('bot', 'save_playlist', fallback=True):
logging.info("bot: load playlist from previous session")
var.playlist.load()
# load playback mode
playback_mode = None playback_mode = None
if var.db.has_option("playlist", "playback_mode"): if var.db.has_option("playlist", "playback_mode"):
playback_mode = var.db.get('playlist', 'playback_mode') playback_mode = var.db.get('playlist', 'playback_mode')
@ -761,9 +767,5 @@ if __name__ == '__main__':
if playback_mode in ["one-shot", "repeat", "random"]: if playback_mode in ["one-shot", "repeat", "random"]:
var.playlist.set_mode(playback_mode) var.playlist.set_mode(playback_mode)
if var.config.getboolean('bot', 'save_playlist', fallback=True):
logging.info("bot: load playlist from previous session")
var.playlist.load()
# Start the main loop. # Start the main loop.
var.botamusique.loop() var.botamusique.loop()