-
Notifications
You must be signed in to change notification settings - Fork 16
/
chat.py
80 lines (58 loc) · 2.59 KB
/
chat.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import platform
import re
from config import readFromConfig
class Chat(object):
def __init__(self):
self.enable = readFromConfig("Chat", "enable")
self.show_kicked = readFromConfig("Chat", "show_kicked")
self.show_message = readFromConfig("Chat", "show_message")
self.show_usernames = readFromConfig("Chat", "show_usernames")
self.show_userids = readFromConfig("Chat", "show_userids")
def showMessage(self, message):
if not self.enable:
return True
self.message = message
if self.message["type"] == "interaction":
preparedMessage = self.prepareMessage()
if platform.system() == "Windows":
finalMessage = self.fixMessage(preparedMessage)
else:
finalMessage = preparedMessage
print(finalMessage)
elif self.message["type"] == "kicked":
if self.show_kicked:
print(self.prepareKickedMessage())
return True
def prepareMessage(self):
toPrint = ""
if self.show_usernames or self.show_userids:
if self.show_usernames:
toPrint += str(self.message["metadata"]["username"])
if self.show_usernames and self.show_userids:
toPrint += " (" + str(self.message["metadata"]["userId"]) + ")"
elif self.show_userids:
toPrint += str(self.message["metadata"]["userId"])
if self.show_message and (self.show_usernames or self.show_userids):
toPrint += ": " + str(self.message["metadata"]["message"])
elif self.show_message:
toPrint += str(self.message["metadata"]["message"])
return toPrint
def fixMessage(self, message):
""" Microsoft Windows Command Line (CMD) are crashing because of incorrect characters.
This will remove all characters that are not letters and / or numbers
"""
return re.sub(r'\W+\(\)', '', message)
def prepareKickedMessage(self):
""" Game server informs all clients when somebody are kicked from chat
This will allow to observe who are kicked
"""
toPrint = ""
if self.show_usernames or self.show_userids:
if self.show_usernames:
toPrint += str(self.message["username"])
if self.show_usernames and self.show_userids:
toPrint += " (" + str(self.message["userId"]) + ")"
elif self.show_userids:
toPrint += str(self.message["userId"])
return toPrint + " are kicked from chat!"
return toPrint