Skip to content

Commit

Permalink
Added ensure_connection decorator.
Browse files Browse the repository at this point in the history
Moved all methods to decorators.
Added bassboost command and get_equalizer.
Added default lavalink server.
  • Loading branch information
adam757521 committed Oct 4, 2021
1 parent 0948421 commit 8103995
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 81 deletions.
34 changes: 6 additions & 28 deletions discordSuperUtils/music/lavalink/lavalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from typing import Optional, TYPE_CHECKING, Dict

import discord
import wavelink

from .equalizer import Equalizer
Expand Down Expand Up @@ -83,39 +82,25 @@ async def _check_queue(self, ctx: commands.Context) -> None:
await self.cleanup(None, ctx.guild)
await self.call_event("on_queue_end", ctx)

@MusicManager.ensure_connection()
async def get_player_played_duration(
self, ctx: commands.Context, _=None
) -> Optional[float]:
if not await self._check_connection(ctx):
return

return ctx.voice_client.position

@MusicManager.ensure_connection(check_playing=True, check_queue=True)
async def volume(
self, ctx: commands.Context, volume: int = None
) -> Optional[float]:
if not await self._check_connection(ctx, True, check_queue=True):
return

if volume is None:
return ctx.voice_client.volume

await ctx.voice_client.set_volume(volume)
self.queue[ctx.guild.id].volume = volume
return ctx.voice_client.volume

async def leave(self, ctx: commands.Context) -> Optional[discord.VoiceChannel]:
if not await self._check_connection(ctx):
return

if ctx.guild.id in self.queue:
self.queue[ctx.guild.id].cleanup()
del self.queue[ctx.guild.id]

await ctx.voice_client.disconnect(force=True)
return ctx.voice_client.channel

def get_equalizer(self, ctx: commands.Context) -> Optional[Equalizer]:
@MusicManager.ensure_connection(check_playing=True)
async def get_equalizer(self, ctx: commands.Context) -> Optional[Equalizer]:
"""
Returns the ctx's equalizer.
Expand All @@ -124,11 +109,9 @@ def get_equalizer(self, ctx: commands.Context) -> Optional[Equalizer]:
:rtype: Optional[Equalizer]
"""

if not await self._check_connection(ctx, check_playing=True):
return

return ctx.voice_client.equalizer or Equalizer.flat()

@MusicManager.ensure_connection(check_playing=True)
async def set_equalizer(
self, ctx: commands.Context, equalizer: Equalizer
) -> Optional[bool]:
Expand All @@ -143,12 +126,10 @@ async def set_equalizer(
:rtype: Optional[bool]
"""

if not await self._check_connection(ctx, check_playing=True):
return

await ctx.voice_client.set_eq(equalizer)
return True

@MusicManager.ensure_connection(check_playing=True)
async def seek(self, ctx: commands.Context, position: int = 0) -> None:
"""
|coro|
Expand All @@ -161,7 +142,4 @@ async def seek(self, ctx: commands.Context, position: int = 0) -> None:
:rtype: None
"""

if not await self._check_connection(ctx, check_playing=True):
return

await ctx.voice_client.seek(position=position)
78 changes: 28 additions & 50 deletions discordSuperUtils/music/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ async def _check_connection(

return True

def ensure_connection(*d_args, **d_kwargs):
def decorator(function):
async def wrapper(self, ctx, *args, **kwargs):
if await self._check_connection(ctx, *d_args, **d_kwargs):
return await function(self, ctx, *args, **kwargs)

return wrapper

return decorator

async def _check_queue(self, ctx: commands.Context) -> None:
"""
|coro|
Expand Down Expand Up @@ -361,6 +371,7 @@ async def _check_queue(self, ctx: commands.Context) -> None:
async def get_player_playlist(self, player: Player) -> Optional[Playlist]:
return await get_playlist(self.spotify, self.youtube, player.used_query)

@ensure_connection()
async def get_player_played_duration(
self, ctx: commands.Context, player: Player
) -> Optional[float]:
Expand All @@ -377,9 +388,6 @@ async def get_player_played_duration(
:rtype: Optional[float]
"""

if not await self._check_connection(ctx):
return

start_timestamp = player.start_timestamp
if ctx.voice_client.is_paused():
start_timestamp = (
Expand Down Expand Up @@ -441,8 +449,9 @@ async def create_player(

return await Player.make_players(self.youtube, query, requester)

@ensure_connection()
async def queue_add(
self, players: List[Player], ctx: commands.Context
self, ctx: commands.Context, players: List[Player],
) -> Optional[bool]:
"""
|coro|
Expand All @@ -458,16 +467,14 @@ async def queue_add(
:rtype: Optional[bool]
"""

if not await self._check_connection(ctx):
return

if ctx.guild.id in self.queue:
self.queue[ctx.guild.id].queue += players
else:
self.queue[ctx.guild.id] = QueueManager(self.default_volume, players)

return True

@ensure_connection(check_queue=True)
async def queue_remove(self, ctx: commands.Context, index: int) -> Optional[Player]:
"""
|coro|
Expand All @@ -483,9 +490,6 @@ async def queue_remove(self, ctx: commands.Context, index: int) -> Optional[Play
:rtype: Optional[Player]
"""

if not await self._check_connection(ctx, check_queue=True):
return

try:
queue = self.queue[ctx.guild.id]

Expand Down Expand Up @@ -529,6 +533,7 @@ async def lyrics(

return (title, authors, lyrics) if lyrics else None

@ensure_connection()
async def play(
self,
ctx: commands.Context,
Expand All @@ -544,13 +549,11 @@ async def play(
:rtype: Optional[bool]
"""

if not await self._check_connection(ctx):
return

if not ctx.voice_client.is_playing():
await self._check_queue(ctx)
return True

@ensure_connection()
async def pause(self, ctx: commands.Context) -> Optional[bool]:
"""
|coro|
Expand All @@ -564,9 +567,6 @@ async def pause(self, ctx: commands.Context) -> Optional[bool]:
:rtype: Optional[bool]
"""

if not await self._check_connection(ctx):
return

if ctx.voice_client.is_paused():
await self.call_event(
"on_music_error", ctx, AlreadyPaused("Player is already paused.")
Expand All @@ -582,6 +582,7 @@ async def pause(self, ctx: commands.Context) -> Optional[bool]:
create_task(self.bot.loop, self.ensure_activity(ctx))
return True

@ensure_connection()
async def resume(self, ctx: commands.Context) -> Optional[bool]:
"""
|coro|
Expand All @@ -595,9 +596,6 @@ async def resume(self, ctx: commands.Context) -> Optional[bool]:
:rtype: Optional[bool]
"""

if not await self._check_connection(ctx):
return

if not ctx.voice_client.is_paused():
await self.call_event(
"on_music_error", ctx, NotPaused("Player is not paused")
Expand All @@ -616,6 +614,7 @@ async def resume(self, ctx: commands.Context) -> Optional[bool]:

return True

@ensure_connection(check_playing=True, check_queue=True)
async def previous(
self, ctx: commands.Context, index: int = None, no_autoplay: bool = False
) -> Optional[List[Player]]:
Expand All @@ -631,9 +630,6 @@ async def previous(
:rtype: Optional[List[Player]]
"""

if not await self._check_connection(ctx, True, check_queue=True):
return

queue = self.queue[ctx.guild.id]

previous_index = 2 if index is None else index + 1
Expand All @@ -659,6 +655,7 @@ async def previous(
await maybe_coroutine(ctx.voice_client.stop)
return previous_players

@ensure_connection(check_playing=True, check_queue=True)
async def skip(self, ctx: commands.Context, index: int = None) -> Optional[Player]:
"""
|coro|
Expand All @@ -674,9 +671,6 @@ async def skip(self, ctx: commands.Context, index: int = None) -> Optional[Playe
:rtype: Optional[Player]
"""

if not await self._check_connection(ctx, True, check_queue=True):
return

queue = self.queue[ctx.guild.id]

# Created duplicate to make sure InvalidSkipIndex isn't raised when the user does pass an index and the queue
Expand Down Expand Up @@ -712,6 +706,7 @@ async def skip(self, ctx: commands.Context, index: int = None) -> Optional[Playe
await maybe_coroutine(ctx.voice_client.stop)
return player

@ensure_connection(check_playing=True, check_queue=True)
async def volume(
self, ctx: commands.Context, volume: int = None
) -> Optional[float]:
Expand All @@ -729,9 +724,6 @@ async def volume(
:rtype: Optional[float]
"""

if not await self._check_connection(ctx, True, check_queue=True):
return

if volume is None:
return ctx.voice_client.source.volume * 100

Expand Down Expand Up @@ -776,6 +768,7 @@ async def join(self, ctx: commands.Context) -> Optional[discord.VoiceChannel]:
)
return channel

@ensure_connection()
async def leave(self, ctx: commands.Context) -> Optional[discord.VoiceChannel]:
"""
|coro|
Expand All @@ -788,17 +781,15 @@ async def leave(self, ctx: commands.Context) -> Optional[discord.VoiceChannel]:
:rtype: Optional[discord.VoiceChannel]
"""

if not await self._check_connection(ctx):
return

if ctx.guild.id in self.queue:
self.queue[ctx.guild.id].cleanup()
del self.queue[ctx.guild.id]

channel = ctx.voice_client.channel
await ctx.voice_client.disconnect()
await ctx.voice_client.disconnect(force=True)
return channel

@ensure_connection(check_queue=True)
async def now_playing(self, ctx: commands.Context) -> Optional[Player]:
"""
|coro|
Expand All @@ -811,9 +802,6 @@ async def now_playing(self, ctx: commands.Context) -> Optional[Player]:
:rtype: Optional[Player]
"""

if not await self._check_connection(ctx, check_queue=True):
return

now_playing = self.queue[ctx.guild.id].now_playing
if not ctx.voice_client.is_playing() and not ctx.voice_client.is_paused():
await self.call_event(
Expand All @@ -824,6 +812,7 @@ async def now_playing(self, ctx: commands.Context) -> Optional[Player]:

return now_playing

@ensure_connection(check_playing=True, check_queue=True)
async def queueloop(self, ctx: commands.Context) -> Optional[bool]:
"""
|coro|
Expand All @@ -836,9 +825,6 @@ async def queueloop(self, ctx: commands.Context) -> Optional[bool]:
:rtype: Optional[bool]
"""

if not await self._check_connection(ctx, check_playing=True, check_queue=True):
return

queue = self.queue[ctx.guild.id]

queue.loop = (
Expand All @@ -852,6 +838,7 @@ async def queueloop(self, ctx: commands.Context) -> Optional[bool]:

return queue.loop == Loops.QUEUE_LOOP

@ensure_connection(check_playing=True, check_queue=True)
async def shuffle(self, ctx: commands.Context) -> Optional[bool]:
"""
|coro|
Expand All @@ -863,12 +850,10 @@ async def shuffle(self, ctx: commands.Context) -> Optional[bool]:
:rtype: Optional[bool]
"""

if not await self._check_connection(ctx, check_playing=True, check_queue=True):
return

self.queue[ctx.guild.id].shuffle = not self.queue[ctx.guild.id].shuffle
return self.queue[ctx.guild.id].shuffle

@ensure_connection(check_playing=True, check_queue=True)
async def autoplay(self, ctx: commands.Context) -> Optional[bool]:
"""
|coro|
Expand All @@ -880,12 +865,10 @@ async def autoplay(self, ctx: commands.Context) -> Optional[bool]:
:rtype: Optional[bool]
"""

if not await self._check_connection(ctx, check_playing=True, check_queue=True):
return

self.queue[ctx.guild.id].autoplay = not self.queue[ctx.guild.id].autoplay
return self.queue[ctx.guild.id].autoplay

@ensure_connection(check_playing=True, check_queue=True)
async def loop(self, ctx: commands.Context) -> Optional[bool]:
"""
|coro|
Expand All @@ -898,14 +881,12 @@ async def loop(self, ctx: commands.Context) -> Optional[bool]:
:rtype: Optional[bool]
"""

if not await self._check_connection(ctx, check_playing=True, check_queue=True):
return

self.queue[ctx.guild.id].loop = (
Loops.LOOP if self.queue[ctx.guild.id].loop != Loops.LOOP else Loops.NO_LOOP
)
return self.queue[ctx.guild.id].loop == Loops.LOOP

@ensure_connection(check_queue=True)
async def get_queue(self, ctx: commands.Context) -> Optional[QueueManager]:
"""
|coro|
Expand All @@ -918,7 +899,4 @@ async def get_queue(self, ctx: commands.Context) -> Optional[QueueManager]:
:rtype: Optional[QueueManager]
"""

if not await self._check_connection(ctx, check_queue=True):
return

return self.queue[ctx.guild.id]
Loading

0 comments on commit 8103995

Please sign in to comment.