probability
This commit is contained in:
69
main.py
69
main.py
@@ -1,8 +1,12 @@
|
||||
import os
|
||||
from random import randrange
|
||||
|
||||
import telebot
|
||||
from cachetools import TTLCache
|
||||
from telebot.types import Message
|
||||
|
||||
import settings
|
||||
from mongo import mongo
|
||||
|
||||
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
||||
|
||||
@@ -14,21 +18,78 @@ answers_net_rus = {"нет", "нeт"}
|
||||
answers_net_eng = {"net", "nеt"}
|
||||
|
||||
|
||||
cache = TTLCache(settings.CACHE_SIZE, settings.CACHE_TTL)
|
||||
|
||||
|
||||
def get_chat_info(chat_id: int) -> dict:
|
||||
cached_info = cache.get(chat_id)
|
||||
if cached_info is not None:
|
||||
return cached_info
|
||||
mongo_info = mongo.chats_collection.find_one({"chat_id": chat_id})
|
||||
if mongo_info is not None:
|
||||
cache[chat_id] = mongo_info
|
||||
return mongo_info
|
||||
chat_info = {"chat_id": chat_id, "state": "default", "probability": 100}
|
||||
mongo.chats_collection.insert_one(chat_info)
|
||||
cache[chat_id] = chat_info
|
||||
return chat_info
|
||||
|
||||
|
||||
def set_values(chat_id: int, **values):
|
||||
cached_info = cache.get(chat_id)
|
||||
if cached_info is None:
|
||||
mongo_info = mongo.chats_collection.find_one({"chat_id": chat_id})
|
||||
if mongo_info is None:
|
||||
chat_info = {"chat_id": chat_id, "state": "default", "probability": 100}
|
||||
chat_info.update(values)
|
||||
mongo.chats_collection.insert_one(chat_info)
|
||||
cache[chat_id] = chat_info
|
||||
else:
|
||||
mongo.chats_collection.update_one({"chat_id": chat_id}, {"$set": values})
|
||||
mongo_info = dict(mongo_info)
|
||||
mongo_info.update(values)
|
||||
cache[chat_id] = mongo_info
|
||||
else:
|
||||
cached_info.update(values)
|
||||
mongo.chats_collection.update_one({"chat_id": chat_id}, {"$set": values})
|
||||
|
||||
|
||||
@bot.message_handler(commands=['setprobability'])
|
||||
def set_probability(message: Message):
|
||||
bot.send_message(message.chat.id, "Отправь одно число - вероятность парирования")
|
||||
set_values(message.chat.id, state="set_probability")
|
||||
|
||||
|
||||
@bot.message_handler()
|
||||
def do_action(message: Message):
|
||||
info = get_chat_info(message.chat.id)
|
||||
if info['state'] == "set_probability":
|
||||
try:
|
||||
value = int(message.text)
|
||||
if value < 0 or value > 100:
|
||||
bot.reply_to(message, "Число не попадает в диапозон от 0 до 100!")
|
||||
else:
|
||||
set_values(message.chat.id, probability=value, state="default")
|
||||
bot.reply_to(message, "Ок! Установил")
|
||||
except ValueError:
|
||||
bot.reply_to(message, "Это не число!")
|
||||
return
|
||||
convert_text = ''.join([letter for letter in message.text if letter in all_letters]).lower().split()
|
||||
if len(convert_text) > 0:
|
||||
convert_text = convert_text[-1]
|
||||
else:
|
||||
return
|
||||
ans = None
|
||||
if convert_text in answers_rus:
|
||||
bot.reply_to(message, "Пизда!")
|
||||
ans = "Пизда!"
|
||||
if convert_text in answers_eng:
|
||||
bot.reply_to(message, "Pizda!")
|
||||
ans = "Pizda!"
|
||||
if convert_text in answers_net_rus:
|
||||
bot.reply_to(message, "Пидора ответ!")
|
||||
ans = "Пидора ответ!"
|
||||
if convert_text in answers_net_eng:
|
||||
bot.reply_to(message, "Pidora otvet!")
|
||||
ans = "Pidora otvet!"
|
||||
if ans is not None and randrange(1, 101) <= info["probability"]:
|
||||
bot.reply_to(message, ans)
|
||||
|
||||
|
||||
bot.polling()
|
||||
|
||||
Reference in New Issue
Block a user