Skip to content

Commit

Permalink
feat: add player controls
Browse files Browse the repository at this point in the history
  • Loading branch information
ooliver1 committed Oct 22, 2022
1 parent b991b3d commit 5aead6a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 15 deletions.
6 changes: 6 additions & 0 deletions mafic/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"MaficException",
"MultipleCompatibleLibraries",
"NoCompatibleLibraries",
"PlayerNotConnected",
"TrackLoadException",
)

Expand Down Expand Up @@ -38,3 +39,8 @@ def __init__(self, libraries: list[str]) -> None:
class TrackLoadException(MaficException):
def __init__(self, *, message: str, severity: str) -> None:
super().__init__(f"The track could not be loaded: {message} ({severity} error)")


class PlayerNotConnected(MaficException):
def __init__(self) -> None:
super().__init__("The player is not connected to a voice channel.")
4 changes: 4 additions & 0 deletions mafic/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ChannelMix",
"Distortion",
"EQBand",
"Filter",
"Karaoke",
"LowPass",
"Rotation",
Expand Down Expand Up @@ -284,3 +285,6 @@ def __iand__(self, other: Any) -> None:
self.channel_mix = self.channel_mix or other.channel_mix
self.low_pass = self.low_pass or other.low_pass
self.volume = self.volume or other.volume


# TODO: people like easy default filters, add some default EQ and combo filters
14 changes: 7 additions & 7 deletions mafic/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,17 @@ def play(
self,
*,
guild_id: int,
track: str,
start_time: int | None,
end_time: int | None,
volume: int | None,
no_replace: bool | None,
pause: bool | None,
track: Track,
start_time: int | None = None,
end_time: int | None = None,
volume: int | None = None,
no_replace: bool | None = None,
pause: bool | None = None,
) -> Coro[None]:
data: PlayPayload = {
"op": "play",
"guildId": str(guild_id),
"track": track,
"track": track.id,
}

if start_time is not None:
Expand Down
95 changes: 87 additions & 8 deletions mafic/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

from __future__ import annotations

from collections import OrderedDict
from functools import reduce
from logging import getLogger
from operator import or_
from time import time
from typing import TYPE_CHECKING

from .__libraries import GuildChannel, StageChannel, VoiceChannel, VoiceProtocol
from .errors import PlayerNotConnected
from .filter import Filter
from .playlist import Playlist
from .pool import NodePool
from .search_type import SearchType
Expand Down Expand Up @@ -67,6 +72,7 @@ def __init__(
self._last_update: int = 0
self._ping = 0
self._current: Track | None = None
self._filters: OrderedDict[str, Filter] = OrderedDict()

@property
def connected(self) -> bool:
Expand Down Expand Up @@ -215,11 +221,84 @@ async def fetch_tracks(

return await node.fetch_tracks(query, search_type=raw_type)

# TODO: controls:
# TODO: play
# TODO: pause
# TODO: stop
# TODO: filter
# TODO: volume
# TODO: seek
# TODO: pause
async def play(
self,
track: Track,
/,
*,
start_time: int | None = None,
end_time: int | None = None,
volume: int | None = None,
replace: bool = True,
pause: bool | None = None,
) -> None:
if self._node is None or not self._connected:
raise PlayerNotConnected

await self._node.play(
guild_id=self._guild_id,
track=track,
start_time=start_time,
end_time=end_time,
volume=volume,
no_replace=not replace,
pause=pause,
)

self._current = track

async def pause(self, pause: bool = True) -> None:
if self._node is None or not self._connected:
raise PlayerNotConnected

await self._node.pause(guild_id=self._guild_id, pause=pause)

async def resume(self) -> None:
await self.pause(False)

async def stop(self) -> None:
if self._node is None or not self._connected:
raise PlayerNotConnected

await self._node.stop(guild_id=self._guild_id)

async def _update_filters(self, *, fast_apply: bool) -> None:
if self._node is None or not self._connected:
raise PlayerNotConnected

await self._node.filter(
guild_id=self._guild_id, filter=reduce(or_, self._filters.values())
)

if fast_apply:
await self.seek(self.position)

async def add_filter(
self, filter: Filter, /, *, label: str, fast_apply: bool = False
) -> None:
self._filters[label] = filter

await self._update_filters(fast_apply=True)

async def remove_filter(self, label: str, *, fast_apply: bool = False) -> None:
self._filters.pop(label, None)

await self._update_filters(fast_apply=True)

async def clear_filters(self, *, fast_apply: bool = False) -> None:
self._filters.clear()

await self._update_filters(fast_apply=True)

async def set_volume(self, volume: int, /) -> None:
if self._node is None or not self._connected:
raise PlayerNotConnected

await self._node.volume(guild_id=self._guild_id, volume=volume)

async def seek(self, position: int, /) -> None:
if self._node is None or not self._connected:
raise PlayerNotConnected

await self._node.seek(guild_id=self._guild_id, position=position)
self._position = position

0 comments on commit 5aead6a

Please sign in to comment.