def ranks(self, update: Update, context: CallbackContext): ranks = DB.get_all_ranks() users = DB.get_chat_users(update.effective_chat.id) string = "🧩 *Звания участников чата*\n\n" i = 0 emoji = ["🔸", "🔹"] for user in users: hours = DB._get_user_useful_time(user['user_id'], 'all')['time'] rank = ranks[0]['name'] for obj in ranks: if obj['min_hours'] <= hours: rank = obj['name'] else: break username = user['username'].replace("_", "\_") username = username.replace(".", "\.") if hours < 0: hours = 0 string += "%s `%s` \- *%s* \(%d часов с пользой\)\n" % ( emoji[i % 2], username, rank, floor(hours)) update.message.reply_text(text=string, parse_mode="MarkdownV2")
def initial_state(self, update: Update): active_days = DB.get_chat_active_days(update.effective_chat.id) members_count = len(DB.get_chat_users(update.effective_chat.id)) return { "members": members_count if members_count < 7 else 7, "days": active_days, "md": active_days, "mm": members_count }
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