async def favour_achieve(user_dict: dict, user: Union[discord.User, None], message: Any, favour_streak: dict) -> dict: """あいこ数に応じたアチーブメントメッセージを送信します Args: user_dict (dict): user_slvから取り出したdict user (user): discord.pyのuserモデル message (message): discord.pyのmessageモデル return: user_dict (dict): 更新済みuser_dict """ flag_bit = 0 favour_count = str(slv.get_dict_value(user_dict, 'janken', 'favour_count')) achieve_title = 'JANKEN_FAVOUR_' + favour_count achieve_dict = achievements.get(achieve_title) if favour_count == '1': flag_bit = await achieve.give(user, message, achieve_dict) if favour_count in ['10', '100']: flag_bit = await achieve.give(user, message, achieve_dict) elif favour_count == '1000': flag_bit = await achieve.give(user, message, achieve_dict) if favour_streak == 5: achieve_dict = achievements.get('FAVOUR_5_STRAIGHT') flag_bit |= await achieve.give(user, message, achieve_dict) elif favour_streak == 10: achieve_dict = achievements.get('FAVOUR_10_STRAIGHT') flag_bit |= await achieve.give(user, message, achieve_dict) else: return user_dict returned_tuple = utils.update_user_flag(user_dict, 'achieve', flag_bit, True) user_dict = returned_tuple[0] return user_dict
def get_user_flag(user_dict: dict, dict_key: str) -> int: """フラグ情報を取得します。 Args: user_dict (dict): user_slvから取り出したdict dict_key (str): shelveのdict_key Returns: int: ビットフラグ """ flag = slv.get_dict_value(user_dict, 'flags', dict_key) return int(flag)
def get_mode(user_dict: dict) -> str: """現在のモードをslvから取得します。 Args: user_dict (dict): user_slvから取り出したdict Returns: str: モード名 """ user_mode = slv.get_dict_value(user_dict, 'data', 'mode', 'normal') print('現在のモード:' + user_mode) return user_mode
def record_count(user_dict: dict, result: str) -> dict: """じゃんけんの履歴をslvに記録します。 Args: user_dict (dict): user_slvから取り出したdict result (str): じゃんけんの勝敗 Returns: user_dict (dict): 更新済みuser_dict """ key = result + '_count' result_count = slv.get_dict_value(user_dict, 'janken', key) if not result_count: result_count = 1 else: result_count = result_count + 1 slv.update_slv_dict(user_dict, 'janken', {key: result_count}) return user_dict
async def check_achieve(user_dict: dict, user: Union[discord.User, None], message: Any, result: Union[int, None]) -> dict: """勝敗に応じたアチーブメント処理を実行します。 Args: user_dict (dict): user_slvから取り出したdict user (user): discord.pyのuserモデル message (message): discord.pyのmessageモデル result (int): じゃんけん結果を表す整数 Returns: user_dict (dict): 更新済みuser_dict """ streak_counts = slv.get_dict_value(user_dict, 'janken', 'streak_counts', {}) if result in [1, -2]: winning_streak = streak_counts.get('winning_streak', 0) + 1 streak_count_dict = { 'winning_streak': winning_streak, 'losing_streak': 0, 'favour_streak': 0 } user_dict = await winning_achieve(user_dict, user, message, winning_streak) elif result in [-1, 2]: losing_streak = streak_counts.get('losing_streak', 0) + 1 streak_count_dict = { 'losing_streak': losing_streak, 'winning_streak': 0, 'favour_streak': 0 } user_dict = await losing_achieve(user_dict, user, message, losing_streak) else: favour_streak = streak_counts.get('favour_streak', 0) + 1 streak_count_dict = {'favour_streak': favour_streak} user_dict = await favour_achieve(user_dict, user, message, favour_streak) streak_counts.update(streak_count_dict) user_dict = slv.update_slv_dict(user_dict, 'janken', {'streak_counts': streak_counts}) return user_dict
def update_user_flag(user_dict: dict, dict_key: str, flag_bit: int, _bool: bool) -> Tuple[dict, int]: """フラグを更新します。 Args: user_dict (dict): user_slvから取り出したdict dict_key (str): shelveのdict_key flag_bit (int): ビットフラグ _bool (bool): 真偽値 Returns: user_dict (dict): 更新済みuser_dict int: ビットフラグ """ flag = int(slv.get_dict_value(user_dict, 'flags', dict_key)) if _bool: flag |= flag_bit else: flag -= flag & flag_bit user_dict = slv.update_slv_dict(user_dict, 'flags', {dict_key: flag}) return user_dict, int(flag)
async def on_message(message: Any): # botの発言を無視 if message.author.bot: return else: # 変数を定義 author = message.author user_name = utils.get_user_name(author) user_id = str(message.author.id) user_slv_path = slv.get_user_slv_path(user_id) now = utils.get_now() hiragana_content = utils.get_hiragana(message.content) utils.print_message_info(message) # slvに初期項目がなければ追記 user_dict = slv.initialize_user(author) user_dict = slv.get_dict(user_slv_path) if not user_dict else user_dict user_dict = slv.update_slv_dict(user_dict, 'data', {'name': user_name}) # モード情報を取得 user_mode = utils.get_mode(user_dict) # 20秒経過している場合normalへ遷移 last_act_at = slv.get_dict_value(user_dict, 'data', 'last_act_at') if isinstance(last_act_at, datetime.datetime): time_passed = now - last_act_at if time_passed > datetime.timedelta(0, 20) and user_mode != 'normal': print('20秒以上経過 -> normalモードへ遷移') user_dict = slv.update_slv_dict(user_dict, 'data', {'mode': 'normal'}) user_mode = 'normal' # チャンネルIDを照合 if str(message.channel.id) in BOT_CH_IDS: # デバッグコマンド debug_response = debug.command_check(user_dict, message) user_dict = debug_response['user_dict'] debug_content = debug_response['content'] if debug_content: return await utils.send_reply(message, debug_content) # じゃんけんモード if user_mode == 'janken': # じゃんけん処理 user_hands = janken.USER_HANDS if utils.check_command(hiragana_content, user_hands): user_dict = await janken.play(user_dict, message=message) # USER_HANDSと不一致 else: ignore_message = random.choice(janken_words.IGNORE_MES) await utils.send_reply(message, ignore_message) print('回答がJANKEN_HANDSと不一致') # 発言時刻記録 user_dict = slv.update_slv_dict(user_dict, 'data', {'last_act_at': now}) # 通常モード elif user_mode == 'normal': # じゃんけん起動 if bot_commands['JANKEN'] in hiragana_content: user_dict = await janken.start(user_dict, message) # おみくじ起動 elif bot_commands['OMIKUJI'] in hiragana_content: user_dict = await omikuji.play_omikuji(user_dict, message) # ヘルプコマンド elif re.search(bot_commands['HELP'], hiragana_content): await utils.send_command_help(message) # 鳴き声機能 elif bot_commands['NORMAL'] in hiragana_content: content = random.choice(random_contents) await utils.send_message(message.channel, content) print('message.channel.id が一致 -> 反応:' + content) # メンションされたとき elif message.mentions: if message.mentions[0].id == client.user.id: random_mes = random.choice(random_contents) hint_mes = bot_words.HINT_MESSAGE content = '{0}\n{1}'.format(random_mes, hint_mes) await utils.send_reply(message, content) # 未設定メッセージを受信時 else: print('未設定メッセージ -> 反応なし') user_dict['data']['updated_at'] = now slv.merge_dict(user_dict, user_slv_path) # チャンネルIDが不一致 elif str(message.channel.id) not in BOT_CH_IDS: return print('=======================================')