Пример #1
0
    def tag_active(self, *args, **afqwe):
        chats = DB.get_all_chats()

        for chat in chats:

            if chat['chat_id'] in [-1001243947001]:
                continue

            active_users = DB.get_chat_active_users(chat['chat_id'])

            if len(active_users) > 0:

                if int(chat['chat_id']) < 0:

                    endings = [
                        "ов", "", "а", "а", "а", "ов", "ов", "ов", "ов", "ов"
                    ]

                    string = "🟢 *{} участник{} чата сейчас онлайн:*\n\n".format(
                        len(active_users), endings[len(active_users) % 10])
                    data_now = datetime.datetime.now()

                    all_ranks = DB.get_all_ranks()

                    for user in active_users:
                        data_start = datetime.datetime.strptime(
                            user['start_time'], '%Y-%m-%d %H:%M:%S')
                        duration = (data_now - data_start).seconds / 3600

                        if user['username'] != "None":

                            if user['tag']:
                                username = (
                                    "@" + user['username'].replace("_", "\_"))
                                username = username.replace(".", "\.")
                            else:
                                username = (
                                    "`" + user['username'].replace("_", "\_") +
                                    "`")
                        else:
                            if user['tag']:
                                username = "******".format(
                                    user['user_id'], user['user_id'])
                            else:
                                username = str(user['user_id'])

                        hours = DB._get_user_useful_time(
                            user['user_id'], 'all')['time']

                        rank = all_ranks[0]['name']

                        for obj in all_ranks:
                            if obj['min_hours'] <= hours:
                                rank = obj['name']
                            else:
                                break

                        activity_name = user['name'].replace("_", "\_")
                        activity_name = activity_name.replace("(", "\(")
                        activity_name = activity_name.replace(")", "\)")
                        activity_name = activity_name.replace("-", "\-")
                        activity_name = activity_name.replace(".", "\.")

                        string += "🔸{} \[`{}`\] \- *{}* \(_{}_\)\n".format(
                            username, rank, activity_name,
                            self.get_string_by_duration(duration))

                    string += "\n`Выключить/включить теги \- ` /toggle\_tag"

                    try:

                        self.updater.bot.send_message(
                            chat_id=chat['chat_id'],
                            # chat_id=-1001243947001,
                            text=string,
                            parse_mode="MarkdownV2")

                    except Exception as e:
                        print(e.message)

                else:
                    user = active_users[0]

                    date_now = datetime.datetime.now()

                    data_start = datetime.datetime.strptime(
                        user['start_time'], '%Y-%m-%d %H:%M:%S')
                    duration = (date_now - data_start).seconds / 3600

                    string = '🟢 У тебя активно занятие "*%s*" \(_%s_\)\n\n⏹ Остановить: /stop' % (
                        user['name'], self.get_string_by_duration(duration))
                    try:

                        self.updater.bot.send_message(
                            chat_id=chat['chat_id'],
                            # chat_id=-1001243947001,
                            text=string,
                            parse_mode="MarkdownV2")

                    except Exception as e:
                        print(e.message)
Пример #2
0
    def tag_all(self):

        chats = DB.get_all_chats()

        for chat in chats:
            users = DB.get_chat_users(chat['chat_id'])

            if int(chat['chat_id']) < 0:
                all_ranks = DB.get_all_ranks()

                durations = dict()
                ranks = dict()

                for user in users:
                    obj = DB.get_today_user_useful_time(user['user_id'])

                    now = datetime.datetime.now()

                    if obj['time'] is not None:
                        duration = float(obj['time'])

                        if obj['start_time'] is not None:
                            data_start = datetime.datetime.strptime(
                                obj['start_time'], '%Y-%m-%d %H:%M:%S')

                            diff = (now - data_start).seconds / 3600
                            duration = duration + diff
                    else:
                        duration = 0

                    if user['username'] != "None":
                        username = ("@" + user['username'].replace("_", "\_"))
                        username = username.replace(".", "\.")
                    else:
                        username = "******".format(
                            user['user_id'], user['user_id'])

                    durations[username] = duration

                    hours = DB._get_user_useful_time(user['user_id'],
                                                     'all')['time']

                    rank = all_ranks[0]['name']

                    for obj in all_ranks:
                        if obj['min_hours'] <= hours:
                            rank = obj['name']
                        else:
                            break

                    ranks[username] = rank

                durations = dict(
                    sorted(durations.items(),
                           key=lambda item: item[1],
                           reverse=True))

                string = "🧮 *Итоги дня*\n\n"

                i = 0

                emoji = ['🔹', '🔸']

                for name in durations:
                    string += "%s%s \- _%s_ \(Звание *%s*\)\n" % (
                        emoji[i % 2], name,
                        self.get_string_by_duration(
                            durations[name]), ranks[name])
                    i += 1

                try:

                    self.updater.bot.send_message(chat_id=chat['chat_id'],
                                                  text=string,
                                                  parse_mode="MarkdownV2")

                except Exception as e:
                    print(e.message)
            else:
                pass