Skip to content

Commit 83036da

Browse files
style(pre-commit): auto fixes from pre-commit.com hooks
1 parent 0b7c147 commit 83036da

File tree

13 files changed

+431
-306
lines changed

13 files changed

+431
-306
lines changed

discord/sinks/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"""
1010

1111
from .core import *
12-
from .errors import *
1312
from .enums import *
13+
from .errors import *
1414
from .m4a import *
1515
from .mka import *
1616
from .mkv import *

discord/sinks/core.py

Lines changed: 102 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
from __future__ import annotations
2727

2828
import asyncio
29-
from collections.abc import Callable, Coroutine, Iterable
30-
from functools import partial
3129
import logging
3230
import struct
3331
import sys
3432
import time
33+
from collections.abc import Callable, Coroutine, Iterable
34+
from functools import partial
3535
from typing import TYPE_CHECKING, Any, Generic, TypeVar, overload
3636

3737
from discord import utils
@@ -44,16 +44,17 @@
4444
from typing_extensions import ParamSpec
4545

4646
from discord import abc
47+
4748
from ..voice.client import VoiceClient
4849

49-
R = TypeVar('R')
50-
P = ParamSpec('P')
50+
R = TypeVar("R")
51+
P = ParamSpec("P")
5152

5253
__all__ = (
5354
"Sink",
5455
"RawData",
55-
'SinkFilter',
56-
'SinkHandler',
56+
"SinkFilter",
57+
"SinkHandler",
5758
)
5859

5960

@@ -63,7 +64,7 @@
6364
CREATE_NO_WINDOW = 0x08000000
6465

6566

66-
S = TypeVar('S', bound='Sink')
67+
S = TypeVar("S", bound="Sink")
6768
_log = logging.getLogger(__name__)
6869

6970

@@ -76,12 +77,16 @@ class SinkFilter(Generic[S]):
7677
"""
7778

7879
@overload
79-
async def filter_packet(self, sink: S, user: abc.Snowflake, packet: RawData) -> bool: ...
80+
async def filter_packet(
81+
self, sink: S, user: abc.Snowflake, packet: RawData
82+
) -> bool: ...
8083

8184
@overload
8285
def filter_packet(self, sink: S, user: abc.Snowflake, packet: RawData) -> bool: ...
8386

84-
def filter_packet(self, sink: S, user: abc.Snowflake, packet: RawData) -> bool | Coroutine[Any, Any, bool]:
87+
def filter_packet(
88+
self, sink: S, user: abc.Snowflake, packet: RawData
89+
) -> bool | Coroutine[Any, Any, bool]:
8590
"""|maybecoro|
8691
8792
This is called automatically everytime a voice packet is received.
@@ -102,15 +107,21 @@ def filter_packet(self, sink: S, user: abc.Snowflake, packet: RawData) -> bool |
102107
:class:`bool`
103108
Whether the filter was successful.
104109
"""
105-
raise NotImplementedError('subclasses must implement this')
110+
raise NotImplementedError("subclasses must implement this")
106111

107112
@overload
108-
async def filter_speaking_state(self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState) -> bool: ...
113+
async def filter_speaking_state(
114+
self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState
115+
) -> bool: ...
109116

110117
@overload
111-
def filter_speaking_state(self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState) -> bool: ...
118+
def filter_speaking_state(
119+
self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState
120+
) -> bool: ...
112121

113-
def filter_speaking_state(self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState) -> bool | Coroutine[Any, Any, bool]:
122+
def filter_speaking_state(
123+
self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState
124+
) -> bool | Coroutine[Any, Any, bool]:
114125
"""|maybecoro|
115126
116127
This is called automatically everytime a speaking state is updated.
@@ -133,11 +144,10 @@ def filter_speaking_state(self, sink: S, user: abc.Snowflake, before: SpeakingSt
133144
:class:`bool`
134145
Whether the filter was successful.
135146
"""
136-
raise NotImplementedError('subclasses must implement this')
147+
raise NotImplementedError("subclasses must implement this")
137148

138149
def cleanup(self) -> None:
139150
"""A function called when the filter is ready for cleanup."""
140-
pass
141151

142152

143153
class SinkHandler(Generic[S]):
@@ -149,12 +159,16 @@ class SinkHandler(Generic[S]):
149159
"""
150160

151161
@overload
152-
async def handle_packet(self, sink: S, user: abc.Snowflake, packet: RawData) -> Any: ...
162+
async def handle_packet(
163+
self, sink: S, user: abc.Snowflake, packet: RawData
164+
) -> Any: ...
153165

154166
@overload
155167
def handle_packet(self, sink: S, user: abc.Snowflake, packet: RawData) -> Any: ...
156168

157-
def handle_packet(self, sink: S, user: abc.Snowflake, packet: RawData) -> Any | Coroutine[Any, Any, Any]:
169+
def handle_packet(
170+
self, sink: S, user: abc.Snowflake, packet: RawData
171+
) -> Any | Coroutine[Any, Any, Any]:
158172
"""|maybecoro|
159173
160174
This is called automatically everytime a voice packet which has successfully passed the filters is received.
@@ -168,15 +182,20 @@ def handle_packet(self, sink: S, user: abc.Snowflake, packet: RawData) -> Any |
168182
packet: :class:`~.RawData`
169183
The raw data packet.
170184
"""
171-
pass
172185

173186
@overload
174-
async def handle_speaking_state(self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState) -> Any: ...
187+
async def handle_speaking_state(
188+
self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState
189+
) -> Any: ...
175190

176191
@overload
177-
def handle_speaking_state(self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState) -> Any: ...
192+
def handle_speaking_state(
193+
self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState
194+
) -> Any: ...
178195

179-
def handle_speaking_state(self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState) -> Any | Coroutine[Any, Any, Any]:
196+
def handle_speaking_state(
197+
self, sink: S, user: abc.Snowflake, before: SpeakingState, after: SpeakingState
198+
) -> Any | Coroutine[Any, Any, Any]:
180199
"""|maybecoro|
181200
182201
This is called automatically everytime a speaking state update is received which has successfully passed the filters.
@@ -192,11 +211,9 @@ def handle_speaking_state(self, sink: S, user: abc.Snowflake, before: SpeakingSt
192211
after: :class:`~discord.SpeakingState`
193212
The speaking state after the update.
194213
"""
195-
pass
196214

197215
def cleanup(self) -> None:
198216
"""A function called when the handler is ready for cleanup."""
199-
pass
200217

201218

202219
class RawData:
@@ -340,10 +357,12 @@ def __init_subclass__(cls) -> None:
340357
elif isinstance(value, classmethod):
341358
value = partial(value.__func__, cls)
342359

343-
if not hasattr(value, '__listener__'):
360+
if not hasattr(value, "__listener__"):
344361
continue
345362

346-
event_name = getattr(value, '__listener_name__', elem).removeprefix('on_')
363+
event_name = getattr(value, "__listener_name__", elem).removeprefix(
364+
"on_"
365+
)
347366

348367
try:
349368
listeners[event_name].append(value)
@@ -364,7 +383,9 @@ def __init__(
364383
self._filters: list[SinkFilter] = filters or []
365384
self._handlers: list[SinkHandler] = handlers or []
366385
self.__dispatch_set: set[asyncio.Task[Any]] = set()
367-
self._listeners: dict[str, list[Callable[[Iterable[object]], bool]]] = self.__listeners__
386+
self._listeners: dict[str, list[Callable[[Iterable[object]], bool]]] = (
387+
self.__listeners__
388+
)
368389

369390
@property
370391
def filtering_mode(self) -> SinkFilteringMode:
@@ -377,13 +398,15 @@ def filtering_mode(self, value: SinkFilteringMode) -> None:
377398
elif value is SinkFilteringMode.any:
378399
self._filter_strat = any
379400
else:
380-
raise TypeError(f'expected a FilteringMode enum member, got {value.__class__.__name__}')
401+
raise TypeError(
402+
f"expected a FilteringMode enum member, got {value.__class__.__name__}"
403+
)
381404

382405
self.__filtering_mode = value
383406

384407
def dispatch(self, event: str, *args: Any, **kwargs: Any) -> Any:
385-
_log.debug('Dispatching sink %s event %s', self.__class__.__name__, event)
386-
method = f'on_{event}'
408+
_log.debug("Dispatching sink %s event %s", self.__class__.__name__, event)
409+
method = f"on_{event}"
387410

388411
listeners = self.__listeners__.get(event, [])
389412
for coro in listeners:
@@ -401,7 +424,7 @@ async def _run_event(
401424
coro: Callable[..., Coroutine[Any, Any, Any]],
402425
event_name: str,
403426
*args: Any,
404-
**kwargs: Any
427+
**kwargs: Any,
405428
) -> None:
406429
try:
407430
await coro(*args, **kwargs)
@@ -426,7 +449,9 @@ def _call_voice_packet_handlers(self, user: abc.Snowflake, packet: RawData) -> N
426449
self.__dispatch_set.add(task)
427450
task.add_done_callback(self.__dispatch_set.remove)
428451

429-
def _call_speaking_state_handlers(self, user: abc.Snowflake, before: SpeakingState, after: SpeakingState) -> None:
452+
def _call_speaking_state_handlers(
453+
self, user: abc.Snowflake, before: SpeakingState, after: SpeakingState
454+
) -> None:
430455
for handler in self._handlers:
431456
task = asyncio.create_task(
432457
utils.maybe_coroutine(
@@ -449,13 +474,13 @@ def _schedule_event(
449474
) -> asyncio.Task:
450475
wrapped = self._run_event(coro, event_name, *args, **kwargs)
451476

452-
task = asyncio.create_task(wrapped, name=f'sinks: {event_name}')
477+
task = asyncio.create_task(wrapped, name=f"sinks: {event_name}")
453478
self.__dispatch_set.add(task)
454479
task.add_done_callback(self.__dispatch_set.discard)
455480
return task
456481

457482
def __repr__(self) -> str:
458-
return f'<{self.__class__.__name__} id={id(self):#x}>'
483+
return f"<{self.__class__.__name__} id={id(self):#x}>"
459484

460485
def stop(self) -> None:
461486
"""Stops this sink's recording.
@@ -499,7 +524,9 @@ def add_filter(self, filter: SinkFilter, /) -> None:
499524
"""
500525

501526
if not isinstance(filter, SinkFilter):
502-
raise TypeError(f'expected a Filter object, not {filter.__class__.__name__}')
527+
raise TypeError(
528+
f"expected a Filter object, not {filter.__class__.__name__}"
529+
)
503530
self._filters.append(filter)
504531

505532
def remove_filter(self, filter: SinkFilter, /) -> None:
@@ -531,7 +558,9 @@ def add_handler(self, handler: SinkHandler, /) -> None:
531558
"""
532559

533560
if not isinstance(handler, SinkHandler):
534-
raise TypeError(f'expected a Handler object, not {handler.__class__.__name__}')
561+
raise TypeError(
562+
f"expected a Handler object, not {handler.__class__.__name__}"
563+
)
535564
self._handlers.append(handler)
536565

537566
def remove_handler(self, handler: SinkHandler, /) -> None:
@@ -549,22 +578,16 @@ def remove_handler(self, handler: SinkHandler, /) -> None:
549578
pass
550579

551580
@staticmethod
552-
def listener(event: str = MISSING) -> Callable[[Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]]]:
581+
def listener(
582+
event: str = MISSING,
583+
) -> Callable[
584+
[Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]]
585+
]:
553586
"""Registers a function to be an event listener for this sink.
554587
555588
The events must be a :ref:`coroutine <coroutine>`, if not, :exc:`TypeError` is raised; and
556589
also must be inside a sink class.
557590
558-
Example
559-
-------
560-
561-
.. code-block:: python3
562-
563-
class MySink(Sink):
564-
@Sink.listener()
565-
async def on_member_speaking_state_update(member, ssrc, state):
566-
pass
567-
568591
Parameters
569592
----------
570593
event: :class:`str`
@@ -574,41 +597,64 @@ async def on_member_speaking_state_update(member, ssrc, state):
574597
------
575598
TypeError
576599
The coroutine passed is not actually a coroutine, or the listener is not in a sink class.
600+
601+
Example
602+
-------
603+
604+
.. code-block:: python3
605+
606+
class MySink(Sink):
607+
@Sink.listener()
608+
async def on_member_speaking_state_update(member, ssrc, state):
609+
pass
577610
"""
578611

579-
def decorator(func: Callable[P, Coroutine[Any, Any, R]]) -> Callable[P, Coroutine[Any, Any, R]]:
580-
parts = func.__qualname__.split('.')
612+
def decorator(
613+
func: Callable[P, Coroutine[Any, Any, R]],
614+
) -> Callable[P, Coroutine[Any, Any, R]]:
615+
parts = func.__qualname__.split(".")
581616

582617
if not parts or not len(parts) > 1:
583-
raise TypeError('event listeners must be declared in a Sink class')
618+
raise TypeError("event listeners must be declared in a Sink class")
584619

585620
if parts[-1] != func.__name__:
586-
raise NameError('qualified name and function name mismatch, this should not happen')
621+
raise NameError(
622+
"qualified name and function name mismatch, this should not happen"
623+
)
587624

588625
if not asyncio.iscoroutinefunction(func):
589-
raise TypeError('event listeners must be coroutine functions')
626+
raise TypeError("event listeners must be coroutine functions")
590627

591628
func.__listener__ = True
592629
if event is not MISSING:
593630
func.__listener_name__ = event
594631
return func
632+
595633
return decorator
596634

597635
async def on_voice_packet_receive(self, user: abc.Snowflake, data: RawData) -> None:
598636
pass
599637

600-
async def on_unfiltered_voice_packet_receive(self, user: abc.Snowflake, data: RawData) -> None:
638+
async def on_unfiltered_voice_packet_receive(
639+
self, user: abc.Snowflake, data: RawData
640+
) -> None:
601641
pass
602642

603-
async def on_speaking_state_update(self, user: abc.Snowflake, before: SpeakingState, after: SpeakingState) -> None:
643+
async def on_speaking_state_update(
644+
self, user: abc.Snowflake, before: SpeakingState, after: SpeakingState
645+
) -> None:
604646
pass
605647

606-
async def on_unfiltered_speaking_state_update(self, user: abc.Snowflake, before: SpeakingState, after: SpeakingState) -> None:
648+
async def on_unfiltered_speaking_state_update(
649+
self, user: abc.Snowflake, before: SpeakingState, after: SpeakingState
650+
) -> None:
607651
pass
608652

609-
async def on_error(self, event: str, exception: Exception, *args: Any, **kwargs: Any) -> None:
653+
async def on_error(
654+
self, event: str, exception: Exception, *args: Any, **kwargs: Any
655+
) -> None:
610656
_log.exception(
611-
'An error ocurred in sink %s while dispatching the event %s',
657+
"An error ocurred in sink %s while dispatching the event %s",
612658
self,
613659
event,
614660
exc_info=exception,

discord/sinks/enums.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323
DEALINGS IN THE SOFTWARE.
2424
"""
25+
2526
from __future__ import annotations
2627

2728
from discord.enums import Enum
2829

29-
__all__ = (
30-
'SinkFilteringMode',
31-
)
30+
__all__ = ("SinkFilteringMode",)
3231

3332

3433
class SinkFilteringMode(Enum):

0 commit comments

Comments
 (0)