Traceback (most recent call last):
File "d:\works\haosy_bot\data_basy\main.py", line 4, in <module>
from db import Database
ImportError: cannot import name 'Database' from 'db' (d:\works\haosy_bot\data_basy\db.py)
import logging
from aiogram import Bot, Dispatcher, executor, types
import markups as nav
from db import Database
TOKEN = "6584883985:AAGxZjDJxnPchMxcNgOdA2mgOXvMjebakH4" # Замените на свой токен
logging.basicConfig(level=logging.INFO)
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
db = Database("haosy_data_basy")
@dp.message_handler(commands=["start"])
async def start(message: types.Message):
if db.user_exist(message.from_user.id):
db.add_user(message.from_user.id, "Укажите свой ник")
else:
await bot.send_message(message.from_user.id, "Вы уже зарегистрированы!", reply_markup=nav.mainMenu)
@dp.message_handler()
async def bot_message(message: types.Message):
if message.chat.type == "private":
if message.text == "Профиль":
pass
else:
if db.get_signup(message.from_user.id) == "setnickname":
if len(message.text) > 15:
await bot.send_message(message.from_user.id, "Никнейм не должен превышать 15 символов")
elif "@" in message.text or "/" in message.text:
await bot.send_message(message.from_user.id, "Вы ввели запрещенный символ")
else:
db.set_nickname(message.from_user.id, message.text)
db.set_signup(message.from_user.id, "done")
await bot.send_message(message.from_user.id, "Регистрация прошла успешно!", reply_markup=nav.mainMenu)
if __name__ == "__main__":
from aiogram import executor
executor.start_polling(dp, skip_updates=True)
bot.polling(none_stop=True)
import sqlite3
class Database :
def __init__(self, db_file):
self.connection = sqlite3.connect(db_file)
self.cursor = self.connection.cursor()
def add_user(self, user_id):
with self.connection:
self.cursor.execute("INSERT INTO users (user_id) VALUES (?)", (user_id,))
self.connection.commit()
def user_exists(self, user_id):
with self.connection:
result = self.cursor.execute("SELECT * FROM users WHERE user_id = ?", (user_id,)).fetchall()
return bool(len(result))
def set_nickname(self, user_id, nickname):
with self.connection:
return self.cursor.execute("UPDATE users SET nickname = ? WHERE user_id = ?", (nickname, user_id))
def get_signup(self, user_id):
with self.connection:
result = self.cursor.execute("SELECT signup FROM users WHERE user_id = ?", (user_id,)).fetchall()
for row in result:
signup = str(row[0])
return signup
def set_signup(self, user_id, signup):
with self.connection:
return self.cursor.execute("UPDATE users SET signup = ? WHERE user_id = ?", (signup, user_id))