|
|
|
|
@ -2,17 +2,22 @@ import html
|
|
|
|
|
import json |
|
|
|
|
import logging |
|
|
|
|
|
|
|
|
|
from asyncio import sleep, gather |
|
|
|
|
|
|
|
|
|
import websockets |
|
|
|
|
|
|
|
|
|
from tools.coco.client import CocoClient |
|
|
|
|
from tools.commons import AbstractResponse, Message, BotMessage, AttackCommand, Sound, UserList |
|
|
|
|
from tools.processors import MessageDispatcher, ConnectionDispatcher |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CoboBot: |
|
|
|
|
COCO_PULSE_TICK = 1 # number of seconds between each check |
|
|
|
|
|
|
|
|
|
def __init__(self, cookie : str, channel : str, domain : str, port : int, method : str, |
|
|
|
|
messages_dispatcher : MessageDispatcher, |
|
|
|
|
connect_dispatcher : ConnectionDispatcher): |
|
|
|
|
def __init__(self, cookie: str, channel: str, domain: str, port: int, method: str, |
|
|
|
|
messages_dispatcher: MessageDispatcher, |
|
|
|
|
connect_dispatcher: ConnectionDispatcher, |
|
|
|
|
cococlient: CocoClient): |
|
|
|
|
|
|
|
|
|
# setting up variables required by the server. The default is a Kabutops on the main lou server, I think |
|
|
|
|
self.cookie = cookie |
|
|
|
|
@ -23,6 +28,7 @@ class CoboBot:
|
|
|
|
|
self.msg_dispatch = messages_dispatcher |
|
|
|
|
self.cnt_dispatch = connect_dispatcher |
|
|
|
|
self.user_list = None # type: UserList |
|
|
|
|
self.cococlient = cococlient |
|
|
|
|
|
|
|
|
|
async def _send_message(self, message): |
|
|
|
|
if isinstance(message, dict): |
|
|
|
|
@ -64,6 +70,40 @@ class CoboBot:
|
|
|
|
|
elif isinstance(response, AbstractResponse): |
|
|
|
|
await self._dispatch_response(response) |
|
|
|
|
|
|
|
|
|
async def socket_listener(self): |
|
|
|
|
while True: |
|
|
|
|
msg = await self.socket.recv() |
|
|
|
|
if type(msg) != bytes: |
|
|
|
|
msg_data = json.loads(msg, encoding="utf-8") |
|
|
|
|
msg_type = msg_data.get("type", "") |
|
|
|
|
if msg_type == "userlist": |
|
|
|
|
self.user_list = UserList(msg_data["users"]) |
|
|
|
|
logging.info(str(self.user_list)) |
|
|
|
|
|
|
|
|
|
elif msg_type == "msg": |
|
|
|
|
await self._on_message(msg_data) |
|
|
|
|
|
|
|
|
|
elif msg_type == "connect": |
|
|
|
|
await self._on_connect(msg_data) |
|
|
|
|
|
|
|
|
|
elif msg_type == "disconnect": |
|
|
|
|
await self._on_disconnect(msg_data) |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
logging.debug("Received sound file") |
|
|
|
|
|
|
|
|
|
async def coco_pulse(self): |
|
|
|
|
while True: |
|
|
|
|
logging.debug("Checking coco for new messages") |
|
|
|
|
await sleep(self.COCO_PULSE_TICK) |
|
|
|
|
if self.cococlient.is_connected: |
|
|
|
|
new_messages = self.cococlient.pulse() |
|
|
|
|
if isinstance(new_messages, list): |
|
|
|
|
for response_obj in new_messages: |
|
|
|
|
await self._dispatch_response(response_obj) |
|
|
|
|
elif isinstance(new_messages, AbstractResponse): |
|
|
|
|
await self._dispatch_response(new_messages) |
|
|
|
|
|
|
|
|
|
async def listen(self): |
|
|
|
|
if self.method == "https": |
|
|
|
|
socket_address = 'wss://%s/socket/%s' % (self.domain, self.channel) |
|
|
|
|
@ -73,25 +113,6 @@ class CoboBot:
|
|
|
|
|
async with websockets.connect(socket_address, |
|
|
|
|
extra_headers={"cookie": "id=%s" % self.cookie}) as websocket: |
|
|
|
|
self.socket = websocket |
|
|
|
|
while True: |
|
|
|
|
msg = await websocket.recv() |
|
|
|
|
websocket.recv() |
|
|
|
|
if type(msg) != bytes: |
|
|
|
|
msg_data = json.loads(msg, encoding="utf-8") |
|
|
|
|
msg_type = msg_data.get("type", "") |
|
|
|
|
if msg_type == "userlist": |
|
|
|
|
self.user_list = UserList(msg_data["users"]) |
|
|
|
|
logging.info(str(self.user_list)) |
|
|
|
|
|
|
|
|
|
elif msg_type == "msg": |
|
|
|
|
await self._on_message(msg_data) |
|
|
|
|
|
|
|
|
|
elif msg_type == "connect": |
|
|
|
|
await self._on_connect(msg_data) |
|
|
|
|
|
|
|
|
|
elif msg_type == "disconnect": |
|
|
|
|
await self._on_disconnect(msg_data) |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
logging.debug("Received sound file") |
|
|
|
|
await gather(self.socket_listener(), self.coco_pulse()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|