more
This commit is contained in:
parent
4dd6eaa729
commit
07fcef826d
43
main.py
43
main.py
@ -2,11 +2,10 @@ import os
|
|||||||
from random import randrange
|
from random import randrange
|
||||||
|
|
||||||
import telebot
|
import telebot
|
||||||
from cachetools import TTLCache
|
|
||||||
from telebot.types import Message
|
from telebot.types import Message
|
||||||
|
|
||||||
import settings
|
|
||||||
from mongo import mongo
|
from mongo import mongo
|
||||||
|
from storage import set_values, get_chat_info
|
||||||
|
|
||||||
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
||||||
|
|
||||||
@ -30,46 +29,12 @@ answers = [
|
|||||||
[{"200", "двести"}, "Отсоси на месте!"],
|
[{"200", "двести"}, "Отсоси на месте!"],
|
||||||
[{"слышь", "слыш"}, "За углом поссышь!"],
|
[{"слышь", "слыш"}, "За углом поссышь!"],
|
||||||
[{"здрасте", "здрасьте"}, "Пизду покрасьте!"],
|
[{"здрасте", "здрасьте"}, "Пизду покрасьте!"],
|
||||||
[{"ладно"}, "Прохладно!"]
|
[{"ладно"}, "Прохладно!"],
|
||||||
|
[{"угу"}, "Иди в пизду!"],
|
||||||
|
[{"опа"}, "Срослась пизда и жопа!"]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
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'])
|
@bot.message_handler(commands=['setprobability'])
|
||||||
def set_probability(message: Message):
|
def set_probability(message: Message):
|
||||||
bot.send_message(message.chat.id, "Отправь одно число - вероятность парирования")
|
bot.send_message(message.chat.id, "Отправь одно число - вероятность парирования")
|
||||||
|
39
storage.py
Normal file
39
storage.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from cachetools import TTLCache
|
||||||
|
|
||||||
|
import settings
|
||||||
|
from mongo import mongo
|
||||||
|
|
||||||
|
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})
|
Loading…
Reference in New Issue
Block a user