@IllIDan3

Счетчик реферальной системы выдает ошибку, как исправить?

main.py:
elif message.text == 'Ваши рефералы':
    count = Users.get_ref_count(message.chat.id)
    bot.reply_to(message, text=f'Count: {count}')


models.py
from peewee import *

db = SqliteDatabase('users.db')


class BaseModel(Model):
    class Meta:
        database = db


class Users(BaseModel):
    user_id = IntegerField(unique=True)
    ref = IntegerField(default=0)

    @classmethod
    def get_user(cls, user_id):
        return cls.get(user_id == user_id)

    @classmethod
    def get_ref_count(cls, user_id):
        return cls.get(Users.user_id == user_id).ref

    @classmethod
    def increase_ref_count(cls, user_id):
        user = cls.get_user(user_id)
        user.ref += 1
        user.save()

    @classmethod
    def user_exists(cls, user_id):
        query = cls().select().where(cls.user_id == user_id)
        return query.exists()

    @classmethod
    def create_user(cls, user_id):
        user, created = cls.get_or_create(user_id=user_id)

#db.create_tables([Users])

вот ошибка:
Traceback (most recent call last):
File "/home/runner/hz/venv/lib/python3.8/site-packages/peewee.py", line 6970, in get
return clone.execute(database)[0]
File "/home/runner/hz/venv/lib/python3.8/site-packages/peewee.py", line 4339, in __getitem__
return self.row_cache[item]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main.py", line 75, in
bot.polling(none_stop=True)
File "/home/runner/hz/venv/lib/python3.8/site-packages/telebot/__init__.py", line 620, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "/home/runner/hz/venv/lib/python3.8/site-packages/telebot/__init__.py", line 694, in __threaded_polling
raise e
File "/home/runner/hz/venv/lib/python3.8/site-packages/telebot/__init__.py", line 650, in __threaded_polling
self.worker_pool.raise_exceptions()
File "/home/runner/hz/venv/lib/python3.8/site-packages/telebot/util.py", line 143, in raise_exceptions
raise self.exception_info
File "/home/runner/hz/venv/lib/python3.8/site-packages/telebot/util.py", line 89, in run
task(*args, **kwargs)
File "main.py", line 54, in new_func
count = Users.get_ref_count(message.chat.id)
File "/home/runner/hz/models.py", line 21, in get_ref_count
return cls.get(Users.user_id == user_id).ref
File "/home/runner/hz/venv/lib/python3.8/site-packages/peewee.py", line 6522, in get
return sq.get()
File "/home/runner/hz/venv/lib/python3.8/site-packages/peewee.py", line 6973, in get
raise self.model.DoesNotExist('%s instance matching query does '
models.UsersDoesNotExist: instance matching query does not exist:
SQL: SELECT "t1"."id", "t1"."user_id", "t1"."ref" FROM "users" AS "t1" WHERE ("t1"."user_id" = ?) LIMIT ? OFFSET ?
Params: [1870006389, 1, 0]
  • Вопрос задан
  • 77 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы