def get_printed_input(msg: str) -> Optional[str]: msg_match = MSG_RE.match(msg) if msg_match: info = parse_badge_info(msg_match['info']) if info['color']: r, g, b = _parse_color(info['color']) else: r, g, b = _gen_color(info['display-name']) color_start = f'\033[1m\033[38;2;{r};{g};{b}m' if msg_match['msg'].startswith('\x01ACTION '): return (f'{dt_str()}' f'{_badges(info["badges"])}' f'{color_start}\033[3m * {info["display-name"]}\033[22m ' f'{msg_match["msg"][8:-1]}\033[m') else: if info.get('msg-id') == 'highlighted-message': msg_s = f'\033[48;2;117;094;188m{msg_match["msg"]}\033[m' elif 'custom-reward-id' in info: msg_s = f'\033[48;2;029;091;130m{msg_match["msg"]}\033[m' else: msg_s = msg_match['msg'] return (f'{dt_str()}' f'{_badges(info["badges"])}' f'<{color_start}{info["display-name"]}\033[m> ' f'{msg_s}') return None
async def vim_bits_handler(config: Config, match: Match[str]) -> str: info = parse_badge_info(match['info']) async with aiosqlite.connect('db.db') as db: await ensure_vim_tables_exist(db) enabled = await get_enabled(db) bits = int(info['bits']) if enabled: time_left = await add_bits(db, match['user'], bits) else: time_left = await add_bits_off(db, match['user'], bits) if enabled: await _set_symlink(should_be_vim=True) return format_msg( match, f'MOAR VIM: {seconds_to_readable(time_left)} remaining', ) else: return format_msg( match, f'vim is currently disabled ' f'{seconds_to_readable(time_left)} banked', )
def get_handler(msg: str) -> tuple[Callback, Match[str]] | None: msg_match = MSG_RE.match(msg) if msg_match: info = parse_badge_info(msg_match['info']) if 'custom-reward-id' in info: if info['custom-reward-id'] in POINTS_HANDLERS: return POINTS_HANDLERS[info['custom-reward-id']], msg_match else: return None if 'bits' in info: bits_n = int(info['bits']) if bits_n % 100 in BITS_HANDLERS: return BITS_HANDLERS[bits_n % 100], msg_match cmd_match = COMMAND_RE.match(msg_match['msg']) if cmd_match: command = f'!{cmd_match["cmd"].lstrip("!").lower()}' if command in COMMANDS: return COMMANDS[command], msg_match for pattern, handler in HANDLERS: match = pattern.match(msg) if match: return handler, match return None
def get_handler(msg: str) -> Optional[Tuple[Callback, Match[str]]]: msg_match = MSG_RE.match(msg) if msg_match: info = parse_badge_info(msg_match['info']) if 'custom-reward-id' in info: if info['custom-reward-id'] in POINTS_HANDLERS: return POINTS_HANDLERS[info['custom-reward-id']], msg_match else: return None cmd_match = COMMAND_RE.match(msg_match['msg']) if cmd_match and cmd_match['cmd'].lower() in COMMANDS: return COMMANDS[cmd_match['cmd'].lower()], msg_match for pattern, handler in HANDLERS: match = pattern.match(msg) if match: return handler, match return None
async def get_printed_input( config: Config, msg: str, *, images: bool, ) -> Optional[Tuple[str, str]]: msg_match = MSG_RE.match(msg) if msg_match: info = parse_badge_info(msg_match['info']) if info['color']: r, g, b = _parse_color(info['color']) else: r, g, b = _gen_color(info['display-name']) color_start = f'\033[1m\033[38;2;{r};{g};{b}m' msg_s = msg_match['msg'] is_action = msg_s.startswith('\x01ACTION ') if is_action: msg_s = msg_s[8:-1] badges_s = badges_plain_text(info['badges']) if images: badges = parse_badges(info['badges']) await download_all_badges( badges, channel=config.channel, oauth_token=config.oauth_token_token, client_id=config.client_id, ) badges_s_images = badges_images(badges) else: badges_s_images = badges_s if images: emote_info = parse_emote_info(info['emotes']) await download_all_emotes(emote_info) msg_s_images = replace_emotes(msg_s, emote_info) else: msg_s_images = msg_s if is_action: fmt = (f'{dt_str()}' f'{{badges}}' f'{color_start}\033[3m * {info["display-name"]}\033[22m ' f'{{msg}}\033[m') elif info.get('msg-id') == 'highlighted-message': fmt = (f'{dt_str()}' f'{{badges}}' f'<{color_start}{info["display-name"]}\033[m> ' f'\033[48;2;117;094;188m{{msg}}\033[m') elif 'custom-reward-id' in info: fmt = (f'{dt_str()}' f'{{badges}}' f'<{color_start}{info["display-name"]}\033[m> ' f'\033[48;2;029;091;130m{{msg}}\033[m') else: fmt = (f'{dt_str()}' f'{{badges}}' f'<{color_start}{info["display-name"]}\033[m> ' f'{{msg}}') to_print = fmt.format(badges=badges_s_images, msg=msg_s_images) to_log = fmt.format(badges=badges_s, msg=msg_s) return to_print, to_log return None