Skip to content

Commit

Permalink
store paused status to restored paused players correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Draper <[email protected]>
  • Loading branch information
Drapersniper committed Apr 1, 2022
1 parent f3d670c commit 3c089c3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
4 changes: 3 additions & 1 deletion redbot/cogs/audio/apis/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,9 @@ async def autoplay(self, player: lavalink.Player, playlist_api: PlaylistWrapper)
if notify_channel_id:
await self.config.guild_from_id(
guild_id=player.guild.id
).currently_auto_playing_in.set([notify_channel_id, player.channel.id])
).currently_auto_playing_in.set(
[notify_channel_id, player.channel.id, player.paused]
)
else:
await self.config.guild_from_id(
guild_id=player.guild.id
Expand Down
2 changes: 1 addition & 1 deletion redbot/cogs/audio/core/events/lavalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async def lavalink_event_handler(
notify_channel = player.fetch("notify_channel")
if notify_channel and autoplay:
await self.config.guild_from_id(guild_id=guild_id).currently_auto_playing_in.set(
[notify_channel, player.channel.id]
[notify_channel, player.channel.id, player.paused]
)
else:
await self.config.guild_from_id(guild_id=guild_id).currently_auto_playing_in.set(
Expand Down
24 changes: 14 additions & 10 deletions redbot/cogs/audio/core/tasks/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ async def restore_players(self) -> None:
)
log.warning("Audio will attempt queue restore on next restart.")
return
metadata: Dict[int, Tuple[Optional[int], Optional[int]]] = {}
metadata: Dict[int, Tuple[Optional[int], Optional[int]], Optional[bool]] = {}
current_track_meta: Dict[int, lavalink.Track] = {}
all_guilds = await self.config.all_guilds()
async for guild_id, guild_data in AsyncIter(all_guilds.items(), steps=100):
if guild_data["auto_play"]:
if guild_data["currently_auto_playing_in"]:
notify_channel, vc_id = guild_data["currently_auto_playing_in"]
metadata[guild_id] = (notify_channel, vc_id)
notify_channel, vc_id, paused = guild_data["currently_auto_playing_in"]
metadata[guild_id] = (notify_channel, vc_id, paused)
if guild_data["last_known_vc_and_notify_channels"]:
notify_channel, vc_id = guild_data["last_known_vc_and_notify_channels"]
notify_channel, vc_id, paused = guild_data["last_known_vc_and_notify_channels"]
if vc_id:
metadata[guild_id] = (notify_channel, vc_id)
metadata[guild_id] = (notify_channel, vc_id, paused)
if guild_data["last_known_track"]:
guild = self.bot.get_guild(guild_id)
if not guild:
Expand Down Expand Up @@ -145,11 +145,11 @@ async def restore_players(self) -> None:
volume = all_guilds[guild.id]["volume"]
shuffle_bumped = all_guilds[guild.id]["shuffle_bumped"]
auto_deafen = all_guilds[guild.id]["auto_deafen"]

paused = False
if player is None:
while tries < 5 and vc is not None:
try:
notify_channel_id, vc_id = metadata.pop(
notify_channel_id, vc_id, paused = metadata.pop(
guild_id, (None, track_data[-1].room_id)
)
vc = guild.get_channel(vc_id)
Expand Down Expand Up @@ -199,26 +199,28 @@ async def restore_players(self) -> None:
player.repeat = repeat
player.shuffle = shuffle
player.shuffle_bumped = shuffle_bumped
player._paused = paused
if player.volume != volume:
await player.set_volume(volume)
if player.guild.id in current_track_meta:
player.current = current_track_meta[player.guild.id]
await player.resume(
current_track_meta[player.guild.id],
start=current_track_meta[player.guild.id].start_timestamp,
pause=paused,
)
for track in track_data:
track = track.track_object
player.add(guild.get_member(track.extras.get("requester")) or guild.me, track)
player.maybe_shuffle()
if not player.is_playing:
if not player.is_playing and not player.paused:
await player.play()
log.debug("Restored %r", player)
except Exception as exc:
log.debug("Error restoring player in %s", guild_id, exc_info=exc)
await self.api_interface.persistent_queue_api.drop(guild_id)

for guild_id, (notify_channel_id, vc_id) in metadata.items():
for guild_id, (notify_channel_id, vc_id, paused) in metadata.items():
guild = self.bot.get_guild(guild_id)
player: Optional[lavalink.Player] = None
vc = 0
Expand Down Expand Up @@ -284,6 +286,7 @@ async def restore_players(self) -> None:
player.repeat = repeat
player.shuffle = shuffle
player.shuffle_bumped = shuffle_bumped
player._paused = paused
if player.volume != volume:
await player.set_volume(volume)
player.maybe_shuffle()
Expand All @@ -293,8 +296,9 @@ async def restore_players(self) -> None:
await player.resume(
current_track_meta[player.guild.id],
start=current_track_meta[player.guild.id].start_timestamp,
pause=paused,
)
elif not player.is_playing:
elif not player.is_playing and not player.paused:
notify_channel = player.fetch("notify_channel")
try:
await self.api_interface.autoplay(player, self.playlist_api)
Expand Down
2 changes: 1 addition & 1 deletion redbot/cogs/audio/core/utilities/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def save_player_state(self) -> None:
if player.current:
data = self.get_track_json(player, position="np")
data["info"]["timestamp"] = player.position
last_known_track = ("last_known_track", data)
last_known_track = ("last_known_track", data, player.paused)
if last_known_vc_and_notify_channels or last_known_track:
guild_data[guild_id] = []
if last_known_vc_and_notify_channels:
Expand Down

0 comments on commit 3c089c3

Please sign in to comment.