コード例 #1
0
def start_reg(msg):
    try:
        players_id[msg.chat.id]
    except KeyError:
        try:
            bot.send_message(
                msg.from_user.id, 'You`re creator of game in chat ' +
                str(msg.chat.id) + '\n Only you can '
                'launch the game')
        except telebot.apihelper.ApiException:
            bot_send_message(
                msg.chat.id, 'To be able to register in game, '
                'say /start to @Avalon117bot in private messages '
                'and push registration button again')
            return
        try:
            players_id[msg.chat.id] = tools.GameInfo('reg', msg.from_user.id,
                                                     dict(), msg)
            keyboard = telebot.types.InlineKeyboardMarkup()
            add_button = telebot.types.InlineKeyboardButton(
                text="Register", callback_data="register")
            keyboard.add(add_button)
            del_m = bot.reply_to(msg,
                                 'Registration is on\nPlayers in game:',
                                 reply_markup=keyboard)
            players_id[msg.chat.id].del_msg.append(del_m.message_id)
            return
        except:
            return
    bot.reply_to(
        msg,
        ('Game' if players_id[msg.chat.id].state == 'game' else 'Registration')
        + ' is on!')
コード例 #2
0
def say_welcome(message):
    logger.info(f'</code>@{message.from_user.username}<code> ({message.chat.id}) used /start or /help')
    bot.send_message(
        message.chat.id,
        '<b>Hello! This is a telegram bot template written by <a href="https://github.com/otter18">otter18</a></b>',
        parse_mode='html'
    )
コード例 #3
0
def say_welcome(message):
    logger.info(
        f'</code>@{message.from_user.username}<code> ({message.chat.id}) used /start or /help'
    )
    bot.send_message(
        message.chat.id, '<b>Коверкает слова. '
        'При добавлении в группу рандомно реагирует на сообщения с негативным смысловым окрасом.</b>',
        parse_mode='html')
コード例 #4
0
ファイル: bot.py プロジェクト: carexoid/telebots
def add_roles(msg):
    try:

        if players_id[msg.chat.id].state == 'game':
            bot.reply_to(
                msg, languages[msg.chat.id]["The game has already started"])
        else:
            if msg.from_user.id != players_id[msg.chat.id].creator:
                bot_send_message(
                    msg.chat.id,
                    languages[msg.chat.id]["The game has already started"])
                return
            bot.send_message(
                msg.chat.id, languages[msg.chat.id]
                ["Morgana, a minion of Mordred, deceives Percival. During the beginning round where "
                 "Merlin puts his "
                 "thumb in the air to reveal himself to Percival, Morgana also raises her own thumb, "
                 "confusing Percival as to which 'Merlin' to trust."] +
                "\n\n" + languages[msg.chat.id]
                ["Oberon despite being a servant of Mordred, is somewhat of "
                 "a hindrance. Oberon does not reveal himself to evil players, and also they do not reveal "
                 "themselves to him. Oberon still raises his thumb for Merlin to see, however."]
                + "\n\n" + languages[msg.chat.id]
                ["Mordred helps the evil side because Merlin does not know his identity. As Mordred, "
                 "it is imperative that you recognize your unique situation because you "
                 "will be able to watch the votes to see how everyone votes for teams to which the other "
                 "minions belong."])
            keyboard = telebot.types.InlineKeyboardMarkup()
            morgana_button = telebot.types.InlineKeyboardButton(
                text=languages[msg.chat.id]["Morgana"],
                callback_data="Morgana")
            mordred_button = telebot.types.InlineKeyboardButton(
                text=languages[msg.chat.id]["Mordred"],
                callback_data="Mordred")
            oberon_button = telebot.types.InlineKeyboardButton(
                text=languages[msg.chat.id]["Oberon"], callback_data="Oberon")
            lady_button = telebot.types.InlineKeyboardButton(
                text=languages[msg.chat.id]["Lady of the Lake"],
                callback_data="Lady of the Lake")
            keyboard.add(mordred_button, morgana_button, oberon_button,
                         lady_button)
            msg = bot_send_message(
                msg.chat.id,
                languages[msg.chat.id]["What role do you want to add?"],
                reply_markup=keyboard)
            players_id[msg.chat.id].del_msg.append(msg.message_id)
            players_id[msg.chat.id].additional_id = msg.message_id
    except KeyError:
        try:
            bot.reply_to(
                msg, languages[msg.chat.id]['No registration started!'] +
                '\n' + languages[msg.chat.id]['Run /start_registration'])
            return
        except KeyError:
            bot.reply_to(
                msg,
                'No registration started!' + '\n' + 'Run /start_registration')
            return
コード例 #5
0
def echo(message):
    for t, resp in dialog.items():
        if sum([e in message.text.lower() for e in resp['in']]):
            logger.info(f'</code>@{message.from_user.username}<code> ({message.chat.id}) used {t}:\n\n%s', message.text)
            bot.send_message(message.chat.id, random.choice(resp['out']))
            return

    logger.info(f'</code>@{message.from_user.username}<code> ({message.chat.id}) used echo:\n\n%s', message.text)
    bot.send_message(message.chat.id, message.text)
コード例 #6
0
ファイル: bot.py プロジェクト: carexoid/telebots
def start_reg(msg):
    try:
        players_id[msg.chat.id]
    except KeyError:
        players_id[msg.chat.id] = tools.GameInfo('reg', msg.from_user.id,
                                                 dict(), msg)

        try:
            bot.send_message(
                msg.from_user.id,
                languages[msg.chat.id]['You`re creator of game in chat '] +
                str(msg.chat.id) + '\n' +
                languages[msg.chat.id]['Only you can launch the game'])
        except telebot.apihelper.ApiException:
            bot_send_message(
                msg.chat.id, languages[msg.chat.id][
                    'To be able to register in game, '
                    'say /start to @Avalon117bot in private messages '
                    'and push registration button again'])
            return
        try:
            keyboard = telebot.types.InlineKeyboardMarkup()
            add_button = telebot.types.InlineKeyboardButton(
                text=languages[msg.chat.id]["Register"],
                callback_data="register")
            keyboard.add(add_button)
            del_m = bot.reply_to(msg,
                                 languages[msg.chat.id]['Registration is on'] +
                                 '\n' +
                                 languages[msg.chat.id]['Players in game:'],
                                 reply_markup=keyboard)
            players_id[msg.chat.id].del_msg.append(del_m.message_id)
            return
        except:
            print("govna")
            return
    bot.reply_to(
        msg, (languages[msg.chat.id]['Game'] if players_id[msg.chat.id].state
              == 'game' else languages[msg.chat.id]['Registration']) +
        languages[msg.chat.id][' is on!'])
コード例 #7
0
def callback_inline(call):
    chat_id = int(call.message.chat.id)
    try:
        if call.message:
            if call.data[0] == 'a' and call.data[1] == '@':
                nickname = call.data.split()[0][1:len(call.data)]
                in_chat = int(call.data.split()[1])
                # players_nick_to_id = players_id[chat_id].players_nick_to_id
                role = players_id[in_chat].players[
                    players_id[in_chat].players_nick_to_id[nickname]]
                # bot_send_message(chat_id, nickname + " was " + role)
                gameplay.endgame(
                    in_chat, players_id[in_chat],
                    players_id[in_chat].players_nick_to_id[nickname])
                if role == 'Merlin':
                    bot_send_message(in_chat, "Mordred wins")
                else:
                    bot_send_message(in_chat, 'Avalon wins')
                for key in players_id[in_chat].players.keys():
                    chat_of_player.pop(key)
                players_id.pop(in_chat)
            elif call.data == "register":
                try:
                    players_id[int(call.message.chat.id)]
                except KeyError:
                    bot.reply_to(
                        call.message,
                        'No registration started!\nRun /start_registration')
                    return
                if players_id[call.message.chat.id].state == 'reg':
                    if not call.from_user.username:
                        bot_send_message(
                            call.from_user.id,
                            "Your telegram account has no username")
                        return
                    if int(call.from_user.id) not in players_id[int(
                            call.message.chat.id)].players:
                        if int(call.from_user.id) in chat_of_player.keys():
                            bot_send_message(int(call.from_user.id),
                                             'You are in not ended game!')
                            return
                        try:

                            bot.send_message(
                                call.from_user.id,
                                'You`re registered for the Avalon game in ' +
                                str(call.message.chat.title))
                        except telebot.apihelper.ApiException:
                            bot_send_message(
                                chat_id, 'To be able to register in game, '
                                'say /start to @Avalon117bot in private messages '
                                'and push registration button again')
                            return
                        chat_of_player[int(call.from_user.id)] = int(
                            call.message.chat.id)
                        players_id[int(call.message.chat.id)].players[int(
                            call.from_user.id)] = None
                        players_id[int(call.message.chat.id)].players_nick_to_id['@' + call.from_user.username] = \
                            int(call.from_user.id)
                        keyboard = telebot.types.InlineKeyboardMarkup()
                        add_button = telebot.types.InlineKeyboardButton(
                            text="Register", callback_data="register")
                        keyboard.add(add_button)
                        text = call.message.text + ' @' + call.from_user.username
                        bot.edit_message_text(
                            chat_id=call.message.chat.id,
                            message_id=call.message.message_id,
                            text=text,
                            reply_markup=keyboard)
                        players_id[call.message.chat.id].reg_btn = call.message

                    # bot_send_message(call.from_user.id, 'You`re registered for the Avalon game in ' +
                    #                  call.message.chat.title)
                    # bot_send_message(chat_id, len(players_id[int(chat_id)]))
                else:
                    bot.reply_to(call.message, 'Game is on!')
            elif call.data == "Lady of the Lake":
                if players_id[chat_id].creator != int(call.from_user.id):
                    return
                out = players_id[chat_id].change_lady()
                keyboard = vote.add_roles_btn(players_id[chat_id])
                bot.edit_message_reply_markup(
                    chat_id=chat_id,
                    message_id=players_id[chat_id].additional_id,
                    reply_markup=keyboard)
            elif call.data == "Morgana" or call.data == "Oberon" or call.data == "Mordred":
                if players_id[chat_id].creator != int(call.from_user.id):
                    return
                out = players_id[chat_id].change_roles(call.data)
                keyboard = vote.add_roles_btn(players_id[chat_id])
                bot.edit_message_reply_markup(
                    chat_id=chat_id,
                    message_id=players_id[chat_id].additional_id,
                    reply_markup=keyboard)
            elif call.data[0] == 'v':
                if call.from_user.id != players_id[chat_id].order[
                        players_id[chat_id].cur_king]:
                    return
                arr = call.data.split()
                data = arr[1]
                if int(data) in players_id[chat_id].cur_exp:
                    for i in range(0, len(players_id[chat_id].cur_exp)):
                        if int(data) == players_id[chat_id].cur_exp[i]:
                            players_id[chat_id].cur_exp.pop(i)
                            break
                    keyboard = vote.vote_keyboard(chat_id, players_id[chat_id])
                    bot.edit_message_reply_markup(
                        chat_id=chat_id,
                        message_id=players_id[chat_id].vote_msg_id,
                        reply_markup=keyboard)
                else:
                    players_id[chat_id].cur_exp.append(int(data))
                    keyboard = vote.vote_keyboard(chat_id, players_id[chat_id])
                    bot.edit_message_reply_markup(
                        chat_id=chat_id,
                        message_id=players_id[chat_id].vote_msg_id,
                        reply_markup=keyboard)
            elif call.data == 'send_expedition':
                if call.from_user.id != players_id[chat_id].order[
                        players_id[chat_id].cur_king]:
                    return
                if len(players_id[chat_id].cur_exp
                       ) != players_id[chat_id].exp_size[
                           players_id[chat_id].get_num_of_exp()]:
                    bot_send_message(chat_id, 'Wrong number of expeditors')
                    return
                string = ''
                for i in players_id[chat_id].cur_exp:
                    string += '\n@' + str(
                        bot.get_chat_member(chat_id, i).user.username)
                bot_send_message(chat_id, "The expedition is:" + string)
                for i in players_id[chat_id].order:
                    bot_send_message(i, "The expedition is:" + string)
                players_id[chat_id].state = 'vote'
                gameplay.vote_for_exp(players_id[chat_id].order, chat_id)
                for id in players_id[chat_id].del_msg:
                    bot.delete_message(chat_id=chat_id, message_id=id)
                players_id[chat_id].del_msg = []
            else:
                data = call.data.split()
                chat = int(data[1])
                user = int(data[0])
                nickname = str(bot.get_chat_member(chat, user).user.username)
                lady_id = players_id[chat].order[players_id[chat].cur_lady]
                bot_send_message(chat,
                                 "Lady of the Lake has checked @" + nickname)
                players_id[chat].pass_lady(user)
                players_id[chat].past_lady.append(user)
                check = ''
                if players_id[chat].players[user] in players_id[chat].peaceful:
                    check = " is servant of Arthur"
                else:
                    check = " is servant of Mordred"
                bot_send_message(lady_id, '@' + nickname + check)
                bot_send_message(chat,
                                 '@' + nickname + " is new Lady of the Lake")
                try:
                    players_id[chat].cur_exp = []
                    for player in players_id[chat].players.keys():
                        players_id[chat].cur_voting_for_exp[player] = None
                    vote.send_voting(chat, players_id[chat])
                except KeyError:
                    print(KeyError)

    except telebot.apihelper.ApiException:
        print("stop DDOSing buttons!")
    except KeyError:
        bot_send_message(
            chat_id, "Don`t touch old buttons, run /start_registration first!")
コード例 #8
0
def leave(msg):
    if msg.from_user.id not in chat_of_player:
        bot_send_message(msg.from_user.id, "You don`t take part in any game")
        return
    if players_id[chat_of_player[msg.from_user.id]].state != 'reg':
        if msg.from_user.id not in chat_of_player.keys():
            return
        bot.reply_to(msg, 'You have left the game')
        currChat = chat_of_player[msg.from_user.id]
        if players_id[currChat].players[msg.from_user.id] == "Merlin":
            bot_send_message(currChat,
                             'Merlin has left the game\n\nRIP Avalon!!')
            for id in players_id[currChat].del_msg:
                bot.delete_message(chat_id=currChat, message_id=id)
            string = ""
            for item in players_id[currChat].players.items():
                string += '\n@' + bot.get_chat_member(currChat, item[0]).user.username + ' was ' + \
                          ('❤️' if item[1] in tools.GameInfo.peaceful else '🖤') + item[1]
            bot_send_message(currChat, 'Roles in this game:' + string)
            for id in players_id[currChat].players:
                chat_of_player.pop(id)
            players_id.pop(currChat)
            return
        if players_id[currChat].players[msg.from_user.id] == "Assassin":
            for item in players_id[currChat].players.keys():
                if players_id[currChat].players[item] not in players_id[
                        currChat].peaceful:
                    bot.send_message(item, "You are the new Assassin")
                    players_id[currChat].players[item] = 'Assassin'
                    break
        print(msg.from_user)
        #print(chat_of_player[currChat].order)
        #print(chat_of_player[currChat].cur_king)
        #print(msg.from_user.username)
        if players_id[currChat].order[
                players_id[currChat].cur_king] == msg.from_user.username:
            players_id[currChat].cur_king = (players_id[currChat].cur_king + 1) % \
                                               (len(players_id[currChat].order) - 1)
        if players_id[currChat].lady_lake and players_id[currChat].order[
                players_id[currChat].cur_lady] == msg.from_user.username:
            players_id[currChat].cur_lady = (players_id[currChat].cur_lady + 1) % \
                                               (len(players_id[currChat].order) - 1)
        if msg.from_user.id in players_id.keys():
            players_id.pop(msg.from_user.id)
        print(players_id[currChat].order)
        for i in range(len(players_id[currChat].order)):
            if players_id[currChat].order[i] == msg.from_user.id:
                players_id[currChat].order.pop(i)
                break
        #players_id[currChat].order.pop(msg.from_user.id)
        chat_of_player.pop(msg.from_user.id)
        players_id[currChat].players.pop(msg.from_user.id)
        if len(players_id[currChat].players) == 0:
            bot.send_message(currChat, "All players have left the game")
            for id in players_id[currChat].del_msg:
                bot.delete_message(chat_id=currChat, message_id=id)
            players_id[currChat].del_msg = []
            for player in players_id[currChat].players.keys():
                chat_of_player.pop(player)
            players_id.pop(currChat)
            bot.send_message(currChat, 'Game aborted')
        return
    if msg.from_user.id in chat_of_player.keys():
        chat_of_player.pop(msg.from_user.id)
    for chat in players_id:
        if msg.from_user.id in players_id[chat].players:
            del players_id[chat].players[msg.from_user.id]

        keyboard = telebot.types.InlineKeyboardMarkup()
        add_button = telebot.types.InlineKeyboardButton(
            text="Register", callback_data="register")
        keyboard.add(add_button)
        reg_msg = players_id[chat].reg_btn
        reg = 'Registration is on\nPlayers in game:'
        for user in players_id[chat].players:
            reg = reg + ' @' + str(
                bot.get_chat_member(chat, user).user.username)

        print(reg)
        try:

            bot.edit_message_text(chat_id=chat,
                                  message_id=reg_msg.message_id,
                                  text=reg,
                                  reply_markup=keyboard)
        except:
            return
        bot_send_message(msg.from_user.id, "You have left the game")