Skip to content

Commit 72f45b0

Browse files
committed
feat(Bot): add ability to pass custom receiver
1 parent a1de10d commit 72f45b0

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

khl/bot/bot.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Dict, Callable, List, Optional, Union, Coroutine, IO
77

88
from .. import AsyncRunnable # interfaces
9-
from .. import Cert, HTTPRequester, RateLimiter, WebhookReceiver, WebsocketReceiver, Gateway, Client # net related
9+
from .. import Cert, HTTPRequester, RateLimiter, Receiver, WebhookReceiver, WebsocketReceiver, Gateway, Client # net related
1010
from .. import MessageTypes, EventTypes, SlowModeTypes, SoftwareTypes # types
1111
from .. import User, Channel, PublicChannel, Guild, Event, Message # concepts
1212
from ..command import CommandManager
@@ -46,6 +46,7 @@ def __init__(self,
4646
cert: Cert = None,
4747
client: Client = None,
4848
gate: Gateway = None,
49+
receiver: Receiver = None,
4950
out: HTTPRequester = None,
5051
compress: bool = True,
5152
port=5000,
@@ -59,6 +60,7 @@ def __init__(self,
5960
:param cert: used to build requester and receiver
6061
:param client: the bot relies on
6162
:param gate: the client relies on
63+
:param receiver: custom receiver as the gate's component
6264
:param out: the gate's component
6365
:param compress: used to tune the receiver
6466
:param port: used to tune the WebhookReceiver
@@ -67,7 +69,7 @@ def __init__(self,
6769
if not token and not cert:
6870
raise ValueError('require token or cert')
6971

70-
self._init_client(cert or Cert(token=token), client, gate, out, compress, port, route, ratelimiter)
72+
self._init_client(cert or Cert(token=token), client, gate, receiver, out, compress, port, route, ratelimiter)
7173
self._register_client_handler()
7274

7375
self.command = CommandManager()
@@ -79,8 +81,8 @@ def __init__(self,
7981
self._startup_index = []
8082
self._shutdown_index = []
8183

82-
def _init_client(self, cert: Cert, client: Client, gate: Gateway, out: HTTPRequester, compress: bool, port, route,
83-
ratelimiter):
84+
def _init_client(self, cert: Cert, client: Client, gate: Gateway, receiver: Receiver, out: HTTPRequester,
85+
compress: bool, port, route, ratelimiter):
8486
"""
8587
construct self.client from args.
8688
@@ -90,6 +92,7 @@ def _init_client(self, cert: Cert, client: Client, gate: Gateway, out: HTTPReque
9092
:param cert: used to build requester and receiver
9193
:param client: the bot relies on
9294
:param gate: the client relies on
95+
:param receiver: custom receiver as the gate's component
9396
:param out: the gate's component
9497
:param compress: used to tune the receiver
9598
:param port: used to tune the WebhookReceiver
@@ -105,7 +108,9 @@ def _init_client(self, cert: Cert, client: Client, gate: Gateway, out: HTTPReque
105108

106109
# client and gate not in args, build them
107110
_out = out if out else HTTPRequester(cert, ratelimiter)
108-
if cert.type == Cert.Types.WEBSOCKET:
111+
if receiver:
112+
_in = receiver
113+
elif cert.type == Cert.Types.WEBSOCKET:
109114
_in = WebsocketReceiver(cert, compress)
110115
elif cert.type == Cert.Types.WEBHOOK:
111116
_in = WebhookReceiver(cert, port=port, route=route, compress=compress)

0 commit comments

Comments
 (0)