Skip to content

Commit

Permalink
Some more documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Mar 21, 2024
1 parent d470656 commit 3caba54
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/extensions/commands/placehold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Bot
## Hello World!
2 changes: 2 additions & 0 deletions docs/extensions/routines/placehold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Routines
## Hello World!
4 changes: 4 additions & 0 deletions docs/references/payloads.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Payloads
::: twitchio.EventErrorPayload
options:
show_root_heading: true
6 changes: 6 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ nav:
- Placeholder: guides/placeholder.md
- References:
- Client: references/client.md
- Payloads: references/payloads.md
- Asset: references/asset.md
- Models: references/models.md
- Utils: references/utils.md
- Extensions:
- Commands:
- Bot: extensions/commands/placehold.md
- Routines:
- Routine: extensions/routines/placehold.md

markdown_extensions:
- abbr
Expand Down
53 changes: 53 additions & 0 deletions twitchio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def __init__(
self._blocker: asyncio.Event = asyncio.Event()

async def event_error(self, payload: EventErrorPayload) -> None:
"""Event called when an error occurs in an event or event listener.
This event can be overriden to handle event errors differently.
By default, this method logs the error and ignores it.
!!! warning
If an error occurs in this event, it will be ignored and logged. It will **NOT** re-trigger this event.
Parameters
----------
payload: EventErrorPayload
A payload containing the Exception, the listener, and the original payload.
"""
logger.error('Ignoring Exception in listener "%s":\n', payload.listener.__qualname__, exc_info=payload.error)

async def _dispatch(
Expand Down Expand Up @@ -134,9 +147,34 @@ def dispatch(self, event: str, payload: Any | None = None) -> None:
_ = [asyncio.create_task(self._dispatch(listener, original=payload)) for listener in listeners]

async def setup_hook(self) -> None:
"""Method called after [`.login`][twitchio.Client.login] has been called but before the client is started.
[`.start`][twitchio.Client.start] calls [`.login`][twitchio.Client.login] internally for you, so when using
[`.start`][twitchio.Client.start] this method will be called after the client has generated and validated an
app token. The client won't complete start up until this method has completed.
This method is intended to be overriden to provide an async environment for any setup required.
By default, this method does not implement any logic.
"""
...

async def login(self, *, token: str | None = None) -> None:
"""Method to login the client and generate or store an app token.
This method is called automatically when using [`.start`][twitchio.Client.start].
You should not call this method if you are using [`.start`][twitchio.Client.start].
This method calls [`.setup_hook`][twitchio.Client.setup_hook].
!!! note
If no token is provided, the client will attempt to generate a new app token for you.
Parameters
----------
token: str | None
An optional app token to use instead of generating one automatically.
"""
if not token:
payload: ClientCredentialsPayload = await self._http.client_credentials_token()
token = payload.access_token
Expand All @@ -151,6 +189,21 @@ async def __aexit__(self, *_: Any) -> None:
await self.close()

async def start(self, token: str | None = None, *, with_adapter: bool = True) -> None:
"""Method to login the client and create a continuously running event loop.
You should not call [`.login`][twitchio.Client.login] if you are using this method as it is called internally
for you.
!!! note
This method blocks asynchronously until the client is closed.
Parameters
----------
token: str | None
An optional app token to use instead of generating one automatically.
with_adapter: bool
Whether to start and run a web adapter. Defaults to `True`. See: ... for more information.
"""
await self.login(token=token)

if with_adapter:
Expand Down
13 changes: 13 additions & 0 deletions twitchio/payloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@


class EventErrorPayload:
"""Payload received in the [`Client.event_error`][twitchio.Client.event_error] event when an error
occurs during an event listener.
Attributes
----------
error: Exception
The error that occurred.
listener: Callable[..., Coroutine[Any, Any, None]]
The listener that raised the error.
original: Any
The original event payload that was passed to the listener that caused the error.
"""

__slots__ = ("error", "listener", "original")

def __init__(self, *, error: Exception, listener: Callable[..., Coroutine[Any, Any, None]], original: Any) -> None:
Expand Down

0 comments on commit 3caba54

Please sign in to comment.