Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [feat] Better _ensure_voice_safety() with improved locks #256

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
<br> <img src="static/banner.png">

[![CodeSee](https://github.com/IgKniteDev/IgKnite/actions/workflows/codesee-arch-diagram.yml/badge.svg)](https://github.com/IgKniteDev/IgKnite/actions/workflows/codesee-arch-diagram.yml)
[![Style Check](https://github.com/IgKniteDev/IgKnite/actions/workflows/stylecheck.yml/badge.svg)](https://github.com/IgKniteDev/IgKnite/actions/workflows/stylecheck.yml)
[![Formatting](https://github.com/IgKniteDev/IgKnite/actions/workflows/formatting.yml/badge.svg)](https://github.com/IgKniteDev/IgKnite/actions/workflows/formatting.yml)
[![Linting](https://github.com/IgKniteDev/IgKnite/actions/workflows/linting.yml/badge.svg)](https://github.com/IgKniteDev/IgKnite/actions/workflows/linting.yml)


</div>

Expand Down
32 changes: 16 additions & 16 deletions cogs/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ async def _ensure_voice_safety(
inter: disnake.CommandInter,
*,
skip_self: bool = False,
skip_play: bool = False,
ignore_lock: bool = False,
) -> Any | None:
"""
Expand All @@ -572,6 +573,9 @@ async def _ensure_voice_safety(
f'The voice state has been locked by **{inter.voice_state.locked.display_name}**.'
)

elif not skip_play and not inter.voice_state.current:
return await inter.send("There's nothing being played at the moment.")

else:
return True

Expand Down Expand Up @@ -600,7 +604,7 @@ async def _join_logic(
async def _ensure_play_safety(self, inter: disnake.CommandInter) -> True | False:
if (not inter.voice_state.voice) and (not await self._join_logic(inter)):
return False
elif not await self._ensure_voice_safety(inter, skip_self=True):
elif not await self._ensure_voice_safety(inter, skip_self=True, skip_play=True):
return False
else:
return True
Expand Down Expand Up @@ -650,7 +654,7 @@ async def _join(
dm_permission=False,
)
async def _leave(self, inter: disnake.CommandInter) -> None:
if not await self._ensure_voice_safety(inter):
if not await self._ensure_voice_safety(inter, skip_play=True):
return

await inter.voice_state.stop()
Expand All @@ -672,8 +676,6 @@ async def _volume(
) -> None:
if not await self._ensure_voice_safety(inter):
return
elif not inter.voice_state.is_playing:
return await inter.send("There's nothing being played at the moment.")

inter.voice_state.current.source.volume = (vol_mod := volume / 100)
inter.voice_state.volume = vol_mod
Expand Down Expand Up @@ -704,7 +706,7 @@ async def _lock(self, inter: disnake.CommandInter) -> None:
dm_permission=False,
)
async def _now(self, inter: disnake.CommandInter) -> None:
if inter.voice_state.is_playing:
if inter.voice_state.current:
embed, view = inter.voice_state.current.create_embed(inter)
await inter.send(embed=embed, view=view)
else:
Expand All @@ -724,7 +726,7 @@ async def _pause(self, inter: disnake.CommandInter) -> None:
inter.voice_state.voice.pause()
await inter.send('Paused voice state.')
else:
await inter.send("There's nothing being played at the moment.")
await inter.send('Already paused.')

# resume
@commands.slash_command(
Expand All @@ -740,7 +742,7 @@ async def _resume(self, inter: disnake.CommandInter) -> None:
inter.voice_state.voice.resume()
await inter.send('Resumed voice state.')
else:
await inter.send("Playback isn't paused to be resumed in the first place.")
await inter.send('Already playing.')

# stop
@commands.slash_command(
Expand All @@ -754,12 +756,12 @@ async def _stop(self, inter: disnake.CommandInter) -> None:

inter.voice_state.songs.clear()

if inter.voice_state.is_playing:
if inter.voice_state.loop:
inter.voice_state.loop = not inter.voice_state.loop
if inter.voice_state.loop:
inter.voice_state.loop = not inter.voice_state.loop

inter.voice_state.voice.stop()
await inter.send('Stopped voice state.')
inter.voice_state.voice.stop()
inter.voice_state.current = None
await inter.send('Stopped voice state.')

# skip
@commands.slash_command(
Expand All @@ -770,8 +772,6 @@ async def _stop(self, inter: disnake.CommandInter) -> None:
async def _skip(self, inter: disnake.CommandInter) -> None:
if not await self._ensure_voice_safety(inter):
return
elif not inter.voice_state.is_playing:
return await inter.send("There's nothing being played at the moment.")

if inter.voice_state.loop:
inter.voice_state.loop = not inter.voice_state.loop
Expand Down Expand Up @@ -806,7 +806,7 @@ async def _skip(self, inter: disnake.CommandInter) -> None:
name='queue', description="Shows the player's queue.", dm_permission=False
)
async def _queue(self, inter: disnake.CommandInter) -> None:
if not await self._ensure_voice_safety(inter, ignore_lock=True):
if not await self._ensure_voice_safety(inter, skip_play=True, ignore_lock=True):
return
elif len(songs := inter.voice_state.songs) == 0:
return await inter.send('The queue is empty.')
Expand Down Expand Up @@ -865,7 +865,7 @@ async def _rmqueue(
default=1,
),
):
if not await self._ensure_voice_safety(inter, ignore_lock=True):
if not await self._ensure_voice_safety(inter, skip_play=True, ignore_lock=True):
return
elif len(inter.voice_state.songs) == 0:
return await inter.send('The queue is empty, so nothing to be removed.')
Expand Down
Loading