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

Macros

üst bceba2c2
......@@ -5,6 +5,7 @@ from pubmarine import PubPen
from chatat.client import TwitchChatProtocol, TwitchHelixProtocol
from chatat.twitch import Auth, Channel, Message
from chatat.macros import Macros
class ChatInterface:
......@@ -15,6 +16,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.pubpen.subscribe("outgoing", self.outgoing)
self._buffer = ""
......@@ -23,6 +25,8 @@ class ChatInterface:
)
self.channel = None
self.macros = Macros(pubpen)
self.pubpen.subscribe("message", self.macros.dispatch)
def __enter__(self):
self.stdscr = curses.initscr()
......@@ -93,9 +97,7 @@ class ChatInterface:
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.pubpen.publish("outgoing", buffer)
return
self.input_current_x += 1
......@@ -139,6 +141,11 @@ class ChatInterface:
if message:
self.pubpen.publish("message", message)
def outgoing(self, buffer: str):
message = Message.from_simple(self.channel, self.auth.username, buffer)
self.pubpen.publish("send", message)
self.pubpen.publish("message", message)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
......
from collections import defaultdict
from chatat.twitch import Actions
class Macros:
macros = defaultdict(list)
def __init__(self, pubpen):
self.pubpen = pubpen
def dispatch(self, message):
for macro in self.macros[message.action]:
macro(self.pubpen, message)
@classmethod
def macro(cls, on_event):
def wrapper(func):
cls.macros[on_event].append(func)
return func
return wrapper
@Macros.macro(Actions.PRIVMSG)
def on_gnu(publisher, message):
lower_message = message.message.lower()
author = message.author
if "linux" in lower_message and "gnu" not in lower_message:
publisher.publish("outgoing", f"{author} dude; not linux, GNU/Linux")
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