-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhakcbot_run.py
255 lines (194 loc) · 8.19 KB
/
hakcbot_run.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/python3
import threading
import uvloop, asyncio
import requests
import time
import traceback
from config import * # pylint: disable=unused-wildcard-import
from socket import socket
from collections import deque, namedtuple
from hakcbot_init import Init
from hakcbot_spam import Spam
from hakcbot_execute import Execute
from hakcbot_commands import Commands
from hakcbot_accountage import AccountAge
from hakcbot_regex import fast_time, USER_TUPLE, ONE_MIN, FIVE_MIN, ANNOUNCEMENT_INTERVAL
from hakcbot_utilities import dynamic_looper, queue
from hakcbot_utilities import load_from_file, write_to_file, Log as L
class Hakcbot:
def __init__(self):
self.sock = socket()
self.Init = Init(self)
self.Automate = Automate(self)
self.Threads = Threads(self)
self.Spam = Spam(self)
self.Execute = Execute(self)
self.Commands = Commands(self)
self.AccountAge = AccountAge(self)
self.online = False
self.linecount = 0
self.last_message = 0
self.uptime_message = 'hakbot is still initializing! try again in a bit.'
self.announced_titles = {}
def start(self):
self.Threads.start()
self.AccountAge.start()
uvloop.install()
asyncio.run(self.main())
async def main(self):
await self.Init.initialize()
await asyncio.gather(self.hakc_general(), self.hakc_automation())
async def hakc_general(self):
L.l1('[+] Starting main bot process.')
sock, loop = self.sock, asyncio.get_running_loop()
while True:
try:
data = await loop.sock_recv(sock, 1024)
except OSError as E:
L.l3(f'MAIN SOCKET ERROR | ATTEMPING RECONNECT | {E}')
break
else:
if (not data):
L.l3('SOCKET CLOSED BY REMOTE SERVER | ATTEMPING RECONNECT')
break
await self._message_handler(data.decode('utf-8', 'ignore').strip())
# closing socket and recursively calling main to attempt reconnect
self.sock.close()
await self.main()
async def _message_handler(self, data):
loop, user = asyncio.get_running_loop(), None
if (data == 'PING :tmi.twitch.tv'):
await loop.sock_sendall(self.sock, 'PONG :tmi.twitch.tv\r\n'.encode('utf-8'))
elif ('PRIVMSG' in data):
valid_data = await self.Spam.pre_process(data)
if (not valid_data): return
self.linecount += 1
self.last_message = fast_time()
user, message = valid_data
# function will check if already in progress before sending to the queue
self.AccountAge.add_to_queue(user)
await self.Execute.task_handler(user, message)
# placeholder for when i want to track joins/ see if a user joins
elif ('JOIN' in data):
L.l3(data)
# probably a bunk ass message, not sure????? should protect higher tier titles for now.
else:
L.l3(f'else: {data}')
return
# T2 TITLES
if (not user): return
try:
titled_user = self.titles[user.name] # pylint: disable=no-member
except KeyError:
pass
else:
prior_announcement = self.announced_titles.get(user.name, None)
if titled_user['tier'] == 2 and not self.recently_announced(prior_announcement):
# already announced users - type > set()
self.announced_titles[user.name] = fast_time()
# announcing the user to chat
if (self.online):
await self.announce_title(user.name, titled_user['title'])
async def announce_title(self, user, title):
# message needs to be iterable for compatibility with commands.
message = [f'attention! {user}, the {title}, has spoken.']
await self.send_message(*message)
def recently_announced(self, prior_announcement):
if (not prior_announcement): return False
current_time = fast_time()
if (current_time - prior_announcement > ANNOUNCEMENT_INTERVAL):
return False
return True
async def send_message(self, *msgs):
loop = asyncio.get_running_loop()
for msg in msgs:
# ensuring empty returns to do not get sent over irc
if (not msg): continue
L.l2(msg)
await loop.sock_sendall(
self.sock, f'PRIVMSG #{CHANNEL} :{msg}\r\n'.encode('utf-8')
)
async def hakc_automation(self):
L.l1('[+] Starting automated command process.')
await asyncio.gather(
self.Automate.reset_line_count(),
self.Automate.timeout(), # pylint: disable=no-value-for-parameter
*[self.Automate.timers(k, v) for k,v in self.Commands._AUTOMATE.items()])
class Automate:
def __init__(self, Hakcbot):
self.Hakcbot = Hakcbot
self.hakcusr = USER_TUPLE(
'hakcbot', False, False, False,
False, True, fast_time()
)
# temp until we switch reference to queue object add function itself.
def flag_for_timeout(self, username):
self.timeout.add(username) # pylint: disable=no-member
@dynamic_looper(func_type='async')
async def reset_line_count(self):
time_elapsed = fast_time() - self.Hakcbot.last_message
if (time_elapsed > FIVE_MIN):
self.Hakcbot.linecount = 0
return FIVE_MIN
@dynamic_looper(func_type='async')
async def timers(self, cmd, timer):
if (self.Hakcbot.linecount >= 3):
getattr(self.Hakcbot.Commands, cmd)(usr=self.hakcusr)
return ONE_MIN * timer
@queue(name='timeout', func_type='async')
async def timeout(self, username):
message = f'/timeout {username} 3600 account age less than one day.'
response = f'{username}, you have been timed out for having an account age \
less that one day old. this is to prevent bot spam. if you are a human \
(i can tell from first message), i will remove the timeout when i see it, \
sorry!'
await self.Hakcbot.send_message(message, response)
class Threads:
def __init__(self, Hakcbot):
self.Hakcbot = Hakcbot
self._last_status = None
def start(self):
L.l1('[+] Starting bot threads.')
threading.Thread(target=self._uptime).start()
threading.Thread(target=self.file_task).start()
# temp until we switch reference to queue object add function itself.
def add_file_task(self, obj_name):
self.file_task.add(obj_name) # pylint: disable=no-member
@queue(name='file_task', func_type='thread')
def file_task(self, obj_name):
config = load_from_file('config')
if (obj_name not in config): return None
updated_attribute = getattr(self.Hakcbot, obj_name)
if isinstance(updated_attribute, set):
updated_attribute = list(updated_attribute)
config[obj_name] = updated_attribute
write_to_file(config, 'config')
@dynamic_looper(func_type='thread')
def _uptime(self):
try:
uptime = requests.get(f'https://decapi.me/twitch/uptime?channel={CHANNEL}')
except Exception:
self.Hakcbot.uptime_message = 'Hakcbot is currently being a dumb dumb. :/'
else:
uptime = uptime.text.strip('\n')
if (uptime == 'dowright is offline'):
self.Hakcbot.online = False
self.Hakcbot.uptime_message = 'DOWRIGHT is OFFLINE'
else:
self.Hakcbot.online = True
self.Hakcbot.uptime_message = f'DOWRIGHT has been live for {uptime}'
if (self._last_status != self.Hakcbot.online):
self._last_status = self.Hakcbot.online
# resetting tricho count and title announcements every stream
if (self.Hakcbot.online):
self.Hakcbot.Commands.tricho_count = 0
self.Hakcbot.announced_titles.clear()
return 90
def main():
Hb = Hakcbot()
Hb.start()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('\nExiting Hakcbot :(')