Listener
class
Listener (CallbackObject)
¶
Source code in naff/models/naff/listener.py
class Listener(CallbackObject):
event: str
"""Name of the event to listen to."""
callback: Coroutine
"""Coroutine to call when the event is triggered."""
def __init__(self, func: Callable[..., Coroutine], event: str) -> None:
super().__init__()
self.event = event
self.callback = func
@classmethod
def create(cls, event_name: Absent[str | BaseEvent] = MISSING) -> Callable[[Coroutine], "Listener"]:
"""
Decorator for creating an event listener.
Args:
event_name: The name of the event to listen to. If left blank, event name will be inferred from the function name or parameter.
Returns:
A listener object.
"""
def wrapper(coro: Coroutine) -> "Listener":
if not asyncio.iscoroutinefunction(coro):
raise TypeError("Listener must be a coroutine")
name = event_name
if name is MISSING:
for typehint in coro.__annotations__.values():
if (
inspect.isclass(typehint)
and issubclass(typehint, BaseEvent)
and typehint.__name__ != "RawGatewayEvent"
):
name = typehint.__name__
break
if not name:
name = coro.__name__
return cls(coro, get_event_name(name))
return wrapper
classmethod
method
create(event_name)
¶
Decorator for creating an event listener.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_name |
Union[str, naff.api.events.internal.BaseEvent, naff.client.const.Missing] |
The name of the event to listen to. If left blank, event name will be inferred from the function name or parameter. |
Missing |
Returns:
Type | Description |
---|---|
Callable[[Coroutine], Listener] |
A listener object. |
Source code in naff/models/naff/listener.py
@classmethod
def create(cls, event_name: Absent[str | BaseEvent] = MISSING) -> Callable[[Coroutine], "Listener"]:
"""
Decorator for creating an event listener.
Args:
event_name: The name of the event to listen to. If left blank, event name will be inferred from the function name or parameter.
Returns:
A listener object.
"""
def wrapper(coro: Coroutine) -> "Listener":
if not asyncio.iscoroutinefunction(coro):
raise TypeError("Listener must be a coroutine")
name = event_name
if name is MISSING:
for typehint in coro.__annotations__.values():
if (
inspect.isclass(typehint)
and issubclass(typehint, BaseEvent)
and typehint.__name__ != "RawGatewayEvent"
):
name = typehint.__name__
break
if not name:
name = coro.__name__
return cls(coro, get_event_name(name))
return wrapper
async
inherited
method
call_with_binding(self, callback, *args, **kwargs)
¶
Call a given method using this objects _binding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable[..., Coroutine[Any, Any, Any]] |
The callback to call. |
required |
Source code in naff/models/naff/listener.py
async def call_with_binding(self, callback: Callable[..., Coroutine[Any, Any, Any]], *args, **kwargs) -> Any:
"""
Call a given method using this objects _binding.
Args:
callback: The callback to call.
"""
if self._binding:
return await callback(self._binding, *args, **kwargs)
return await callback(*args, **kwargs)
function
listen(event_name)
¶
Decorator to make a function an event listener.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_name |
Union[str, naff.api.events.internal.BaseEvent, naff.client.const.Missing] |
The name of the event to listen to. If left blank, event name will be inferred from the function name or parameter. |
Missing |
Returns:
Type | Description |
---|---|
Callable[[Callable[..., Coroutine]], naff.models.naff.listener.Listener] |
A listener object. |
Source code in naff/models/naff/listener.py
def listen(event_name: Absent[str | BaseEvent] = MISSING) -> Callable[[Callable[..., Coroutine]], Listener]:
"""
Decorator to make a function an event listener.
Args:
event_name: The name of the event to listen to. If left blank, event name will be inferred from the function name or parameter.
Returns:
A listener object.
"""
return Listener.create(event_name)