-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleChatLog.py
68 lines (54 loc) · 2.04 KB
/
ModuleChatLog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#chat log module
from Module import Module
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi_htmx import htmx, htmx_init
from EventHandler import EventHandler
from twitchAPI.chat import ChatMessage
import re as regex
class cmessage():
def __init__(self,
ident: str,
user: str,
text: str,
timestamp: int):
self.id = ident
self.user = user
self.text = text
self.timestamp = timestamp
def __str__(self):
return f"{self.user}: {self.text}"
messages: list[cmessage] = []
router = APIRouter()
htmx_init(templates = Jinja2Templates(directory="templates"))
def construct_messages():
out = []
for message in messages:
out.append([message.id, message.user, message.text])
if out == []:
return {"messages": [["", "", "No messages yet!"]]}
tofrontend = out[::-1]
if len(tofrontend) > 250:
tofrontend = tofrontend[:250]
return {"messages": tofrontend}
@router.get("/messages", response_class=HTMLResponse)
@htmx("chat-messages", "index", construct_messages)
async def get_messages(request: Request):
pass
class ChatLog(Module):
def __init__(self, eventHandler: EventHandler):
super().__init__("ChatLog" , eventHandler)
self.reg = "/[^a-z0-9]/gi"
self.event_Handler.loginfo(self.name, " module loaded")
#data: ChatMessage
async def on_message(self, data: ChatMessage):
text = regex.sub('[^0-9a-zA-Z:)(]+', ' ', data.text)
message = cmessage(data.id, data.user.name, text, data.sent_timestamp)
messages.append(message)
self.event_Handler.loginfo(self.name, f'{data.user.name}: {text}')
self.event_Handler.DBConn.AddMessage(message)
async def on_webfrontend_message(self, command):
if "Send_message" in command:
output = cmessage("0", "(tada)", command["Send_message"], 0)
messages.append(output)