Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @consume_event decorator #23

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/source/api_references/events.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
====================
Events API Reference
====================

.. automodule:: toolbox.events
:members:
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
hikari>=2.0.0.dev113
hikari>=2.0.0.dev113
typing_extensions>=4.4.0
1 change: 1 addition & 0 deletions toolbox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .commands import *
from .errors import *
from .events import *
from .members import *
from .messages import *
from .roles import *
Expand Down
39 changes: 39 additions & 0 deletions toolbox/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations

import typing as t
import typing_extensions as te

import hikari
import functools

__all__: t.Sequence[str] = ["consume_event"]

P = te.ParamSpec("P")
T = t.TypeVar("T")


def consume_event(
callback: t.Callable[P, t.Awaitable[T]]
) -> t.Callable[te.Concatenate[hikari.Event, P], t.Awaitable[T]]:
"""
Consume the first argument of an event callback.

.. code-block:: python

import hikari
import toolbox

@toolbox.consume_event
async def on_started():
...

bot = hikari.GatewayBot("TOKEN")
bot.subscribe(hikari.StartingEvent, on_started)
bot.run()
"""

@functools.wraps(callback)
async def inner(_event: hikari.Event, /, *args: P.args, **kwargs: P.kwargs) -> T:
return await callback(*args, **kwargs)

return inner