Skip to content

Commit 57311f1

Browse files
Allow passing multiple callbacks to some add methods (#646)
1 parent e6d73c5 commit 57311f1

File tree

8 files changed

+186
-79
lines changed

8 files changed

+186
-79
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Support for passing multiple callbacks to `add_check`, `add_client_callback`, `add_listener`,
10+
`add_on_close` and `add_on_open`.
11+
812
## [2.10.1a1] - 2022-12-02
913
### Added
1014
- Slash command specific [tanjun.annotations.InteractionChannel][] and

tanjun/abc.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,13 +2419,13 @@ def set_hooks(self, hooks: typing.Optional[Hooks[_ContextT_co]], /) -> Self:
24192419
"""
24202420

24212421
@abc.abstractmethod
2422-
def add_check(self, check: CheckSig, /) -> Self: # TODO: remove or add with_check?
2422+
def add_check(self, *checks: CheckSig) -> Self: # TODO: remove or add with_check?
24232423
"""Add a check to the command.
24242424
24252425
Parameters
24262426
----------
2427-
check
2428-
The check to add.
2427+
*checks
2428+
The checks to add.
24292429
24302430
Returns
24312431
-------
@@ -3343,15 +3343,15 @@ def with_message_command(
33433343
"""
33443344

33453345
@abc.abstractmethod
3346-
def add_listener(self, event: type[hikari.Event], listener: ListenerCallbackSig, /) -> Self:
3346+
def add_listener(self, event: type[hikari.Event], /, *callbacks: ListenerCallbackSig) -> Self:
33473347
"""Add a listener to this component.
33483348
33493349
Parameters
33503350
----------
33513351
event
33523352
The event to listen for.
3353-
listener
3354-
The listener to add.
3353+
*callbacks
3354+
The callbacks to add.
33553355
33563356
Returns
33573357
-------
@@ -4143,7 +4143,7 @@ def remove_component_by_name(self, name: str, /) -> Self:
41434143
"""
41444144

41454145
@abc.abstractmethod
4146-
def add_client_callback(self, name: typing.Union[str, ClientCallbackNames], callback: MetaEventSig, /) -> Self:
4146+
def add_client_callback(self, name: typing.Union[str, ClientCallbackNames], /, *callbacks: MetaEventSig) -> Self:
41474147
"""Add a client callback.
41484148
41494149
Parameters
@@ -4152,10 +4152,10 @@ def add_client_callback(self, name: typing.Union[str, ClientCallbackNames], call
41524152
The name this callback is being registered to.
41534153
41544154
This is case-insensitive.
4155-
callback
4156-
The callback to register.
4155+
*callbacks
4156+
The callbacks to register.
41574157
4158-
This may be sync or async and must return None. The positional and
4158+
These may be sync or async and must return None. The positional and
41594159
keyword arguments a callback should expect depend on implementation
41604160
detail around the `name` being subscribed to.
41614161
@@ -4263,20 +4263,19 @@ async def on_close() -> None:
42634263
"""
42644264

42654265
@abc.abstractmethod
4266-
def add_listener(self, event_type: type[hikari.Event], callback: ListenerCallbackSig, /) -> Self:
4266+
def add_listener(self, event_type: type[hikari.Event], /, *callbacks: ListenerCallbackSig) -> Self:
42674267
"""Add a listener to the client.
42684268
42694269
Parameters
42704270
----------
42714271
event_type
42724272
The event type to add a listener for.
4273-
callback
4274-
The callback to register as a listener.
4273+
*callbacks
4274+
The callbacks to register as a listener.
42754275
4276-
This callback must be a coroutine function which returns [None][]
4277-
and always takes one positional arg of type
4278-
[hikari.events.base_events.Event][] regardless of client
4279-
implementation detail.
4276+
These callbacks must be coroutine functions which returns [None][]
4277+
and always takes one positional arg of the event type passed for
4278+
`event_type` regardless of client implementation detail.
42804279
42814280
Returns
42824281
-------

tanjun/clients.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,15 +1763,15 @@ def set_human_only(self, value: bool = True, /) -> Self:
17631763

17641764
return self
17651765

1766-
def add_check(self, check: tanjun.CheckSig, /) -> Self:
1766+
def add_check(self, *checks: tanjun.CheckSig) -> Self:
17671767
"""Add a generic check to this client.
17681768
17691769
This will be applied to both message and slash command execution.
17701770
17711771
Parameters
17721772
----------
1773-
check
1774-
The check to add. This may be either synchronous or asynchronous
1773+
*checks
1774+
The checks to add. These may be either synchronous or asynchronous
17751775
and must take one positional argument of type [tanjun.abc.Context][]
17761776
with dependency injection being supported for its keyword arguments.
17771777
@@ -1780,8 +1780,9 @@ def add_check(self, check: tanjun.CheckSig, /) -> Self:
17801780
Self
17811781
The client instance to enable chained calls.
17821782
"""
1783-
if check not in self._checks:
1784-
self._checks.append(check)
1783+
for check in checks:
1784+
if check not in self._checks:
1785+
self._checks.append(check)
17851786

17861787
return self
17871788

@@ -1884,17 +1885,20 @@ def remove_component_by_name(self, name: str, /) -> Self:
18841885
return self.remove_component(self._components[name])
18851886

18861887
def add_client_callback(
1887-
self, name: typing.Union[str, tanjun.ClientCallbackNames], callback: tanjun.MetaEventSig, /
1888+
self, name: typing.Union[str, tanjun.ClientCallbackNames], /, *callbacks: tanjun.MetaEventSig
18881889
) -> Self:
18891890
# <<inherited docstring from tanjun.abc.Client>>.
18901891
name = name.casefold()
1891-
try:
1892-
if callback in self._client_callbacks[name]:
1893-
return self
1892+
for callback in callbacks:
1893+
try:
1894+
if callback in self._client_callbacks[name]:
1895+
continue
18941896

1895-
self._client_callbacks[name].append(callback)
1896-
except KeyError:
1897-
self._client_callbacks[name] = [callback]
1897+
except KeyError:
1898+
self._client_callbacks[name] = [callback]
1899+
1900+
else:
1901+
self._client_callbacks[name].append(callback)
18981902

18991903
return self
19001904

@@ -1938,20 +1942,22 @@ def decorator(callback: _MetaEventSigT, /) -> _MetaEventSigT:
19381942

19391943
return decorator
19401944

1941-
def add_listener(self, event_type: type[hikari.Event], callback: tanjun.ListenerCallbackSig, /) -> Self:
1945+
def add_listener(self, event_type: type[hikari.Event], /, *callbacks: tanjun.ListenerCallbackSig) -> Self:
19421946
# <<inherited docstring from tanjun.abc.Client>>.
1943-
injected = self.injector.as_async_self_injecting(callback)
1944-
try:
1945-
if callback in self._listeners[event_type]:
1946-
return self
1947+
for callback in callbacks:
1948+
injected = self.injector.as_async_self_injecting(callback)
1949+
try:
1950+
if callback in self._listeners[event_type]:
1951+
continue
19471952

1948-
self._listeners[event_type][callback] = injected
1953+
except KeyError:
1954+
self._listeners[event_type] = {callback: injected}
19491955

1950-
except KeyError:
1951-
self._listeners[event_type] = {callback: injected}
1956+
else:
1957+
self._listeners[event_type][callback] = injected
19521958

1953-
if self._loop and self._events:
1954-
self._events.subscribe(event_type, injected.__call__)
1959+
if self._loop and self._events:
1960+
self._events.subscribe(event_type, injected.__call__)
19551961

19561962
return self
19571963

tanjun/commands/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ def set_metadata(self, key: typing.Any, value: typing.Any, /) -> Self:
9898
self._metadata[key] = value
9999
return self
100100

101-
def add_check(self, check: tanjun.CheckSig, /) -> Self:
101+
def add_check(self, *checks: tanjun.CheckSig) -> Self:
102102
# <<inherited docstring from tanjun.abc.ExecutableCommand>>.
103-
if check not in self._checks:
104-
self._checks.append(check)
103+
for check in checks:
104+
if check not in self._checks:
105+
self._checks.append(check)
105106

106107
return self
107108

tanjun/components.py

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -565,21 +565,22 @@ def set_slash_hooks(self, hooks: typing.Optional[tanjun.SlashHooks], /) -> Self:
565565
self._slash_hooks = hooks
566566
return self
567567

568-
def add_check(self, check: tanjun.CheckSig, /) -> Self:
568+
def add_check(self, *checks: tanjun.CheckSig) -> Self:
569569
"""Add a command check to this component to be used for all its commands.
570570
571571
Parameters
572572
----------
573-
check
574-
The check to add.
573+
*checks
574+
The checks to add.
575575
576576
Returns
577577
-------
578578
Self
579579
This component to enable method chaining.
580580
"""
581-
if check not in self._checks:
582-
self._checks.append(check)
581+
for check in checks:
582+
if check not in self._checks:
583+
self._checks.append(check)
583584

584585
return self
585586

@@ -621,7 +622,7 @@ def with_check(self, check: _CheckSigT, /) -> _CheckSigT:
621622
return check
622623

623624
def add_client_callback(
624-
self, name: typing.Union[str, tanjun.ClientCallbackNames], callback: tanjun.MetaEventSig, /
625+
self, name: typing.Union[str, tanjun.ClientCallbackNames], /, *callbacks: tanjun.MetaEventSig
625626
) -> Self:
626627
"""Add a client callback.
627628
@@ -631,10 +632,10 @@ def add_client_callback(
631632
The name this callback is being registered to.
632633
633634
This is case-insensitive.
634-
callback
635-
The callback to register.
635+
*callbacks
636+
The callbacks to register.
636637
637-
This may be sync or async and must return None. The positional and
638+
These may be sync or async and must return None. The positional and
638639
keyword arguments a callback should expect depend on implementation
639640
detail around the `name` being subscribed to.
640641
@@ -644,16 +645,19 @@ def add_client_callback(
644645
The client instance to enable chained calls.
645646
"""
646647
name = name.lower()
647-
try:
648-
if callback in self._client_callbacks[name]:
649-
return self
648+
for callback in callbacks:
649+
try:
650+
if callback in self._client_callbacks[name]:
651+
continue
650652

651-
self._client_callbacks[name].append(callback)
652-
except KeyError:
653-
self._client_callbacks[name] = [callback]
653+
except KeyError:
654+
self._client_callbacks[name] = [callback]
654655

655-
if self._client:
656-
self._client.add_client_callback(name, callback)
656+
else:
657+
self._client_callbacks[name].append(callback)
658+
659+
if self._client:
660+
self._client.add_client_callback(name, callback)
657661

658662
return self
659663

@@ -982,19 +986,21 @@ def with_message_command(
982986
# <<inherited docstring from tanjun.abc.Component>>.
983987
return _with_command(self.add_message_command, command, copy=copy)
984988

985-
def add_listener(self, event: type[hikari.Event], listener: tanjun.ListenerCallbackSig, /) -> Self:
989+
def add_listener(self, event: type[hikari.Event], /, *callbacks: tanjun.ListenerCallbackSig) -> Self:
986990
# <<inherited docstring from tanjun.abc.Component>>.
987-
try:
988-
if listener in self._listeners[event]:
989-
return self
991+
for listener in callbacks:
992+
try:
993+
if listener in self._listeners[event]:
994+
continue
990995

991-
self._listeners[event].append(listener)
996+
except KeyError:
997+
self._listeners[event] = [listener]
992998

993-
except KeyError:
994-
self._listeners[event] = [listener]
999+
else:
1000+
self._listeners[event].append(listener)
9951001

996-
if self._client:
997-
self._client.add_listener(event, listener)
1002+
if self._client:
1003+
self._client.add_listener(event, listener)
9981004

9991005
return self
10001006

@@ -1022,7 +1028,7 @@ def decorator(callback: _ListenerCallbackSigT, /) -> _ListenerCallbackSigT:
10221028

10231029
return decorator
10241030

1025-
def add_on_close(self, callback: OnCallbackSig, /) -> Self:
1031+
def add_on_close(self, *callbacks: OnCallbackSig) -> Self:
10261032
"""Add a close callback to this component.
10271033
10281034
!!! note
@@ -1032,8 +1038,8 @@ def add_on_close(self, callback: OnCallbackSig, /) -> Self:
10321038
10331039
Parameters
10341040
----------
1035-
callback
1036-
The close callback to add to this component.
1041+
*callbacks
1042+
The close callbacks to add to this component.
10371043
10381044
This should take no positional arguments, return [None][] and may
10391045
take use injected dependencies.
@@ -1043,7 +1049,7 @@ def add_on_close(self, callback: OnCallbackSig, /) -> Self:
10431049
Self
10441050
The component object to enable call chaining.
10451051
"""
1046-
self._on_close.append(callback)
1052+
self._on_close.extend(callbacks)
10471053
return self
10481054

10491055
def with_on_close(self, callback: _OnCallbackSigT, /) -> _OnCallbackSigT:
@@ -1060,7 +1066,7 @@ def with_on_close(self, callback: _OnCallbackSigT, /) -> _OnCallbackSigT:
10601066
The close callback to add to this component.
10611067
10621068
This should take no positional arguments, return [None][] and may
1063-
take use injected dependencies.
1069+
request injected dependencies.
10641070
10651071
Returns
10661072
-------
@@ -1070,7 +1076,7 @@ def with_on_close(self, callback: _OnCallbackSigT, /) -> _OnCallbackSigT:
10701076
self.add_on_close(callback)
10711077
return callback
10721078

1073-
def add_on_open(self, callback: OnCallbackSig, /) -> Self:
1079+
def add_on_open(self, *callbacks: OnCallbackSig) -> Self:
10741080
"""Add a open callback to this component.
10751081
10761082
!!! note
@@ -1080,18 +1086,18 @@ def add_on_open(self, callback: OnCallbackSig, /) -> Self:
10801086
10811087
Parameters
10821088
----------
1083-
callback
1084-
The open callback to add to this component.
1089+
*callbacks
1090+
The open callbacks to add to this component.
10851091
1086-
This should take no positional arguments, return [None][] and may
1087-
take use injected dependencies.
1092+
These should take no positional arguments, return [None][] and may
1093+
request injected dependencies.
10881094
10891095
Returns
10901096
-------
10911097
Self
10921098
The component object to enable call chaining.
10931099
"""
1094-
self._on_open.append(callback)
1100+
self._on_open.extend(callbacks)
10951101
return self
10961102

10971103
def with_on_open(self, callback: _OnCallbackSigT, /) -> _OnCallbackSigT:

0 commit comments

Comments
 (0)