This repository was archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathChatEventHandler.py
More file actions
136 lines (117 loc) · 5.18 KB
/
ChatEventHandler.py
File metadata and controls
136 lines (117 loc) · 5.18 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Handles and formats chat events."""
import requests
class Handler():
"""Handle chat events."""
def __init__(self, config, chat):
self.config = config
self.event_types = {
"reply": self.type_reply, "event": self.type_event,
"method": self.type_method, "system": self.type_system}
self.poll_switch = True
self.chat = chat
def formatting(self, data):
"""Check the event type and call the function relating to that type."""
func = self.event_types[data["type"]]
func(data)
if self.config.CHATDEBUG:
print(data)
def type_reply(self, data):
"""Handle the Reply type data."""
if "data" in data:
if "authenticated" in data["data"]:
if data["data"]["authenticated"]:
print("Authenticated with the server")
else:
print("Authenticated Failed, Chat log restricted")
else:
print(f"Server Reply: {str(data)}")
else:
print(f"Server Reply: {str(data['error'])}")
def type_event(self, data):
"""Handle the reply chat event types."""
s = requests.Session()
s.headers.update({"Client-ID": self.config.CLIENTID})
event_string = {
"WelcomeEvent": "Connected to the channel chat...",
"UserJoin": "{} has joined the channel.",
"UserLeave": "{} has left the channel.",
"ChatMessage": "{user}: {msg}",
"whisper": "{user} → {target} : {msg}",
"me": "{user} {msg}",
"PollStart": "{} has started a poll.",
"PollEnd": "The poll started by {} has ended.",
"ClearMessages": "{} has cleared chat.",
"PurgeMessage": "{modname} has purged {username}'s messages.",
"SkillAttribution": "{user} used the {skill} skill for {sparks}.",
"DeleteMessage": "{Mod} deleted a message."}
if data["event"] == "WelcomeEvent":
print(event_string[data["event"]])
elif data["event"] == "UserJoin" or data["event"] == "UserLeave":
if data["data"]["username"] is not None:
print(event_string[data["event"]].format(
data["data"]["username"]))
elif data["event"] == "PurgeMessage":
users_response = s.get("https://mixer.com/api/v1/users/{}".format(
data["data"]["user_id"])).json()["username"]
USERNAME = users_response
if "moderator" in data["data"]:
mod = data["data"]["moderator"]["user_name"]
print(f"{mod} has purged {USERNAME}'s messages.")
else:
pass
elif data["event"] == "DeleteMessage":
# pass
print(event_string[data["event"]].format(
Mod=data["data"]["moderator"]["user_name"]))
elif data["event"] == "ClearMessages":
print(event_string[data["event"]].format(
data["data"]["clearer"]["user_name"]))
elif data["event"] == "PollStart":
if self.poll_switch:
print(event_string[data["event"]].format(
data["data"]["author"]["user_name"]))
self.poll_switch = False
elif data["event"] == "PollEnd":
print(event_string[data["event"]].format(
data["data"]["author"]["user_name"]))
self.poll_switch = True
elif data["event"] == "SkillAttribution":
user = data["data"]["user_name"]
skill = data["data"]["skill"]["skill_name"]
sparks = data["data"]["skill"]["cost"]
print(event_string["SkillAttribution"].format(
user=user,
skill=skill,
sparks=sparks))
elif data["event"] == "ChatMessage":
msg = "".join(
item["text"] for item in data["data"]["message"]["message"])
if "whisper" in data["data"]["message"]["meta"]:
print(event_string["whisper"].format(
user=data["data"]["user_name"],
target=data["data"]["target"],
msg=msg))
elif "me" in data["data"]["message"]["meta"]:
print(event_string["me"].format(
user=data["data"]["user_name"],
msg=msg))
else:
print(event_string[data["event"]].format(
user=data["data"]["user_name"],
msg=msg))
if msg == "!ping":
self.chat.message("Its ping pong time")
def type_method(self, data):
"""Handle the reply chat event types."""
if self.config.CHATDEBUG:
if data["method"] == "auth":
print("Authenticating with the server...")
elif data["method"] == "msg":
if self.config.CHATDEBUG:
print(f"METHOD MSG: {str(data)}")
else:
print(f"METHOD MSG: {str(data)}")
def type_system(self, data):
"""Handle the reply chat event types."""
if self.config.CHATDEBUG:
print(f"SYSTEM MSG: {str(data['data'])}")