Compare commits
No commits in common. "707eef30a5699a3ddc4f3f3f1576d7799cb51068" and "0111ddecca0ba377646ffa147b8a1ecfd8cf967b" have entirely different histories.
707eef30a5
...
0111ddecca
4 changed files with 20 additions and 12 deletions
18
database.py
18
database.py
|
|
@ -4,7 +4,7 @@ from typing import List, Union
|
||||||
|
|
||||||
class BasicDatabase:
|
class BasicDatabase:
|
||||||
def __init__(self, connection_string: str):
|
def __init__(self, connection_string: str):
|
||||||
self.conn = connect(connection_string, autocommit=True)
|
self.conn = connect(connection_string)
|
||||||
self.cursor = self.conn.cursor()
|
self.cursor = self.conn.cursor()
|
||||||
|
|
||||||
def get_chats(self):
|
def get_chats(self):
|
||||||
|
|
@ -17,13 +17,16 @@ class BasicDatabase:
|
||||||
|
|
||||||
def add_chat(self, chat_id: int):
|
def add_chat(self, chat_id: int):
|
||||||
self.cursor.execute("INSERT INTO chats (id) VALUES (?)", chat_id)
|
self.cursor.execute("INSERT INTO chats (id) VALUES (?)", chat_id)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def chat_update(self, chat_id: int, **kwargs):
|
def chat_update(self, chat_id: int, **kwargs):
|
||||||
self.cursor.execute("UPDATE chats SET " + ", ".join(f + " = ?" for f in kwargs) +
|
self.cursor.execute("UPDATE chats SET " + ", ".join(f + " = ?" for f in kwargs) +
|
||||||
" WHERE id = ?", list(kwargs.values()) + [chat_id])
|
" WHERE id = ?", list(kwargs.values()) + [chat_id])
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def chat_delete(self, chat_id: int):
|
def chat_delete(self, chat_id: int):
|
||||||
self.cursor.execute("DELETE FROM chats WHERE id = ?", chat_id)
|
self.cursor.execute("DELETE FROM chats WHERE id = ?", chat_id)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def get_user(self, chat_id: int, user_id: int):
|
def get_user(self, chat_id: int, user_id: int):
|
||||||
self.cursor.execute("SELECT * FROM users WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
self.cursor.execute("SELECT * FROM users WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
||||||
|
|
@ -35,12 +38,14 @@ class BasicDatabase:
|
||||||
|
|
||||||
def add_user(self, chat_id: int, user_id: int):
|
def add_user(self, chat_id: int, user_id: int):
|
||||||
self.cursor.execute("INSERT INTO users (chat_id, user_id) VALUES (?, ?)", chat_id, user_id)
|
self.cursor.execute("INSERT INTO users (chat_id, user_id) VALUES (?, ?)", chat_id, user_id)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def user_set_last_message(self, chat_id: int, user_id: int, last_message: int):
|
def user_set_last_message(self, chat_id: int, user_id: int, last_message: int):
|
||||||
self.user_update(chat_id, user_id, last_message=last_message)
|
self.user_update(chat_id, user_id, last_message=last_message)
|
||||||
|
|
||||||
def user_increment_messages(self, chat_id: int, user_id: int):
|
def user_increment_messages(self, chat_id: int, user_id: int):
|
||||||
self.user_increment(chat_id, user_id, ['messages_today', 'messages_month'])
|
self.user_increment(chat_id, user_id, ['messages_today', 'messages_month'])
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def user_increment_warnings(self, chat_id: int, user_id: int):
|
def user_increment_warnings(self, chat_id: int, user_id: int):
|
||||||
self.user_increment(chat_id, user_id, ['warnings'])
|
self.user_increment(chat_id, user_id, ['warnings'])
|
||||||
|
|
@ -48,13 +53,18 @@ class BasicDatabase:
|
||||||
def user_increment(self, chat_id: int, user_id: int, fields: List[str]):
|
def user_increment(self, chat_id: int, user_id: int, fields: List[str]):
|
||||||
self.cursor.execute("UPDATE users SET " + ", ".join(f + " = " + f + " + 1" for f in fields) +
|
self.cursor.execute("UPDATE users SET " + ", ".join(f + " = " + f + " + 1" for f in fields) +
|
||||||
" WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
" WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def user_update(self, chat_id: int, user_id: int, **kwargs):
|
def user_update(self, chat_id: int, user_id: int, **kwargs):
|
||||||
|
sql = "UPDATE users SET " + ", ".join(f + " = ?" for f in kwargs) + " WHERE chat_id = ? AND user_id = ?"
|
||||||
|
args = [chat_id, user_id] + list(kwargs.values())
|
||||||
self.cursor.execute("UPDATE users SET " + ", ".join(f + " = ?" for f in kwargs) +
|
self.cursor.execute("UPDATE users SET " + ", ".join(f + " = ?" for f in kwargs) +
|
||||||
" WHERE chat_id = ? AND user_id = ?", list(kwargs.values()) + [chat_id, user_id])
|
" WHERE chat_id = ? AND user_id = ?", list(kwargs.values()) + [chat_id, user_id])
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def delete_user(self, chat_id: int, user_id: int):
|
def delete_user(self, chat_id: int, user_id: int):
|
||||||
self.cursor.execute("DELETE FROM users WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
self.cursor.execute("DELETE FROM users WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def get_top_messages_today(self, chat_id: int):
|
def get_top_messages_today(self, chat_id: int):
|
||||||
self.cursor.execute("""
|
self.cursor.execute("""
|
||||||
|
|
@ -94,11 +104,17 @@ class BasicDatabase:
|
||||||
""", chat_id)
|
""", chat_id)
|
||||||
return self._to_dict(self.cursor.fetchall())
|
return self._to_dict(self.cursor.fetchall())
|
||||||
|
|
||||||
|
def delete_users(self, chat_id: int):
|
||||||
|
self.cursor.execute("DELETE FROM users WHERE chat_id = ?", chat_id)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def reset_messages_today(self):
|
def reset_messages_today(self):
|
||||||
self.cursor.execute("UPDATE users SET messages_today = 0")
|
self.cursor.execute("UPDATE users SET messages_today = 0")
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def reset_messages_month(self):
|
def reset_messages_month(self):
|
||||||
self.cursor.execute("UPDATE users SET messages_month = 0")
|
self.cursor.execute("UPDATE users SET messages_month = 0")
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def create_chat_if_not_exists(self, chat_id: int):
|
def create_chat_if_not_exists(self, chat_id: int):
|
||||||
chat = self.get_chat(chat_id)
|
chat = self.get_chat(chat_id)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ MESSAGE_CHAT_NOT_ACTIVE = 'Извините, но я пока не работа
|
||||||
MESSAGE_PERMISSION_DENIED = 'Извините, но о таком меня может попросить только администратор чата.'
|
MESSAGE_PERMISSION_DENIED = 'Извините, но о таком меня может попросить только администратор чата.'
|
||||||
MESSAGE_NEED_REPLY = 'Извините, но эту команду нужно вызывать в ответ на текстовое сообщение.'
|
MESSAGE_NEED_REPLY = 'Извините, но эту команду нужно вызывать в ответ на текстовое сообщение.'
|
||||||
MESSAGE_NEED_REPLY_OR_FORWARD = 'Извините, но эту команду нужно вызывать в ответ на текстовое сообщение или с пересылкой текстовых сообщений.'
|
MESSAGE_NEED_REPLY_OR_FORWARD = 'Извините, но эту команду нужно вызывать в ответ на текстовое сообщение или с пересылкой текстовых сообщений.'
|
||||||
MESSAGE_NOT_TEXT = 'Извините, но я понимаю только текст.'
|
|
||||||
MESSAGE_DEFAULT_RULES = 'Правила не установлены. Просто ведите себя хорошо.'
|
MESSAGE_DEFAULT_RULES = 'Правила не установлены. Просто ведите себя хорошо.'
|
||||||
MESSAGE_DEFAULT_CHECK_RULES = 'Правила чата не установлены. Проверка невозможна.'
|
MESSAGE_DEFAULT_CHECK_RULES = 'Правила чата не установлены. Проверка невозможна.'
|
||||||
MESSAGE_DEFAULT_GREETING_JOIN = 'Добро пожаловать, {name}!'
|
MESSAGE_DEFAULT_GREETING_JOIN = 'Добро пожаловать, {name}!'
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ from aiogram.enums.content_type import ContentType
|
||||||
|
|
||||||
import ai_agent
|
import ai_agent
|
||||||
import utils
|
import utils
|
||||||
from messages import *
|
|
||||||
|
|
||||||
import tg.tg_database as database
|
import tg.tg_database as database
|
||||||
from tg.utils import get_user_name_for_ai
|
from tg.utils import get_user_name_for_ai
|
||||||
|
|
@ -76,10 +75,6 @@ async def any_message_handler(message: Message):
|
||||||
ai_agent.Message(user_name=await get_user_name_for_ai(message.reply_to_message.from_user),
|
ai_agent.Message(user_name=await get_user_name_for_ai(message.reply_to_message.from_user),
|
||||||
text=message.reply_to_message.text)]
|
text=message.reply_to_message.text)]
|
||||||
|
|
||||||
if ai_message.text is None:
|
|
||||||
await message.reply(MESSAGE_NOT_TEXT)
|
|
||||||
return
|
|
||||||
|
|
||||||
ai_message.user_name = await get_user_name_for_ai(message.from_user)
|
ai_message.user_name = await get_user_name_for_ai(message.from_user)
|
||||||
chat_prompt = chat['ai_prompt']
|
chat_prompt = chat['ai_prompt']
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ from vkbottle_types.codegen.objects import GroupsGroup
|
||||||
|
|
||||||
import ai_agent
|
import ai_agent
|
||||||
import utils
|
import utils
|
||||||
from messages import *
|
|
||||||
|
|
||||||
import vk.vk_database as database
|
import vk.vk_database as database
|
||||||
from vk.utils import get_user_name_for_ai
|
from vk.utils import get_user_name_for_ai
|
||||||
|
|
@ -26,6 +25,9 @@ async def any_message_handler(message: Message):
|
||||||
if chat['active'] == 0:
|
if chat['active'] == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if len(message.text) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
# Игнорировать ботов
|
# Игнорировать ботов
|
||||||
if message.from_id < 0:
|
if message.from_id < 0:
|
||||||
return
|
return
|
||||||
|
|
@ -73,10 +75,6 @@ async def any_message_handler(message: Message):
|
||||||
ai_agent.Message(user_name=await get_user_name_for_ai(message.ctx_api, fwd_message.from_id),
|
ai_agent.Message(user_name=await get_user_name_for_ai(message.ctx_api, fwd_message.from_id),
|
||||||
text=fwd_message.text))
|
text=fwd_message.text))
|
||||||
|
|
||||||
if len(ai_message.text) == 0:
|
|
||||||
await message.reply(MESSAGE_NOT_TEXT)
|
|
||||||
return
|
|
||||||
|
|
||||||
ai_message.user_name = await get_user_name_for_ai(message.ctx_api, message.from_id)
|
ai_message.user_name = await get_user_name_for_ai(message.ctx_api, message.from_id)
|
||||||
chat_prompt = chat['ai_prompt']
|
chat_prompt = chat['ai_prompt']
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue