Skip to content

Commit

Permalink
improved playlist looping
Browse files Browse the repository at this point in the history
  • Loading branch information
solaluset committed May 20, 2022
1 parent e1ae804 commit 343a7d1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
HELP_PING_LONG = "Test bot response status"
HELP_CLEAR_SHORT = "Clear the queue."
HELP_CLEAR_LONG = "Clears the queue and skips the current song."
HELP_LOOP_SHORT = "Loops the currently playing song, toggle on/off."
HELP_LOOP_LONG = "Loops the currently playing song and locks the queue. Use the command again to disable loop."
HELP_LOOP_SHORT = "Loops the currently playing song or queue."
HELP_LOOP_LONG = "Loops the currently playing song or queue. Modes are all/single/off."
HELP_QUEUE_SHORT = "Shows the songs in queue."
HELP_QUEUE_LONG = "Shows the number of songs in queue, up to 10."
HELP_SHUFFLE_SHORT = "Shuffle the queue"
Expand Down
4 changes: 2 additions & 2 deletions musicbot/audiocontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def next_song(self, error):
async def play_song(self, song):
"""Plays a song object"""

if self.playlist.loop != True: #let timer run thouh if looping
if self.playlist.loop == "off": #let timer run thouh if looping
self.timer.cancel()
self.timer = utils.Timer(self.timeout_handler)

Expand Down Expand Up @@ -296,7 +296,7 @@ async def stop_player(self):
not self.guild.voice_client.is_paused() and not self.guild.voice_client.is_playing()):
return

self.playlist.loop = False
self.playlist.loop = "off"
self.playlist.next(self.current_song)
self.clear_queue()
self.guild.voice_client.stop()
Expand Down
32 changes: 21 additions & 11 deletions musicbot/commands/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ async def _play_song(self, ctx, *, track: str):
audiocontroller.timer.cancel()
audiocontroller.timer = utils.Timer(audiocontroller.timeout_handler)

if audiocontroller.playlist.loop == True:
await ctx.send("Loop is enabled! Use {}loop to disable".format(config.BOT_PREFIX))
return
# if audiocontroller.playlist.loop == True:
# await ctx.send("Loop is enabled! Use {}loop to disable".format(config.BOT_PREFIX))
# return

song = await audiocontroller.process_song(track)

Expand All @@ -58,7 +58,7 @@ async def _play_song(self, ctx, *, track: str):
await ctx.send(config.SONGINFO_PLAYLIST_QUEUED)

@commands.command(name='loop', description=config.HELP_LOOP_LONG, help=config.HELP_LOOP_SHORT, aliases=['l'])
async def _loop(self, ctx):
async def _loop(self, ctx, mode = None):

current_guild = utils.get_guild(self.bot, ctx.message)
audiocontroller = utils.guild_to_audiocontroller[current_guild]
Expand All @@ -70,11 +70,21 @@ async def _loop(self, ctx):
await ctx.send("No songs in queue!")
return

if audiocontroller.playlist.loop == False:
audiocontroller.playlist.loop = True
if mode is None:
if audiocontroller.playlist.loop == "off":
mode = "all"
else:
mode = "off"

if mode not in ("all", "single", "off"):
await ctx.send("Invalid loop mode!")
return

audiocontroller.playlist.loop = mode

if mode in ("all", "single"):
await ctx.send("Loop enabled :arrows_counterclockwise:")
else:
audiocontroller.playlist.loop = False
await ctx.send("Loop disabled :x:")

@commands.command(name='shuffle', description=config.HELP_SHUFFLE_LONG, help=config.HELP_SHUFFLE_SHORT,
Expand Down Expand Up @@ -156,7 +166,7 @@ async def _stop(self, ctx):
return

audiocontroller = utils.guild_to_audiocontroller[current_guild]
audiocontroller.playlist.loop = False
audiocontroller.playlist.loop = "off"
if current_guild is None:
await ctx.send(config.NO_GUILD_MESSAGE)
return
Expand Down Expand Up @@ -196,7 +206,7 @@ async def _skip(self, ctx):
return

audiocontroller = utils.guild_to_audiocontroller[current_guild]
audiocontroller.playlist.loop = False
# audiocontroller.playlist.loop = False

audiocontroller.timer.cancel()
audiocontroller.timer = utils.Timer(audiocontroller.timeout_handler)
Expand All @@ -221,7 +231,7 @@ async def _clear(self, ctx):
audiocontroller = utils.guild_to_audiocontroller[current_guild]
audiocontroller.clear_queue()
current_guild.voice_client.stop()
audiocontroller.playlist.loop = False
audiocontroller.playlist.loop = "off"
await ctx.send("Cleared queue :no_entry_sign:")

@commands.command(name='prev', description=config.HELP_PREV_LONG, help=config.HELP_PREV_SHORT, aliases=['back'])
Expand All @@ -232,7 +242,7 @@ async def _prev(self, ctx):
return

audiocontroller = utils.guild_to_audiocontroller[current_guild]
audiocontroller.playlist.loop = False
# audiocontroller.playlist.loop = False

audiocontroller.timer.cancel()
audiocontroller.timer = utils.Timer(audiocontroller.timeout_handler)
Expand Down
6 changes: 4 additions & 2 deletions musicbot/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self):
# A seperate history that remembers the names of the tracks that were played
self.trackname_history = deque()

self.loop = False
self.loop = "off"

def __len__(self):
return len(self.playque)
Expand All @@ -30,8 +30,10 @@ def add(self, track):

def next(self, song_played):

if self.loop == True:
if self.loop == "single":
self.playque.appendleft(self.playhistory[-1])
elif self.loop == "all":
self.playque.append(self.playhistory[-1])

if len(self.playque) == 0:
return None
Expand Down

0 comments on commit 343a7d1

Please sign in to comment.