def bot_manager_loop(): sleep_time = int(config['Twitch']['channel_check_sleep']) error_sleep_time = int(config['Twitch']['twitch_error_sleep']) while True: channel_list = get_prismata_streams() if channel_list is None: sleep(error_sleep_time) continue reset_bot_lifetimes(channel_list) channel_set = set(channel_list) existing_bots_set = set(processes.keys()) new_channels = channel_set - existing_bots_set ended_channels = existing_bots_set - channel_set if len(new_channels) > 0 or len(ended_channels) > 0: log.debug("Existing channels: {} - Existing bots: {}".format( channel_set, existing_bots_set)) for new_channel in new_channels: create_new_bot(new_channel) for ended_channel in ended_channels: decrement_bot_lifetime(ended_channel) sleep(sleep_time)
def reset_bot_lifetimes(channel_list): for channel in channel_list: if channel in bot_lifetimes: log.debug( "Bot for channel {} was on lifetime {} but was saved".format( channel, bot_lifetimes[channel])) del bot_lifetimes[channel]
def get_substring_match(query): for key in unit_lowercase_to_name.keys(): if query in key: unit_name = unit_lowercase_to_name[key] log.debug('Substring match: {}->{}->{}'.format(query, key, unit_name)) return unit_name return None
def pixel_equals_colour(position_name): coordinates = config['Screen positions'][position_name] coordinates = tuple(map(int, coordinates.split(','))) colours = PrismataController.pixel_colour(coordinates) expected_colours = config['Colours'][position_name] expected_colours = tuple(map(int, expected_colours.split(','))) log.debug('{}: expected {} got {}'.format(position_name, colours, expected_colours)) return colours == expected_colours
def decrement_bot_lifetime(channel): if channel not in bot_lifetimes: bot_lifetimes[channel] = 5 else: bot_lifetimes[channel] -= 1 log.debug("Bot for channel {} is on lifetime {}".format( channel, bot_lifetimes[channel])) if bot_lifetimes[channel] <= 0: terminate_bot(channel)
def get_difflib_match(query): unit_alias = get_close_matches(query, unit_aliases_to_name.keys(), 1, 0.4) unit_name = None if unit_alias: unit_alias = unit_alias[0] unit_name = unit_aliases_to_name[unit_alias] log.debug('Closest match: {}->{}->{} with {}' .format(query, unit_alias, unit_name, round(SequenceMatcher(None, unit_alias, query).ratio(), 2))) return unit_name
def get_unit_match(query): query_lower = query.lower() match = None if query_lower in unit_lowercase_to_name: unit_name = unit_lowercase_to_name[query_lower] log.debug('Direct match: {}->{}'.format(query, unit_name)) return unit_name if len(query_lower) >= 4: match = get_substring_match(query_lower) if match is None: match = get_difflib_match(query_lower) return match
def answer_unit_command(self, query): unit_match = get_unit_match(query) if unit_match: emote = '' if 'anime' in query: emote = ' TehePelo' elif 'goose' in query: emote = ' DuckerZ' self.chat('{}: {}{}'.format(unit_match, tooltips[unit_match], emote)) else: log.debug('Not found') self.chat("Couldn't find a unit for {} NotLikeThis".format(query))
def answer_prismata_command(self, query): tooltip_key = get_close_matches(query, prismata_responses_keys, 1, 0.55) if tooltip_key: tooltip_key = tooltip_key[0] log.debug('Closest match: {}->{} with {}' .format(query, tooltip_key, round(SequenceMatcher(None, query, tooltip_key).ratio(), 2))) if tooltip_key == 'commands': self.chat("I can talk about: {}".format(prismata_displayed_commands)) else: self.chat(prismata_responses[tooltip_key]) else: log.debug('Not found') self.chat("I don't have anything to say about that FrankerZ Try one of these: basics, f2p, p2w, rng, defense, sets. `!prismata commands` for the full list of topics")
def process_commands(): while True: command = command_queue.get() log.debug("Processing command: {} {}".format(command.method, command.arguments)) method = command.method if method == 'press': PrismataController.press(command.arguments) elif method == 'hotkey': modifier, key = command.arguments PrismataController.hotkey(modifier, key) elif method == 'click': column, horizontal = command.arguments PrismataController.click(column, horizontal) elif method == 'emote': PrismataController.emote(command.arguments)
def click_position(position_name): coordinates = config['Screen positions'][position_name] x, y = coordinates.split(',') log.debug('clicking {}'.format(position_name)) pyautogui.click(x=int(x), y=int(y))
def on_join(self, connection, event): log.debug('Joined channel ' + event.target)
def on_welcome(self, connection, event): log.debug('Connected (channel {})'.format(self.channel)) connection.cap('REQ', ':twitch.tv/tags') # Request display-names in messages connection.cap('REQ', ':twitch.tv/commands') # Request ROOMSTATE and CLEARCHAT updates connection.join(self.channel)
def on_disconnect(self, connection, event): log.info('Disconnected (channel {})'.format(self.channel)) log.debug(event)