add database

This commit is contained in:
Rykov7
2023-02-25 19:33:39 +03:00
parent d0cf0762c8
commit fd069a9615
7 changed files with 88 additions and 54 deletions
+9 -9
View File
@@ -57,8 +57,8 @@ def start(message: types.Message):
send_or_reply(message, "Начни с прочтения", reply_markup=markup)
@bot.message_handler(commands=["rules", "rule", "r", "правила", "правило", "п"])
def send_rules(message):
@bot.message_handler(commands=["rules", "rule", f"r", "правила", "правило", "п"])
def send_rules(message: types.Message):
markup = types.InlineKeyboardMarkup([[RULES]], 1)
args = message.text.split()
if len(args) > 1 and args[-1].isdigit() and 0 < int(args[-1]):
@@ -72,19 +72,19 @@ def send_rules(message):
@bot.message_handler(commands=["faq", "чзв"])
def send_faq(message):
def send_faq(message: types.Message):
markup = types.InlineKeyboardMarkup([[FAQ]], 1)
send_or_reply(message, "...", reply_markup=markup)
@bot.message_handler(commands=["lib", "library", "books", "книги", "библиотека"])
def send_lib(message):
def send_lib(message: types.Message):
markup = types.InlineKeyboardMarkup([[LIB]], 1)
send_or_reply(message, "...", reply_markup=markup)
@bot.message_handler(commands=["lutz", "лутц"])
def send_lutz(message):
def send_lutz(message: types.Message):
bot.send_document(
message.chat.id,
document="BQACAgQAAxkBAAPBYsWJG9Ml0fPrnbU9UyzTQiQSuHkAAjkDAAIstCxSkuRbXAlcqeQpBA",
@@ -93,7 +93,7 @@ def send_lutz(message):
@bot.message_handler(commands=["bdmtss", "бдмтсс"])
def send_bdmtss_audio(message):
def send_bdmtss_audio(message: types.Message):
bot.send_voice(
message.chat.id,
"AwACAgIAAxkBAAIJrWOg2WUvLwrf7ahyJxQHB8_nqllwAAL5JQAC2_IJSbhfQIO5YnVmLAQ",
@@ -101,7 +101,7 @@ def send_bdmtss_audio(message):
@bot.message_handler(commands=["tr", "тр"])
def translate_layout(message):
def translate_layout(message: types.Message):
if message.reply_to_message and message.reply_to_message.text:
if message.reply_to_message.text[0] in RUS:
bot.send_message(
@@ -114,8 +114,8 @@ def translate_layout(message):
@bot.message_handler(commands=["quote", "цитата"])
def stan_speak(message):
bot.send_message(message.chat.id, stan.speak(0))
def stan_speak(message: types.Message):
bot.send_message(message.chat.id, stan.speak(0, message.chat.id))
@bot.message_handler(commands=["tsya", "тся", "ться"])
+2 -1
View File
@@ -23,6 +23,7 @@ bot = TeleBot(
allow_sending_without_reply=True,
colorful_logs=True,
)
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
@@ -70,7 +71,7 @@ FAQ = types.InlineKeyboardButton(
)
LIB = types.InlineKeyboardButton("📚 Книги", url="https://telegra.ph/what-to-read-10-06")
SPAM = ["me.sv/", "tg.sv/", "goo.by/", "go.sv/", "intim.video/", "uclck.ru/"]
SPAM = ["me.sv/", "tg.sv/", "goo.by/", "go.sv/", "intim.video/", "uclck.ru/", 'П0 ВСЕМ В0ПР0С0М']
NON_GRATA = ["дудар", "хауди", "dudar"]
BAN_WORDS = ["GREEN"]
+13
View File
@@ -0,0 +1,13 @@
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from .models import Base
basedir = os.path.abspath(os.path.dirname(__file__))
engine = create_engine('sqlite:///' + os.path.join(basedir, 'db/db.sqlite'))
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
+30
View File
@@ -0,0 +1,30 @@
from sqlalchemy import Column, String, Integer, Boolean, ForeignKey
from sqlalchemy.orm import relationship, declarative_base
Base = declarative_base()
class Quote(Base):
__tablename__ = "quotes"
id = Column(Integer, primary_key=True)
chat_id = Column(Integer, ForeignKey('chats.chat_id'))
text = Column(String)
def __repr__(self):
return f"Quote({self.text=}, {self.chat_id=})"
class Chat(Base):
__tablename__ = "chats"
id = Column(Integer, primary_key=True)
chat_id = Column(Integer, unique=True)
title = Column(String)
# Settings
quotes = relationship('Quote', backref='chat')
antispam = Column(Boolean)
report = Column(Boolean)
reminder = Column(Boolean)
def __repr__(self):
return f'Chat({self.chat_id=}, {self.title=})'
+33 -37
View File
@@ -4,14 +4,14 @@ import threading
from time import sleep
from .config import bot, types
from .filters import is_white, is_nongrata
from .database import session
from .models import Quote
def speak(chance_of):
def speak(chance_of, group_id):
number = random.randint(0, chance_of)
if number == 0:
return random.choice(
[i.rstrip() for i in open("Stan.txt", "r", encoding="utf8") if i]
)
return random.choice([i[0] for i in session.query(Quote.text).filter(Quote.chat_id == group_id).all()])
def send_quote(after_sec, message, quote):
@@ -25,8 +25,8 @@ def send_quote(after_sec, message, quote):
bot.send_message(message.chat.id, quote)
def act(message):
quote = speak(50)
def act(message: types.Message):
quote = speak(50, message.chat.id)
if quote:
threading.Thread(
target=send_quote, args=(len(quote) * 0.13, message, quote)
@@ -34,49 +34,45 @@ def act(message):
@bot.message_handler(func=is_white, commands=["add"])
def add_stan_quote(message):
def add_stan_quote(message: types.Message):
if message.reply_to_message and message.reply_to_message.text:
with open("Stan.txt", "a", encoding="utf8") as stan_quotes:
if message.reply_to_message.text not in (
i.rstrip() for i in open("Stan.txt", "r", encoding="utf8")
):
stan_quotes.write(
message.reply_to_message.text.replace("\n", " ") + "\n"
)
bot.send_message(
if message.reply_to_message.text not in [i[0] for i in session.query(Quote.text).all()]:
session.add(Quote(chat_id=message.chat.id, text=message.reply_to_message.text.replace("\n", " ")))
session.commit()
bot.send_message(
message.chat.id,
"✅ <b>Добавил</b>\n └ <i>"
+ message.reply_to_message.text.replace("\n", " ")
+ "</i>",
)
else:
bot.send_message(
message.chat.id,
f"⛔️ <b>Не добавил</b>, есть токое\n └ <i>{message.reply_to_message.text}</i>",
)
@bot.message_handler(func=is_white, commands=["remove"])
def remove_stan_quote(message):
if message.reply_to_message and message.reply_to_message.text:
if message.reply_to_message.text in (
i.rstrip() for i in open("Stan.txt", "r", encoding="utf8")
):
quotes = list(open("Stan.txt", "r", encoding="utf8"))
with open("Stan.txt", "w", encoding="utf8") as stan_quotes:
quotes.remove(message.reply_to_message.text + "\n")
stan_quotes.writelines(quotes)
bot.send_message(
message.chat.id,
f"✅ <b>Удалил</b>\n └ <i>{message.reply_to_message.text}</i>",
)
else:
bot.send_message(
message.chat.id,
f"⛔️ <b>Нет такого</b>\n └ <i>{message.reply_to_message.text}</i>",
f"⛔️ <b>Не добавил</b>, есть токое\n └ <i>{message.reply_to_message.text}</i>",
)
# @bot.message_handler(func=is_white, commands=["remove"])
# def remove_stan_quote(message):
# if message.reply_to_message and message.reply_to_message.text:
# if message.reply_to_message.text in (
# i.rstrip() for i in open("Stan.txt", "r", encoding="utf8")
# ):
# quotes = list(open("Stan.txt", "r", encoding="utf8"))
# with open("Stan.txt", "w", encoding="utf8") as stan_quotes:
# quotes.remove(message.reply_to_message.text + "\n")
# stan_quotes.writelines(quotes)
# bot.send_message(
# message.chat.id,
# f"✅ <b>Удалил</b>\n └ <i>{message.reply_to_message.text}</i>",
# )
# else:
# bot.send_message(
# message.chat.id,
# f"⛔️ <b>Нет такого</b>\n └ <i>{message.reply_to_message.text}</i>",
# )
@bot.message_handler(func=is_nongrata)
def tease_nongrata(message: types.Message):
"""Reply to non grata mentions."""