Ejemplo n.º 1
0
def vote(update: Update, context: CallbackContext):
    query = update.callback_query
    chat_data = context.chat_data
    user_id = update.effective_user.id
    game_id = query.data[4:14]
    # somehow this button is there, but no game is running
    if "game_id" not in chat_data:
        no_game(update, context, "join_no_game_running")
        return
    # somehow this button is there, but another game is running
    elif chat_data["game_id"] != game_id:
        no_game(update, context, "wrong_game")
        return
    vote_id = int(query.data[14:])
    lang = chat_data["lang"]
    if user_id not in [user["user_id"] for user in chat_data["players"]]:
        query.answer(get_string(lang, "user_not_in_game"), show_alert=True)
        return
    players = chat_data["players"]
    if user_id in chat_data["voted"]:
        query.answer(get_string(lang, "already_voted"), show_alert=True)
        return
    elif user_id == vote_id:
        query.answer(get_string(lang, "vote_yourself"), show_alert=True)
        return
    else:
        pass
    for player in players:
        if vote_id == player["user_id"]:
            if "votes" in player:
                player["votes"] += 1
            else:
                player["votes"] = 1
            break
    chat_data["voted"].append(user_id)
    query.answer(get_string(lang, "voted"))
    voters = players.copy()
    for voter_id in chat_data["voted"]:
        for index, voter in enumerate(voters):
            if voter["user_id"] == voter_id:
                del voters[index]
    if len(players) is not len(chat_data["voted"]):
        buttons = vote_buttons(players, chat_data["game_id"])
        words = wordlist(players)
        text = get_string(lang, "final_word_list").format(words) + "\n" + get_string(lang, "vote_list").\
            format(player_mention_string(voters))
        query.edit_message_text(text,
                                reply_markup=InlineKeyboardMarkup(buttons),
                                parse_mode=ParseMode.HTML)
    else:
        words = wordlist(players)
        text = get_string(lang, "final_word_list").format(words) + "\n" + get_string(lang, "vote_list").\
            format(player_mention_string(voters))
        query.edit_message_text(text, parse_mode=ParseMode.HTML)
        votes = []
        for player in players:
            if "votes" in player:
                votes.append((player["votes"], player["user_id"]))
            else:
                votes.append((0, player["user_id"]))
        # this works cause https://docs.python.org/3/howto/sorting.html#key-functions. Do I have any idea why? no
        votes = sorted(votes, key=lambda votes: votes[0], reverse=True)
        # this means, we have a draw, so the first person who had to say a word (cause disadvantage) can decide
        if votes[0][0] == votes[1][0]:
            same_votes = []
            for vote_number in votes:
                if vote_number[0] is not votes[0][0]:
                    break
                else:
                    voter_id = vote_number[1]
                    for player in players:
                        if player["user_id"] == voter_id:
                            same_votes.append({
                                "user_id":
                                voter_id,
                                "first_name":
                                player["first_name"]
                            })
                            break
            text = get_string(lang, "draw").format(
                mention_html(players[0]["user_id"], players[0]["first_name"]))
            buttons = draw_buttons(same_votes, chat_data["game_id"])
            query.message.reply_html(
                text, reply_markup=InlineKeyboardMarkup(buttons), quote=False)
        else:
            chat_id = update.effective_message.chat.id
            unmasked_id = votes[0][1]
            who_wins(context, chat_id, unmasked_id)
Ejemplo n.º 2
0
def message(update: Update, context: CallbackContext):
    chat_data = context.chat_data
    # check if a game is running, could also be game_id or smth else
    if "chameleon" not in chat_data or "voted" in chat_data:
        return
    if not chat_data["restrict"]:
        if update.effective_message.text.startswith("!"):
            return
    user_id = update.effective_user.id
    if user_id not in [user["user_id"] for user in chat_data["players"]]:
        return
    chat_id = update.effective_chat.id
    lang = chat_data["lang"]
    players = chat_data["players"]
    done = False
    for index, player in enumerate(players):
        if "word" not in player:
            if user_id == player["user_id"]:
                word = update.effective_message.text
                players[index]["word"] = word
                try:
                    next_player = players[index + 1]
                    if chat_data["restrict"]:
                        try:
                            if not chat_data["restrict"]["skip"]:
                                context.bot.promote_chat_member(
                                    chat_id,
                                    player["user_id"],
                                    can_invite_users=False)
                            if not is_admin(context.bot,
                                            next_player["user_id"],
                                            update.effective_chat):
                                context.bot.promote_chat_member(
                                    chat_id,
                                    next_player["user_id"],
                                    can_invite_users=True)
                                chat_data["restrict"]["skip"] = False
                            else:
                                chat_data["restrict"]["skip"] = True
                        except BadRequest as e:
                            chat_data["restrict"] = False
                            database.insert_group_restrict(chat_id)
                            e.message += "handled in game, L50"
                            raise e
                except IndexError:
                    done = True
                    if chat_data["restrict"]:
                        try:
                            if not chat_data["restrict"]["skip"]:
                                context.bot.promote_chat_member(
                                    chat_id,
                                    player["user_id"],
                                    can_invite_users=False)
                            context.bot.set_chat_permissions(
                                chat_id,
                                chat_data["restrict"]["initial_permissions"])
                        except BadRequest as e:
                            chat_data["restrict"] = False
                            database.insert_group_restrict(chat_id)
                            e.message += "handled in game, L62"
                            raise e
                    break
                words = wordlist(players)
                restricted = ""
                if not chat_data["restrict"]:
                    restricted = "\n\n" + get_string(
                        lang, "say_word_not_restricted")
                text = get_string(lang, "more_players_say_word")\
                    .format(mention_html(next_player["user_id"], next_player["first_name"]), words, restricted)
                update.effective_message.reply_html(text)
                return
            else:
                break
    if done:
        chat_data["voted"] = []
        words = wordlist(players)
        text = get_string(lang,
                          "final_word_list").format(words) + "\n" + get_string(
                              lang, "vote_list").format(
                                  player_mention_string(chat_data["players"]))
        buttons = vote_buttons(chat_data["players"], chat_data["game_id"])
        v_message = update.effective_message.reply_html(
            text, reply_markup=InlineKeyboardMarkup(buttons), quote=False)
        if chat_data["pin"]:
            try:
                context.bot.pin_chat_message(chat_id, v_message.message_id,
                                             True)
            except BadRequest as e:
                chat_data["pin"] = False
                e.message += "handled in game L88"
                raise e
Ejemplo n.º 3
0
def message(update: Update, context: CallbackContext):
    chat_data = context.chat_data
    # check if a game is running, could also be game_id or smth else
    if "chameleon" not in chat_data or "voted" in chat_data:
        return
    # this means every message counts anyway
    if not chat_data["restrict"]:
        if update.effective_message.text.startswith("!"):
            # if the exclamation setting is deactivated, every message starting with an ! is ignored
            if not chat_data["exclamation"]:
                return
            else:
                # if the exclamation setting is activated, only messages starting with an ! are valid
                # lets remove the !
                update.effective_message.text = update.effective_message.text[
                    1:]
        else:
            # if the exclamation setting is activated, only messages starting with an ! are valid
            if chat_data["exclamation"]:
                return
    user_id = update.effective_user.id
    if user_id not in [user["user_id"] for user in chat_data["players"]]:
        return
    chat_id = update.effective_chat.id
    lang = chat_data["lang"]
    players = chat_data["players"]
    done = False
    for index, player in enumerate(players):
        if "word" not in player:
            if user_id == player["user_id"]:
                word = update.effective_message.text
                if len(word) > 103:
                    if update.effective_message.link:
                        word = f"<a href=\"{update.effective_message.link}\">{word[:100]}...</a>"
                    else:
                        word = f"{escape(word[:100])}..."
                else:
                    word = escape(word)
                players[index]["word"] = word
                try:
                    next_player = players[index + 1]
                    if chat_data["restrict"]:
                        try:
                            if not chat_data["restrict"]["skip"]:
                                context.bot.promote_chat_member(
                                    chat_id,
                                    player["user_id"],
                                    can_invite_users=False)
                            if not is_admin(context.bot,
                                            next_player["user_id"],
                                            update.effective_chat):
                                context.bot.promote_chat_member(
                                    chat_id,
                                    next_player["user_id"],
                                    can_invite_users=True)
                                chat_data["restrict"]["skip"] = False
                            else:
                                chat_data["restrict"]["skip"] = True
                        except BadRequest as e:
                            chat_data["restrict"] = False
                            database.insert_group_restrict(chat_id)
                            e.message += "handled in game, L50"
                            logger.info(e.message)
                except IndexError:
                    done = True
                    if chat_data["restrict"]:
                        try:
                            if not chat_data["restrict"]["skip"]:
                                context.bot.promote_chat_member(
                                    chat_id,
                                    player["user_id"],
                                    can_invite_users=False)
                            context.bot.set_chat_permissions(
                                chat_id,
                                chat_data["restrict"]["initial_permissions"])
                        except BadRequest as e:
                            chat_data["restrict"] = False
                            database.insert_group_restrict(chat_id)
                            e.message += "handled in game, L62"
                            logger.info(e.message)
                    break
                words = wordlist(players)
                restricted = ""
                if not chat_data["restrict"]:
                    if chat_data["exclamation"]:
                        restricted += "\n\n" + get_string(
                            lang, "exclamation_activated")
                    else:
                        restricted += "\n\n" + get_string(
                            lang, "exclamation_deactivated")
                text = get_string(lang, "more_players_say_word")\
                    .format(mention_html(next_player["user_id"], next_player["first_name"]), words, restricted)
                update.effective_message.reply_html(text)
                return
            else:
                break
    if done:
        chat_data["voted"] = []
        words = wordlist(players)
        text = get_string(lang,
                          "final_word_list").format(words) + "\n" + get_string(
                              lang, "vote_list").format(
                                  player_mention_string(chat_data["players"]))
        buttons = vote_buttons(chat_data["players"], chat_data["game_id"])
        v_message = update.effective_message.reply_html(
            text,
            reply_markup=InlineKeyboardMarkup(buttons),
            reply_to_message_id=chat_data["word_list"])
        if chat_data["pin"]:
            try:
                context.bot.pin_chat_message(chat_id, v_message.message_id,
                                             True)
            except BadRequest as e:
                chat_data["pin"] = False
                e.message += "handled in game L88"
                logger.info(e.message)