This repository has been 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
/
ChatEventHandler.py
104 lines (89 loc) · 3.79 KB
/
ChatEventHandler.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
''' Handles and formats chat events '''
class Handler():
'''handles 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):
'''
checks the event type and calls the function
relating to that event type
'''
func = self.event_types[data['type']]
func(data)
if self.config.CHATDEBUG:
print(data)
def type_reply(self, data):
'''Handles 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('Server Reply: {}'.format(str(data)))
else:
print('Server Reply: {}'.format(str(data['error'])))
def type_event(self, data):
'''handles the reply chat event types'''
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'}
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'] == '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'] == '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):
'''handles 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('METHOD MSG: {}'.format(str(data)))
else:
print('METHOD MSG: {}'.format(str(data)))
def type_system(self, data):
'''handles the reply chat event types'''
if self.config.CHATDEBUG:
print('SYSTEM MSG: {}'.format(str(data['data'])))