diff --git a/chatat/__main__.py b/chatat/__main__.py index 9833f4b2a0374c6a281a77d53e53227ef81780b8..263a2272f7efbb48b2296c4e3dff332dd0fbea36 100644 --- a/chatat/__main__.py +++ b/chatat/__main__.py @@ -14,6 +14,7 @@ class ChatInterface: self.pubpen.subscribe("message", self.show_message) self.pubpen.subscribe("new_char", self.show_typing) + self.pubpen.subscribe("switch_channel", self.switch_channel) self._buffer = "" @@ -21,6 +22,8 @@ class ChatInterface: TwitchHelixProtocol.session(auth, self.pubpen.loop) ) + self.channel = None + def __enter__(self): self.stdscr = curses.initscr() @@ -79,14 +82,20 @@ class ChatInterface: char = chr(await self.pubpen.loop.run_in_executor(None, self.stdscr.getch)) self.pubpen.publish("new_char", char) - def show_typing(self, char): + def show_typing(self, char: str): if char == "\n": - message = Message.from_simple( - Channel("btaskaya"), self.auth.username, self._buffer - ) + buffer = self._buffer + self.clear_typing() + if buffer.startswith(":"): + self._run_cmd(buffer[1:]) + return + + if self.channel is None: + return + + message = Message.from_simple(self.channel, self.auth.username, buffer) self.pubpen.publish("send", message) self.pubpen.publish("message", message) - self.clear_typing() return self.input_current_x += 1 @@ -100,6 +109,18 @@ class ChatInterface: self._buffer = "" self.input_buffer.refresh() + def switch_channel(self, channel: Channel): + self.channel = channel + + def _run_cmd(self, cmd: str): + cmd = cmd.split(" ") + if "switch" in cmd: + self.channel = Channel(cmd[1]) + message = Message.from_simple( + "<system>", "<system>", f"Channel switched to {self.channel}" + ) + self.pubpen.publish("message", message) + if __name__ == "__main__": loop = asyncio.get_event_loop() diff --git a/chatat/client.py b/chatat/client.py index 78203dedee65b04f1ff9ffc6c0e132a568e0bbb2..b1b19c14d530903a96f35c3b75d744cfeb3ce14e 100644 --- a/chatat/client.py +++ b/chatat/client.py @@ -61,6 +61,9 @@ class TwitchChatProtocol(_Protocol, asyncio.Protocol): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.pubpen.subscribe("send", self._send_to_channel) + self.pubpen.subscribe("switch_channel", self._switch_channel) + + self._active_channels = [] def _send_irc_command(self, cmd: str, value: str) -> None: request = f"{cmd.upper()} {value}\r\n" @@ -69,6 +72,11 @@ class TwitchChatProtocol(_Protocol, asyncio.Protocol): def _send_to_channel(self, message: Message) -> None: self._send_irc_command("privmsg", f"{message.channel} :{message.message}") + def _switch_channel(self, channel: Channel) -> None: + if not channel in self._active_channels: + self._send_irc_command("join", channel) + self._active_channels.append(channel) + def connection_made( self, transport: asyncio.transports.Transport ) -> None: # type: ignore @@ -81,6 +89,7 @@ class TwitchChatProtocol(_Protocol, asyncio.Protocol): self.logger.info(f"Connection made to {ip}:{port}") for channel in self.channels: self._send_irc_command("join", channel) + self._active_channels.append(channel) def data_received(self, raw_msg: bytes) -> None: data = raw_msg.decode()