From 5eed097dfacc2fee2c6b9abb7fcca14f3af0ed5e Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 20 Jan 2023 18:53:07 +0300 Subject: [PATCH] paring --- main.py | 13 +++++++++++++ mongo.py | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/main.py b/main.py index 7a8e2b7..12c01bc 100644 --- a/main.py +++ b/main.py @@ -64,6 +64,18 @@ def set_probability(message: Message): set_values(message.chat.id, state="set_probability") +@bot.message_handler(commands=['rating']) +def set_probability(message: Message): + rating = list(mongo.counter_collection.find({"chat_id": message.chat.id}).sort("count", -1)) + if not rating: + bot.send_message(message.chat.id, "В этом чате я пока никому не парировал") + return + text = "Вот кому я парировал:\n" + for index, value in enumerate(rating): + text += f"{index + 1}. @{value['username']} - {value['count']}\n" + bot.send_message(message.chat.id, text) + + @bot.message_handler() def do_action(message: Message): info = get_chat_info(message.chat.id) @@ -102,6 +114,7 @@ def do_action(message: Message): ans = "Хуй на!" if ans is not None and randrange(1, 101) <= info["probability"]: bot.reply_to(message, ans) + mongo.inc(message.from_user.username, message.chat.id) bot.polling() diff --git a/mongo.py b/mongo.py index 6b0c991..6b9880e 100644 --- a/mongo.py +++ b/mongo.py @@ -13,6 +13,10 @@ class Mongo: self.chats_collection.create_index([ ("chat_id", 1) ]) + self.counter_collection.create_index([ + ("chat_id", 1), + ("username", 1), + ]) def __getitem__(self, item): return self.database.get_collection(item) @@ -21,5 +25,12 @@ class Mongo: def chats_collection(self): return self["chats"] + @cached_property + def counter_collection(self): + return self["counter"] + + def inc(self, username, chat_id): + self.counter_collection.update_one({"chat_id": chat_id, "username": username}, {"$inc": {"count": 1}}) + mongo = Mongo()