aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2024-02-19 16:52:51 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-19 16:53:32 +0100
commitbbe053ac2b7a9b5712b0464df72d4283aaa5d08b (patch)
tree8c86509d52b9084c45b2506537be4840141114c2
parent6b3ee1829cb6df6bc2b785e70b0674cb1f81711b (diff)
downloadwee-slack-bbe053ac2b7a9b5712b0464df72d4283aaa5d08b.tar.gz
Support async command functions
-rw-r--r--slack/commands.py21
1 files changed, 15 insertions, 6 deletions
diff --git a/slack/commands.py b/slack/commands.py
index 40c2554..1c057c1 100644
--- a/slack/commands.py
+++ b/slack/commands.py
@@ -8,7 +8,9 @@ from functools import wraps
from itertools import chain
from typing import (
TYPE_CHECKING,
+ Any,
Callable,
+ Coroutine,
Dict,
Iterable,
List,
@@ -37,6 +39,10 @@ if TYPE_CHECKING:
from typing_extensions import Literal
Options = Dict[str, Union[str, Literal[True]]]
+ WeechatCommandCallback = Callable[[str, str], None]
+ InternalCommandCallback = Callable[
+ [str, List[str], Options], Optional[Coroutine[Any, None, None]]
+ ]
T = TypeVar("T")
@@ -75,7 +81,7 @@ class Command:
args: str
args_description: str
completion: str
- cb: Callable[[str, str], None]
+ cb: WeechatCommandCallback
def weechat_command(
@@ -84,12 +90,12 @@ def weechat_command(
split_all_args: bool = False,
slack_buffer_required: bool = False,
) -> Callable[
- [Callable[[str, List[str], Options], None]],
- Callable[[str, str], None],
+ [InternalCommandCallback],
+ WeechatCommandCallback,
]:
def decorator(
- f: Callable[[str, List[str], Options], None],
- ) -> Callable[[str, str], None]:
+ f: InternalCommandCallback,
+ ) -> WeechatCommandCallback:
cmd = removeprefix(f.__name__, "command_").replace("_", " ")
top_level = " " not in cmd
@@ -103,7 +109,10 @@ def weechat_command(
f'Too few arguments for command "/{cmd}" (help on command: /help {cmd})'
)
return
- return f(buffer, split_args, options)
+ result = f(buffer, split_args, options)
+ if result is not None:
+ run_async(result)
+ return
commands[cmd] = Command(cmd, top_level, "", "", "", completion, wrapper)