Compare commits

..

2 commits

Author SHA1 Message Date
707eef30a5 Добавлен ответ на сообщение боту без текста.
Исправлен подсчет сообщений без текста в VK.
2026-01-19 16:03:42 +03:00
bb16256ecc Включена опция autocommit. 2026-01-19 16:01:46 +03:00
4 changed files with 12 additions and 20 deletions

View file

@ -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) self.conn = connect(connection_string, autocommit=True)
self.cursor = self.conn.cursor() self.cursor = self.conn.cursor()
def get_chats(self): def get_chats(self):
@ -17,16 +17,13 @@ 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)
@ -38,14 +35,12 @@ 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'])
@ -53,18 +48,13 @@ 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("""
@ -104,17 +94,11 @@ 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)

View file

@ -2,6 +2,7 @@ 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}!'

View file

@ -8,6 +8,7 @@ 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
@ -75,6 +76,10 @@ 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']

View file

@ -8,6 +8,7 @@ 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
@ -25,9 +26,6 @@ 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
@ -75,6 +73,10 @@ 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']