Kaydet (Commit) 2aa8fd00 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

channel switching

üst b99ca815
......@@ -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()
......
......@@ -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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment