pizda-bot/main.py
2022-12-17 16:53:03 +03:00

96 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"))
all_letters = "йцукенгшщзхъёфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪЁФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm "
answers_rus = {"да", "дa"}
answers_eng = {"da", "dа"}
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:
ans = "Пизда!"
if convert_text in answers_eng:
ans = "Pizda!"
if convert_text in answers_net_rus:
ans = "Пидора ответ!"
if convert_text in answers_net_eng:
ans = "Pidora otvet!"
if ans is not None and randrange(1, 101) <= info["probability"]:
bot.reply_to(message, ans)
bot.polling()