Файл Main
import logging
from aiogram import Bot, Dispatcher, executor, types
import config as cfg
import markups as nav
from db import Database
logging.basicConfig(level=logging.INFO)
bot = Bot(token=cfg.TOKEN)
dp = Dispatcher(bot)
database = Database('database.db')
@dp.message_handler(commands=["start"])
async def start(message: types.Message):
if message.chat.type == "private":
if not db.user_exsists(message.from_user,id):
start_command = message.text
referrer_id = str(start_command[7:])
if str(referrer_id) != "" :
if str(referrer_id) != str(message.from_user.id):
db.add_user(message.from_user.id, referrer_id)
try:
await bot.send_message(referrer_id, "No")
except:
pass
else:
db.add_user(message.from_user.id)
await bot.send_message(message.from_user.id, "Yes")
else:
db.add_user(message.from_user.id)
await bot.send_message(message.from_user.id, "Hi", reply_markup=nav.mainMenu)
@dp.message_handler()
async def bot_message(message: types.Message):
if message.chat.type == "private":
if message.text == "Профиль":
await bot.send_message(message.from_user.id, f"ID: {message.from_user.id}\nhttps://t.me/{cfg.BOT_NICNAME}?start={message.from_user.id}\nКол-во рефералов: {dp.count_reeferals(message.from_user.id)}")
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
Файл db
import sqlite3
class Database:
def __init__(self, db_file):
self.connection = sqlite3.connect(db_file)
self.cursor = self.connection.cursor()
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 add_user(self, user_id, referrer_id=None):
with self.connection:
if referrer_id is not None:
return self.cursor.execute("INSERT INTO users (user_id, referrer_id) VALUES (?, ?)", (user_id, referrer_id))
else:
return self.cursor.execute("INSERT INTO users (user_id) VALUES (?)", (user_id,))
def count_referrers(self, user_id):
with self.connection:
return self.cursor.execute("SELECT COUNT('id') as count FROM users WHERE referrer_id = ?", (user_id,)).fetchone()[0]
Ошибка AttributeError: 'Dispatcher' object has no attribute 'count_reeferals'