-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.py
44 lines (36 loc) · 912 Bytes
/
messages.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
from collections import deque
from const import *
class Message:
def __init__(self, msg, type):
self.type = type
self.text = msg
self.count = 1
class MessageLog:
def __init__(self, capacity):
self.msgs = deque(maxlen=capacity)
def add_message(self, msg, typ="neutral"):
if not msg:
return
if typ not in MSG_TYPES:
raise ValueError(f"invalid message type {typ!r}")
msg = str(msg)
combine = False
if self.msgs:
last = self.msgs[-1]
combine = last.type == typ and last.text == msg
if combine: #Combine similar messages
self.msgs[-1].count += 1
else:
self.msgs.append(Message(msg, typ))
def get_messages(self, num):
messages = []
for msg in reversed(self.msgs):
text = msg.text
if msg.count > 1:
text += f" (x{msg.count})"
messages.append((text, msg.type))
num -= 1
if num <= 0:
break
messages.reverse()
return messages