Beispiel #1
0
async def start(user_dict: dict, message: Any) -> dict:
    """jankenを開始します。

    Args:
        user_dict (dict): user_slvから取り出したdict
        message (message): discord.pyのmessageモデル

    Returns:
        user_dict (dict): 更新済みuser_dict
    """
    print('========== じゃんけんを開始 ===========')
    now = utils.get_now()
    START_MES = janken_words.START_MES
    message_id = message.id
    # モード切替
    new_data_dict = {'mode': 'janken', 'last_act_at': now}
    user_dict = slv.update_slv_dict(user_dict, 'data', new_data_dict)
    # メッセージ送信
    content = random.choice(START_MES)
    emoji_hands = list(EMOJI_HANDS.values())
    reply_message = await utils.send_reply(message, content)
    print(content)
    # リアクション送信
    await utils.add_reaction_list(reply_message, emoji_hands)
    print('じゃんけん用リアクション送信')
    new_janken_dict = {
        'last_message_id': reply_message.id,
        'start_mes_id': message_id
    }
    user_dict = slv.update_slv_dict(user_dict, 'janken', new_janken_dict)
    return user_dict
Beispiel #2
0
def get_result_mes(user_dict: dict, janken_mes: list,
                   result: str) -> Tuple[dict, str]:
    """じゃんけんの結果に応じたメッセージを取得します。

    Args:
        user_dict (dict): user_slvから取り出したdict
        janken_mes (list): 結果に応じたlist
        result (str): 勝敗

    Returns:
        user_dict (dict): 更新済みuser_dict
        str: じゃんけん結果メッセージ
    """
    result_mes = random.choice(janken_mes)
    # 勝利, 敗北処理
    if janken_mes != favour_mes:
        print('じゃんけんの結果:botの' + result)
        user_dict = slv.update_slv_dict(user_dict, 'data', {'mode': 'normal'})
    # あいこ処理
    else:
        print('結果:' + result)
        now = utils.get_now()
        user_dict = slv.update_slv_dict(user_dict, 'data',
                                        {'last_act_at': now})
    return user_dict, result_mes
Beispiel #3
0
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
Beispiel #4
0
async def send_favour_mes(user_dict: dict, reply_message: Any) -> dict:
    """あいこになった際のメッセージを送信します。

    Args:
        user_dict (dict): user_slvから取り出したdict
        reply_message (reply): discord.pyのreplyモデル

    Returns:
        user_dict (dict): 更新済みuser_dict
    """
    emoji_hands = list(EMOJI_HANDS.values())
    user_dict = slv.update_slv_dict(user_dict, 'janken',
                                    {'last_message_id': reply_message.id})
    await utils.add_reaction_list(reply_message, emoji_hands)
    return user_dict
Beispiel #5
0
async def on_reaction_add(reaction, user):

    user_id = str(user.id)
    now = utils.get_now()
    if user.bot:
        return
    user_slv_path = slv.get_user_slv_path(user.id)
    user_dict = slv.get_dict(user_slv_path)
    user_mode = utils.get_mode(user_dict)
    user_slv = slv.get_user_slv_path(user_id)
    if user_mode == 'janken':
        last_message_id = slv.get_value(user_slv, 'janken', 'last_message_id')
        if str(last_message_id) == str(reaction.message.id):
            user_dict = await janken.play(user_dict,
                                          user=user,
                                          reaction=reaction)
    user_dict = slv.update_slv_dict(user_dict, 'data', {'updated_at': now})
    slv.merge_dict(user_dict, user_slv_path)
Beispiel #6
0
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
Beispiel #7
0
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)
Beispiel #8
0
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('=======================================')