class PidorCommand(Command): def __init__(self): super().__init__() self.only_public_chats = True self.registration = Registration() def run(self): chat = self.last_update.message.chat if self.need_to_choose(chat): self.choose_user(chat) else: self.show_last_winner(chat) def choose_user(self, chat): users = self.registration.get_registered_users_for_chat(chat) print(len(users)) successful = False user_id = 0 while not successful: index = random.randrange(0, len(users)) user_id = list(users)[index] try: member = self.bot.getChatMember(chat.id, user_id) successful = True except TelegramError: continue name = self.get_display_name(member.user) self.registration.reward_user(chat, member.user, name) self.reply_with_text(f"Выбираю пидора дня...") self.reply_with_text(f"TODO: Сделать побольше сообщений") self.reply_with_text(f'Ты пидор дня, {name}, поздравляю!') def show_last_winner(self, chat): winner = self.registration.get_last_winner(chat) if len(winner) == 0: self.reply_with_text("О нет! Не могу получить победителя.") else: self.reply_with_text(f"Пидор дня – {winner}") def need_to_choose(self, chat): drawing_time = self.registration.get_last_drawing_time(chat) current_time = time() return current_time - drawing_time > 24 * 60 * 60
class StatsCommand(Command): first_places_formats = [ "🌟 *{}* 🌟 – мега-пидор, был пидором дня {} раз(а)", "⭐️ *{}* ⭐️ – почти всем пидорам пидор, был пидором дня {} раз(а)", "✨ *{}* ✨ – уважаемый в широких кругах пидор, был пидором дня {} раз(а)", ] def __init__(self): super().__init__() self.only_public_chats = True self.registration = Registration() def __get_format_string(self, place): if place < len(self.first_places_formats): return self.first_places_formats[place] else: return str(place + 1) + '. {} – {}' def run(self): chat = self.last_update.message.chat users = self.registration.get_registered_users_for_chat(chat) wins = sorted(users, key=lambda key: users[key]['wins'], reverse=True)[:10] usernames = {user: self.get_username(chat, user) for user in wins} lines = [ self.__get_format_string(place).format(usernames[user], str(users[user]['wins'])) for place, user in enumerate(wins) if users[user]['wins'] > 0 ] text = "\n".join(lines) self.reply_with_markdown(text) def get_username(self, chat, user_id): try: user = self.bot.get_chat_member(chat.id, user_id).user return self.get_display_name(user) except TelegramError: return 'Ноунейм'