def checker(): global last_ping global allow_to_post error_alert = False while True: try: ready = select.select([twitch_socket], [], [], 1) while True: if ready[0]: last_ping = time.time() del ready response = twitch_socket.recv(1024).decode('utf-8', 'replace') break else: ready = select.select([twitch_socket], [], [], 1) yield from asyncio.sleep(0.1) except socket.error as err: worker.crash_logger('2.1. Impossible to connect (main)', err) worker.reconnect(channel_to_connect, twitch_socket, 1) except Exception as ex: worker.crash_logger('3. Funny code page or strange connection', ex) continue if r'PING :tmi.twitch.tv' in response: twitch_socket.send('PONG :tmi.twitch.tv\r\n'.encode('utf-8')) print('>[{:%H:%M:%S}] ping sent\t\t\t\t'.format(datetime.datetime.now())) last_ping = time.time() else: try: username = response.split('!')[0][1:] message = ''.join(response.split(':')[2:]).replace('tps/', 'tps:/').replace('tp/', 'tp:/') current_channel = ''.join(response.split('#')[1].split(' ')[0]) print('[{}] {}: {}'.format(datetime.datetime.now().strftime('%H:%M:%S'), username, message).translate({ord('\n'): None})) error_alert = False except UnicodeError as uex: worker.crash_logger('4. Parsing went wrong', uex) continue except Exception as ex: worker.crash_logger('4.1. Parsing went _really wrong_', ex) if error_alert: exit(0) error_alert = True continue if r'!HAMMERTIME' or r'!HAMMERZEIT' in message: allow_to_post = True if not allow_to_post: message = '' # ######################################## ADMINISTRATION ################################################# for pattern in cfg.TESTER: if pattern in message: send_message(current_channel, 'hi') break for pattern in cfg.HALT: if pattern in message and ((username in admins) or (username == channel_to_connect[1:])): print( '[{2:%H:%M:%S}] HALT MODE ACTIVATED by {0} on {1} channel'.format(username, current_channel, datetime.datetime.now())) allow_to_post = False break for pattern in cfg.JOINER: if pattern in message: twitch_socket.send('JOIN {}\r\n'.format('#{}'.format(username)).encode('utf-8')) # subprocess.call('start /wait python3 bot.py {}'.format(username), shell=True) # Windows edition send_message(current_channel, 'joined #{}'.format(username)) break for pattern in cfg.LEAVER: if pattern in message and (username in admins or username == channel_to_connect[1:]): print('{}'.format('leaving, good bye!')) exit(0) break for pattern in cfg.ADD_ADMIN: if pattern in message and username in admins or username == channel_to_connect[1:]: admins.append(message.split(" ")[1]) for pattern in cfg.REMOVE_ADMIN: if pattern in message and username in admins or username == channel_to_connect[1:]: admins.remove(message.split(" ")[1]) # ############################################ INSTRUMENTS ################################################ for pattern in cfg.ROLLER: if pattern in message: if re.search(roll_pattern, response) is not None: send_message(current_channel, str(random.randint(0, int(re.search(roll_pattern, response).group(1))))) else: send_message(current_channel, str(random.choice(range(100)))) break for pattern in cfg.ROLL_JACK: if pattern in message: send_message(current_channel, '{}'.format(str(casino.call_the_roll(pattern, username)))) # ############################################## JOKES #################################################### for pattern in cfg.ALLAHU: if pattern in message: for x in range(12): send_message(current_channel, content_deviz[random.randint(0, 141)]) yield from asyncio.sleep(rate_of_fire) send_message(current_channel, 'dank') break for pattern in cfg.HUYKU: if pattern in message: i = random.randint(0, 24) * 4 for x in range(3): send_message(current_channel, str(content_huiku[i + x])) yield from asyncio.sleep(rate_of_fire) break for pattern in cfg.HAIKU: if pattern in message: i = random.randint(0, 24) * 4 for x in range(3): send_message(current_channel, str(content_haiku[i + x])) yield from asyncio.sleep(rate_of_fire) break
content_huiku = u.readlines() print('hu loaded') # u.close() with open(cfg.hai_file, mode='r', encoding='utf-8') as a: content_haiku = a.readlines() print('ha loaded') # a.close() with open(cfg.deviz_file, mode='r', encoding='utf-8') as d: content_deviz = d.readlines() print('de loaded (pun not intended)') # u.close() except Exception as exc: worker.crash_logger('1. No files? check hai(/uy)ku and deviz', exc) exit(0) print('-=connecting to {}=-'.format(channel_to_connect)) try: worker.connector(twitch_socket, channel_to_connect) except socket.error as e: worker.reconnect(channel_to_connect, twitch_socket, 1) except Exception as exc: worker.crash_logger('2. Impossible to connect to {}'.format(channel_to_connect), exc) exit(0) last_ping = time.time() loop = asyncio.get_event_loop()