Skip to content

Commit

Permalink
feat!: properly resume players even on full restart (#53)
Browse files Browse the repository at this point in the history
* tests: update test bot for resuming, even after restart

* feat: add from_payload to filters

* feat!: properly resume players

BREAKING CHANGE: players is now a read-only list property

* docs(player): remove set_state
  • Loading branch information
ooliver1 authored Feb 17, 2023
1 parent 8a13874 commit e3d7ba5
Show file tree
Hide file tree
Showing 15 changed files with 815 additions and 309 deletions.
20 changes: 10 additions & 10 deletions docs/api/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ A :class:`Filter` is used with :meth:`Player.add_filter` to modify the playback
.. attributetable:: Filter

.. autoclass:: Filter
:exclude-members: payload
:exclude-members: payload, from_payload

.. attributetable:: ChannelMix

.. autoclass:: ChannelMix
:exclude-members: payload
:exclude-members: payload, from_payload

.. attributetable:: Distortion

.. autoclass:: Distortion
:exclude-members: payload
:exclude-members: payload, from_payload

.. attributetable:: EQBand

Expand All @@ -40,34 +40,34 @@ A :class:`Filter` is used with :meth:`Player.add_filter` to modify the playback
.. attributetable:: Equalizer

.. autoclass:: Equalizer
:exclude-members: payload
:exclude-members: payload, from_payload

.. attributetable:: Karaoke

.. autoclass:: Karaoke
:exclude-members: payload
:exclude-members: payload, from_payload

.. attributetable:: LowPass

.. autoclass:: LowPass
:exclude-members: payload
:exclude-members: payload, from_payload

.. attributetable:: Rotation

.. autoclass:: Rotation
:exclude-members: payload
:exclude-members: payload, from_payload

.. attributetable:: Timescale

.. autoclass:: Timescale
:exclude-members: payload
:exclude-members: payload, from_payload

.. attributetable:: Tremolo

.. autoclass:: Tremolo
:exclude-members: payload
:exclude-members: payload, from_payload

.. attributetable:: Vibrato

.. autoclass:: Vibrato
:exclude-members: payload
:exclude-members: payload, from_payload
2 changes: 1 addition & 1 deletion docs/api/player.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ your Discord library.
.. attributetable:: Player

.. autoclass:: Player
:exclude-members: on_voice_server_update, on_voice_state_update, update_state
:exclude-members: on_voice_server_update, on_voice_state_update, update_state, set_state

.. autoclass:: SearchType
:undoc-members:
109 changes: 109 additions & 0 deletions mafic/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
if TYPE_CHECKING:
from typing import Any

from typing_extensions import Self

from .typings import (
ChannelMix as ChannelMixPayload,
Distortion as DistortionPayload,
Expand Down Expand Up @@ -81,6 +83,12 @@ class Equalizer:
def payload(self) -> list[EQBandPayload]:
return [band.payload for band in self.bands]

@classmethod
def from_payload(cls, payload: list[EQBandPayload]) -> Self:
return cls(
bands=[EQBand(band=band["band"], gain=band["gain"]) for band in payload]
)


@dataclass(unsafe_hash=True)
class Karaoke:
Expand Down Expand Up @@ -131,6 +139,15 @@ def payload(self) -> KaraokePayload:

return data

@classmethod
def from_payload(cls, payload: KaraokePayload) -> Self:
return cls(
level=payload.get("level"),
mono_level=payload.get("monoLevel"),
filter_band=payload.get("filterBand"),
filter_width=payload.get("filterWidth"),
)


@dataclass(unsafe_hash=True)
class Timescale:
Expand Down Expand Up @@ -168,6 +185,14 @@ def payload(self) -> TimescalePayload:

return data

@classmethod
def from_payload(cls, payload: TimescalePayload) -> Self:
return cls(
speed=payload.get("speed"),
pitch=payload.get("pitch"),
rate=payload.get("rate"),
)


@dataclass(unsafe_hash=True)
class Tremolo:
Expand Down Expand Up @@ -205,6 +230,13 @@ def payload(self) -> TremoloPayload:

return data

@classmethod
def from_payload(cls, payload: TremoloPayload) -> Self:
return cls(
frequency=payload.get("frequency"),
depth=payload.get("depth"),
)


@dataclass(repr=True)
class Vibrato:
Expand Down Expand Up @@ -242,6 +274,13 @@ def payload(self) -> VibratoPayload:

return data

@classmethod
def from_payload(cls, payload: VibratoPayload) -> Self:
return cls(
frequency=payload.get("frequency"),
depth=payload.get("depth"),
)


@dataclass(unsafe_hash=True)
class Rotation:
Expand Down Expand Up @@ -271,6 +310,12 @@ def payload(self) -> RotationPayload:

return data

@classmethod
def from_payload(cls, payload: RotationPayload) -> Self:
return cls(
rotation_hz=payload.get("rotationHz"),
)


@dataclass(unsafe_hash=True)
class Distortion:
Expand Down Expand Up @@ -360,6 +405,19 @@ def payload(self) -> DistortionPayload:

return data

@classmethod
def from_payload(cls, payload: DistortionPayload) -> Self:
return cls(
sin_offset=payload.get("sinOffset"),
sin_scale=payload.get("sinScale"),
cos_offset=payload.get("cosOffset"),
cos_scale=payload.get("cosScale"),
tan_offset=payload.get("tanOffset"),
tan_scale=payload.get("tanScale"),
offset=payload.get("offset"),
scale=payload.get("scale"),
)


@dataclass(unsafe_hash=True)
class ChannelMix:
Expand Down Expand Up @@ -408,6 +466,15 @@ def payload(self) -> ChannelMixPayload:

return data

@classmethod
def from_payload(cls, payload: ChannelMixPayload) -> Self:
return cls(
left_to_left=payload.get("leftToLeft"),
left_to_right=payload.get("leftToRight"),
right_to_left=payload.get("rightToLeft"),
right_to_right=payload.get("rightToRight"),
)


@dataclass(unsafe_hash=True)
class LowPass:
Expand Down Expand Up @@ -437,6 +504,10 @@ def payload(self) -> LowPassPayload:

return data

@classmethod
def from_payload(cls, payload: LowPassPayload) -> Self:
return cls(smoothing=payload.get("smoothing"))


@dataclass(unsafe_hash=True)
class Filter:
Expand Down Expand Up @@ -533,6 +604,44 @@ def payload(self) -> Filters:

return payload

@classmethod
def from_payload(cls, data: Filters) -> Self:
"""Generate a filter from a raw Lavalink payload."""

self = cls()

if "equalizer" in data:
self.equalizer = Equalizer.from_payload(data["equalizer"])

if "karaoke" in data:
self.karaoke = Karaoke.from_payload(data["karaoke"])

if "timescale" in data:
self.timescale = Timescale.from_payload(data["timescale"])

if "tremolo" in data:
self.tremolo = Tremolo.from_payload(data["tremolo"])

if "vibrato" in data:
self.vibrato = Vibrato.from_payload(data["vibrato"])

if "rotation" in data:
self.rotation = Rotation.from_payload(data["rotation"])

if "distortion" in data:
self.distortion = Distortion.from_payload(data["distortion"])

if "channelMix" in data:
self.channel_mix = ChannelMix.from_payload(data["channelMix"])

if "lowPass" in data:
self.low_pass = LowPass.from_payload(data["lowPass"])

if "volume" in data:
self.volume = data["volume"]

return self

def __or__(self, other: Any) -> Filter:
# A | B uses A and replaces B, like dictionaries.

Expand Down
Loading

0 comments on commit e3d7ba5

Please sign in to comment.