def get_repeated_message(universal_id: str) -> str: """Get repeated message and add some variations on it.""" msg_base = load(universal_id, 'current_msg_base') left_increment = load(universal_id, 'current_left_increment') right_increment = load(universal_id, 'current_right_increment') combo = load(universal_id, 'current_combo') mutate_prob = load(universal_id, 'mutate_probability') if not mutate_prob: mutate_prob = 5 save(universal_id, 'mutate_probability', mutate_prob) cut_in_prob = load(universal_id, 'cut_in_probability') if not cut_in_prob: cut_in_prob = 5 save(universal_id, 'cut_in_probability', cut_in_prob) if random.randint(1, 100) <= cut_in_prob: # reset the combo counter here save(universal_id, 'current_combo', 0) return exclaim_msg('打断' * (msg_base[0:2] == '打断'), '4', True, 1) final = combo * left_increment + msg_base + combo * right_increment can_mutate = random.randint(1, 100) <= mutate_prob # add combo for self repetition if the message is not mutated save(universal_id, 'current_combo', (combo + 1) * (not can_mutate)) return mutate_msg(final, mutate=can_mutate)
async def show_id_battle(bot: Bot, event: Event, state: dict): """Handle the id_battle command.""" try: uids = map(int, str(event.message).split()) uid1 = next(uids) uid2 = next(uids) except Exception: await bot.send(event=event, message=exclaim_msg('', '3', False, 1)) return try: uid1_info = await get_user_info(uid1) uid2_info = await get_user_info(uid2) except Exception: logger.error(traceback.format_exc()) await bot.send(event=event, message='用户信息获取失败~') return battle_message = '[%d] vs [%d]\n' % (uid1, uid2) for v in ['beg', 'int', 'exp', 'total']: tdiff = uid1_info[f'record_{v}'][0] - uid2_info[f'record_{v}'][0] bdiff = uid1_info[f'record_{v}'][1] - uid2_info[f'record_{v}'][1] result = f'{v}: %+.3f | %+.3f' % (tdiff, bdiff) battle_message += (result + '\n') await bot.send(event=event, message=battle_message.strip())
async def show_autopvp(bot: Bot, event: Event, state: dict): """Handle the autopvp command.""" autopvp_result = await get_autopvp_info() await bot.send(event=event, message=format_pvp_info(autopvp_result)) if autopvp_result['latest_battle_winner'] != 'autopvp': await bot.send(event=event, message=exclaim_msg( autopvp_result['latest_battle_winner'], '1', False))
async def search(bot: Bot, event: Event, state: dict): """Analyze the result.""" search_nickname = str(event.message) try: search_result = await get_search_info(search_nickname) if len(search_result) == 0: await bot.send(event=event, message='未查询到符合条件的玩家~') else: await bot.send(event=event, message=format_search(search_result)) except Exception: await bot.send(event=event, message=exclaim_msg('', '3', False, 1))
async def show_randi(bot: Bot, event: Event, state: dict): """Generate random integers.""" args = str(event.message).split() try: begin = int(args[0]) end = int(args[1]) count = int(args[2]) extras = args[3:] result = get_randi(begin, end, count, extras) message = '随机结果: %s' % ' '.join(map(str, result)) await bot.send(event=event, message=message) except Exception: await bot.send(event=event, message=exclaim_msg('', '3', False, 1))
async def show_greet(bot: Bot, event: Event, state: dict): """Greet the person.""" current_time = datetime.datetime.now() current_hour = current_time.hour if current_hour not in [0, 23]: # offset current_hour += random.choice([-1, 0, 0, 0, 0, 0, 0, 0, 0, 1]) # add randomness time_tag = [ 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 19, 19 ] # a tag for time await bot.send(event=event, message=exclaim_msg('', str(time_tag[current_hour]), False, 1))
async def autopvp_broadcast(bot_id: str, group_id: str): """Scheduled dailymap broadcast.""" autopvp_result = await get_autopvp_info() message = [format_pvp_info(autopvp_result)] if autopvp_result['latest_battle_winner'] != 'autopvp': message.append( exclaim_msg(autopvp_result['latest_battle_winner'], '1', False)) bot = nonebot.get_bots()[bot_id] try: for msg in message: await bot.send_group_msg(group_id=int(group_id), message=msg) except Exception: logger.error(traceback.format_exc())
async def show_dailystar_count(bot: Bot, event: Event, state: dict): """Handle the dailystar_count command.""" try: uid = str(int(str(event.message).strip())) except Exception: await bot.send(event=event, message=exclaim_msg('', '3', False, 1)) return dailystar_count = _load_daily_star(uid) if len(dailystar_count) > 0: message = '用户[%s]在联萌的每日一星次数: %d, 最近%d次获得时间为: %s' % ( uid, len(dailystar_count), min(len(dailystar_count), 5), ', '.join( dailystar_count[-1:-6:-1])) await bot.send(event=event, message=message) else: await bot.send(event=event, message='该用户尚未获得每日一星, 请继续努力~')
async def enter_black_room(bot: Bot, event: Event, state: dict): """Handle the blackroom command.""" group_id = event.group_id user_id = event.user_id try: duration = int(str(event.message).strip()) except Exception: await bot.send(event=event, message=exclaim_msg('', '3', False, 1)) return # convert the duration in seconds duration = duration * 60 if 1 <= duration <= 43200 else 600 try: await bot.set_group_ban(group_id=group_id, user_id=user_id, duration=duration) except Exception: await bot.send(event=event, message='权限不足,无法使用小黑屋~')
async def analyze(bot: Bot, event: Event, state: dict): """Analyze the result.""" args = str(event.message).split() id_type = { 'record': False, 'r': False, '录像': False, 'post': True, 'p': True, '帖子': True } try: use_post_id = id_type[args[0]] _id = int(args[1]) record_info = await get_record(_id, use_post_id) await bot.send(event=event, message=format_record(record_info)) except TypeError: # indicates the message can't be found from the remote server await bot.send(event=event, message='无法查询到录像信息') except Exception: logger.error(traceback.format_exc()) await bot.send(event=event, message=exclaim_msg('', '3', False, 1))
async def show_id_info(bot: Bot, event: Event, state: dict): """Handle the id_info command.""" try: uid = int(str(event.message).strip()) except Exception: await bot.send(event=event, message=exclaim_msg('', '3', False, 1)) return universal_id = str(event.self_id) + str(event.group_id) colding_time = _validate_id(universal_id, str(uid), gap=3600) if colding_time > 0: wait_message = '用户[%d]尚在查询冷却期,剩余时间%d秒~' % (uid, colding_time) await bot.send(event=event, message=wait_message) return try: user_info = await get_user_info(uid) except Exception: await bot.send(event=event, message='用户信息获取失败~') logger.error(traceback.format_exc()) return user_info_message = format_user_info(user_info) await bot.send(event=event, message=user_info_message)
async def show_cheer_ending(bot: Bot, event: Event, state: dict): """Cheer the person with the ending.""" if str(event.message).strip()[-2:] != '加油': return # recheck the message person = str(event.message).strip()[:-2] await bot.send(event=event, message=exclaim_msg(person, '2', True))
async def show_cheer(bot: Bot, event: Event, state: dict): """Cheer the person.""" person = str(event.message).strip() await bot.send(event=event, message=exclaim_msg(person, '2', True))
async def show_admire(bot: Bot, event: Event, state: dict): """Admire(Praise * 2) the person.""" person = str(event.message).strip() message = exclaim_msg(person, '1', False) await bot.send(event=event, message=message) await bot.send(event=event, message=message)
async def show_praise(bot: Bot, event: Event, state: dict): """Praise the person.""" person = str(event.message).strip() await bot.send(event=event, message=exclaim_msg(person, '1', True))