paring
This commit is contained in:
parent
59d670db82
commit
5eed097dfa
13
main.py
13
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()
|
||||
|
11
mongo.py
11
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()
|
||||
|
Loading…
Reference in New Issue
Block a user