Ejemplo n.º 1
0
    def choose_new_name(self, update: Update, context: CallbackContext):
        Queue.add(update.message.chat.id, update.message.message_id)

        chat_id = update.message.chat.id
        new_team_name = update.message.text
        old_team_name = context.user_data['old_team_name']

        if App.db.get_teams().find_one({'chat_id': chat_id, 'name': new_team_name}):
            message = update.message.reply_text(
                f'Team wasn\'t renamed, cause "{new_team_name}" already exists, send new name for team "{old_team_name}" again',
            )
            Queue.add(message.chat.id, message.message_id)
            return RenameTeam.CHOOSING_NEW_NAME

        if not re.match(r'^[0-9a-zа-яё._-]{2,16}$', new_team_name.lower(), re.IGNORECASE):
            message = update.message.reply_text(
                f'Team wasn\'t renamed, cause name "{new_team_name}" contains incorrect symbols, try again',
            )
            Queue.add(message.chat.id, message.message_id)
            return RenameTeam.CHOOSING_NEW_NAME

        if App.db.get_teams().find_one_and_update({'chat_id': chat_id, 'name': old_team_name}, {'$set': {'name': new_team_name}}):
            message = update.message.reply_text(
                f'Team "{old_team_name}" was renamed to "{new_team_name}"'
            )
            Queue.add(message.chat.id, message.message_id)
        else:
            message = update.message.reply_text(
                f'Team "{old_team_name}" wasn\'t renamed to "{new_team_name}" due to some errors, try again later',
                reply_markup=ReplyKeyboardRemove(remove_keyboard=True),
            )
            Queue.add(message.chat.id, message.message_id)

        Queue.clean(update.message.chat.id, timeout=30)
        return RenameTeam.CHOOSING_END
Ejemplo n.º 2
0
    def choose_team(self, update: Update, context: CallbackContext):
        Queue.add(update.message.chat.id, update.message.message_id)
        
        chat_id = update.message.chat.id
        team = update.message.text

        if not re.match(f'^({HandlerHelpers.make_teams_regex(chat_id)})$', team):
            message = update.message.reply_text(f'Team "{team}" wasn\'t found, try again')
            Queue.add(message.chat.id, message.message_id)
            return RemoveTeam.CHOOSING_TEAM

        document = App.db.get_teams().find_one_and_delete({
            'chat_id': chat_id,
            'name': team,
        })
        if document:
            message = update.message.reply_text(
                f'Team "{team}" was successfully removed',
                reply_markup=ReplyKeyboardRemove(remove_keyboard=True),
            )
            Queue.add(message.chat.id, message.message_id)
        else:
            message = update.message.reply_text(
                f'Team "{team}" wasn\'t removed due to some errors, try again later',
                reply_markup=ReplyKeyboardRemove(remove_keyboard=True),
            )
            Queue.add(message.chat.id, message.message_id)

        Queue.clean(update.message.chat.id, timeout=30)
        return RemoveTeam.CHOOSING_END
Ejemplo n.º 3
0
    def mention(self, update, context: CallbackContext):
        Queue.add(update.message.chat.id, update.message.message_id)

        chat_id = update.message.chat.id
        team = update.message.text

        if not re.match(f'^({HandlerHelpers.make_teams_regex(chat_id)})$', team):
            message = update.message.reply_text(f'Team "{team}" wasn\'t found, try again')
            Queue.add(message.chat.id, message.message_id)
            return Mention.MENTION

        members = HandlerHelpers.get_team_members(chat_id, team)

        if members:
            update.message.reply_text(
                ' '.join(members),
                reply_markup=ReplyKeyboardRemove(remove_keyboard=True)
            )
        else:
            message = update.message.reply_text(
                'Members weren\'t found or something went wrong',
                reply_markup=ReplyKeyboardRemove(remove_keyboard=True)
            )
            Queue.add(message.chat.id, message.message_id)

        Queue.clean(update.message.chat.id, timeout=30)
        return Mention.CHOOSING_END
Ejemplo n.º 4
0
    def get(self, update: Update, context: CallbackContext):
        Queue.add(update.message.chat.id, update.message.message_id)

        chat_id = update.message.chat.id
        teams = HandlerHelpers.get_teams(chat_id)

        if len(teams) > 0:
            message = update.message.reply_text(', '.join(teams))
            Queue.add(message.chat.id, message.message_id, timeout=300)
        else:
            message = update.message.reply_text('No teams were found')
            Queue.add(message.chat.id, message.message_id)

        Queue.clean(update.message.chat.id)
        return GetTeams.GETTING_END
Ejemplo n.º 5
0
    def choose_members(self, update: Update, context: CallbackContext):
        Queue.add(update.message.chat.id, update.message.message_id)

        chat_id = update.message.chat.id
        team = context.user_data['team']

        # set is used to remove duplicated
        members = list(
            set(filter(
                lambda x: re.match(r'^@[0-9a-z._-]{5,32}$', x.lower(), re.IGNORECASE) and x not in HandlerHelpers.get_team_members(chat_id, team),
                update.message.text.split(' ')
            ))
        )

        if members:
            App.db.get_teams().find_one_and_update({
                'chat_id': chat_id,
                'name': team,
            }, {
                '$push': {
                    'members': {
                        '$each': members,
                    },
                },
            })

            message = update.message.reply_text(
                f'Greeting new members of the "{team}" team: {" ".join(members)}'
            )

            Queue.add(message.chat.id, message.message_id)
        else:
            message = update.message.reply_text(
                f'No members were added to the team "{team}"'
            )

            Queue.add(message.chat.id, message.message_id)

        Queue.clean(update.message.chat.id, timeout=30)
        return AddMembers.CHOOSING_END
Ejemplo n.º 6
0
    def help(self, update: Update, context: CallbackContext):
        timeout = 120
        Queue.add(update.message.chat.id,
                  update.message.message_id,
                  timeout=timeout)

        message = update.message.reply_text(
            '*Commands for admins:*\n'
            '/addteam – Add team\n'
            '/addmembers – Add members to the chosen team\n'
            '/removeteam – Remove team\n'
            '/removemembers – Remove members from the chosen team\n'
            '/renameteam – Rename team to the new given name\n'
            '\n'
            '*Commands for all members:*\n'
            '/mention – Choose a team to mention\n'
            '/getteams – List of all added teams\n'
            '/help – Get some basic info about bot and commands\n'
            '/cancel – Prevent any running command\n',
            parse_mode=ParseMode.MARKDOWN)

        Queue.add(message.chat.id, message.message_id, timeout=timeout)
        Queue.clean(update.message.chat.id)
Ejemplo n.º 7
0
    def choose_name(self, update: Update, context: CallbackContext):
        Queue.add(update.message.chat.id, update.message.message_id)

        name = update.message.text

        if re.match(r'^[0-9a-zа-яё._-]{2,16}$', name.lower(), re.IGNORECASE):
            if App.db.get_teams().find_one({
                'name': re.compile(f'^{name.lower()}$', re.IGNORECASE),
                'chat_id': update.message.chat.id
            }):
                message = update.message.reply_text(
                    f'Team "{name}" already exists, try another name'
                )
                Queue.add(message.chat.id, message.message_id)
                return AddTeam.CHOOSING_NAME
            else:
                App.db.get_teams().insert_one({
                    'name': name,
                    'chat_id': update.message.chat.id,
                    'members': [],
                })

                message = update.message.reply_text(
                    f'Team "{name}" was successfully created'
                )

                Queue.add(message.chat.id, message.message_id)
        else:
            message = update.message.reply_text(
                f'Team "{name}" wasn\'t created cause name contains incorrect symbols, try again'
            )

            Queue.add(message.chat.id, message.message_id)
            return AddTeam.CHOOSING_NAME

        Queue.clean(update.message.chat.id, timeout=30)
        return AddTeam.CHOOSING_END
Ejemplo n.º 8
0
    def choose_members(self, update, context: CallbackContext):
        Queue.add(update.message.chat.id, update.message.message_id)

        chat_id = update.message.chat.id
        team = context.user_data['team']

        members = list(
            filter(
                lambda x: x.startswith('@') and x in
                HandlerHelpers.get_team_members(chat_id, team),
                update.message.text.split(' ')))

        if members:
            App.db.get_teams().find_one_and_update(
                {
                    'chat_id': chat_id,
                    'name': team,
                }, {
                    '$pull': {
                        'members': {
                            '$in': members,
                        },
                    },
                })

            message = update.message.reply_text(
                f'Members who were removed from "{team}" team: {" ".join(members)}'
            )
            Queue.add(message.chat.id, message.message_id)
        else:
            message = update.message.reply_text(
                f'No members were removed from the team "{team}"')
            Queue.add(message.chat.id, message.message_id)

        Queue.clean(update.message.chat.id, timeout=30)
        return RemoveMembers.CHOOSING_END