示例#1
0
def yes_game(context, data, chat_id, dp):
    chat_data = dp.chat_data[chat_id]
    lang = data["lang"]
    group_settings = data["group_settings"]
    deck = Deck(*group_settings["deck"].split("_"))
    chameleon = random.choice(list(data["players"]))
    random.shuffle(data["players"])
    game_id = ''.join(random.choices(string.ascii_lowercase, k=10))
    chat_data.update({"chameleon": chameleon, "secret": deck.secret, "players": data["players"], "lang": lang,
                      "starter": data["starter"], "words": deck.words, "game_id": game_id,
                      "fewer": group_settings["fewer"], "tournament": group_settings["tournament"],
                      "more": group_settings["more"], "pin": group_settings["pin"], "restrict": {},
                      "deck": group_settings["deck"], "tutorial": data["tutorial"],
                      "exclamation": group_settings["exclamation"]})
    text = get_string(lang, "game_succeed").format(deck.topic, deck.word_list)
    button = InlineKeyboardMarkup([[InlineKeyboardButton(get_string(lang, "play_button"),
                                                         callback_data="word" + game_id)]])
    message = context.bot.send_message(chat_id, text, reply_to_message_id=data["message"], reply_markup=button,
                                       parse_mode=ParseMode.HTML)
    chat = None
    if group_settings["pin"] or group_settings["restrict"]:
        chat = context.bot.get_chat(chat_id)
    if group_settings["pin"]:
        pinned_message = chat.pinned_message
        if pinned_message:
            chat_data["pin"] = pinned_message.message_id
        try:
            context.bot.pin_chat_message(chat_id, message.message_id, True)
        except BadRequest as e:
            if e.message == "Not enough rights to pin a message":
                chat_data["pin"] = False
                database.insert_group_pin(chat_id)
                e.message += "handled in ghelper L48"
                raise e
    user = data["players"][0]
    text = get_string(lang, "first_player_say_word").format(mention_html(user["user_id"], user["first_name"]))
    if not group_settings["restrict"]:
        if group_settings["exclamation"]:
            text += "\n\n" + get_string(lang, "exclamation_activated")
        else:
            text += "\n\n" + get_string(lang, "exclamation_deactivated")
    context.bot.send_message(chat_id, text, reply_to_message_id=message.message_id, parse_mode=ParseMode.HTML)
    if group_settings["restrict"]:
        chat_data["restrict"]["initial_permissions"] = chat.permissions
        try:
            context.bot.set_chat_permissions(chat_id, ChatPermissions(can_send_messages=False))
            if not is_admin(context.bot, user["user_id"], chat):
                context.bot.promote_chat_member(chat_id, user["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 ghelper L68"
            raise e
    chat_data["word_list"] = message.message_id
def restrict(update: Update, context: CallbackContext):
    query = update.callback_query
    chat_id = int(query.data.split("_")[1])
    lang = context.user_data["lang"]
    if not database.get_restrict_setting(chat_id):
        chat_member = context.bot.get_chat_member(chat_id, context.bot.id)
        if chat_member.can_invite_users and chat_member.can_promote_members and chat_member.can_restrict_members:
            database.insert_group_restrict(chat_id)
            query.answer(get_string(lang, "group_setting_restrict_activate"))
        else:
            query.answer(get_string(lang, "group_setting_restrict_required"),
                         show_alert=True)
            return
    else:
        database.insert_group_restrict(chat_id)
        query.answer(get_string(lang, "group_setting_restrict_deactivate"))
    edit(query, chat_id, lang)
示例#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
    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
示例#4
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)