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

feat: add simple RabbitMQ support #241

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
feat(Bot): add ability to pass custom receiver
  • Loading branch information
hank9999 committed Nov 20, 2024
commit ab04d31cc67c0000ed3f6a926cef1beb99a6b3c4
15 changes: 10 additions & 5 deletions khl/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Dict, Callable, List, Optional, Union, Coroutine, IO

from .. import AsyncRunnable # interfaces
from .. import Cert, HTTPRequester, RateLimiter, WebhookReceiver, WebsocketReceiver, Gateway, Client # net related
from .. import Cert, HTTPRequester, RateLimiter, Receiver, WebhookReceiver, WebsocketReceiver, Gateway, Client # net related
from .. import MessageTypes, EventTypes, SlowModeTypes, SoftwareTypes # types
from .. import User, Channel, PublicChannel, Guild, Event, Message # concepts
from ..command import CommandManager
Expand Down Expand Up @@ -46,6 +46,7 @@ def __init__(self,
cert: Cert = None,
client: Client = None,
gate: Gateway = None,
receiver: Receiver = None,
out: HTTPRequester = None,
compress: bool = True,
port=5000,
Expand All @@ -59,6 +60,7 @@ def __init__(self,
:param cert: used to build requester and receiver
:param client: the bot relies on
:param gate: the client relies on
:param receiver: custom receiver as the gate's component
:param out: the gate's component
:param compress: used to tune the receiver
:param port: used to tune the WebhookReceiver
Expand All @@ -67,7 +69,7 @@ def __init__(self,
if not token and not cert:
raise ValueError('require token or cert')

self._init_client(cert or Cert(token=token), client, gate, out, compress, port, route, ratelimiter)
self._init_client(cert or Cert(token=token), client, gate, receiver, out, compress, port, route, ratelimiter)
self._register_client_handler()

self.command = CommandManager()
Expand All @@ -79,8 +81,8 @@ def __init__(self,
self._startup_index = []
self._shutdown_index = []

def _init_client(self, cert: Cert, client: Client, gate: Gateway, out: HTTPRequester, compress: bool, port, route,
ratelimiter):
def _init_client(self, cert: Cert, client: Client, gate: Gateway, receiver: Receiver, out: HTTPRequester,
compress: bool, port, route, ratelimiter):
"""
construct self.client from args.

Expand All @@ -90,6 +92,7 @@ def _init_client(self, cert: Cert, client: Client, gate: Gateway, out: HTTPReque
:param cert: used to build requester and receiver
:param client: the bot relies on
:param gate: the client relies on
:param receiver: custom receiver as the gate's component
:param out: the gate's component
:param compress: used to tune the receiver
:param port: used to tune the WebhookReceiver
Expand All @@ -105,7 +108,9 @@ def _init_client(self, cert: Cert, client: Client, gate: Gateway, out: HTTPReque

# client and gate not in args, build them
_out = out if out else HTTPRequester(cert, ratelimiter)
if cert.type == Cert.Types.WEBSOCKET:
if receiver:
_in = receiver
elif cert.type == Cert.Types.WEBSOCKET:
_in = WebsocketReceiver(cert, compress)
elif cert.type == Cert.Types.WEBHOOK:
_in = WebhookReceiver(cert, port=port, route=route, compress=compress)
Expand Down