This commit is contained in:
wt
2022-10-20 07:58:55 +07:00
parent 76709cbdc7
commit 6ad2e596d9
6 changed files with 74 additions and 0 deletions
BIN
View File
Binary file not shown.
+4
View File
@@ -0,0 +1,4 @@
from peewee import SqliteDatabase
# подключение к базе данных
conn = SqliteDatabase('database.sqlite')
+28
View File
@@ -0,0 +1,28 @@
from peewee import *
from .base import conn
# базовая можель от которой наследуются остальные
class BaseModel(Model):
class Meta:
database = conn
# модель пользователя
class User(BaseModel):
id = AutoField(column_name='UserId')
username = CharField(column_name='Username', max_length=100, null=False)
balance = FloatField(column_name='Balance', null=False, default=0)
class Meta:
table_name = 'User'
class Transaction(BaseModel):
id = AutoField(column_name='TransactionId')
type = CharField(column_name='TransactionType', max_length=10, null=False, default='incom')
sum = FloatField(column_name='TransactionSum', null=False, default=0)
user = ForeignKeyField(User, unique=True)
conn.create_tables([User, Transaction])
+6
View File
@@ -7,6 +7,9 @@ ru = {
/help - показать этот список.
/ru - русский язык
/en - английский язык
/balance - текущий баланс
/add или + - добавить доход
/rm или - - добавить расход
''',
'error404_message': 'Я не могу это сделать.'
}
@@ -18,6 +21,9 @@ Hi i am finance bot and i can do this:
/help - open this list
/ru - russian language
/en - english language
/balance - balance
/add или + - add incom
/rm или - - add consuption
''',
'error404_message': 'I can\'t do this.'
}
+35
View File
@@ -2,6 +2,11 @@ from termcolor import cprint
from bot import bot
import locale
from changelocale import change_locale
from database.models import User
def to_fixed(num_obj, digits=0):
return f"{num_obj:.{digits}f}"
def main_bot(messages):
@@ -12,9 +17,39 @@ def main_bot(messages):
mes = locale.ru['help_message']
elif locale.LOCALE == 'en':
mes = locale.en['help_message']
_, user = User.get_or_create(username=message.chat.id)
bot.send_message(message.chat.id, mes)
elif message.text.split(' ')[0] == '/ru' or message.text.split(' ')[0] == '/en':
change_locale(message)
elif message.text.split(' ')[0] == '/balance':
user = User.get(username=message.chat.id)
bot.send_message(message.chat.id, to_fixed(user.balance, 2))
elif message.text.split(' ')[0] == '/add' or message.text.split(' ')[0] == '+':
if len(message.text.split(' ')) < 2:
mes = None
if locale.LOCALE == 'ru':
mes = 'Используйте /add [сумма] или + [сумма]'
elif locale.LOCALE == 'en':
mes = 'Use /add [sum] or + [sum]'
bot.send_message(message.chat.id, mes)
else:
user = User.get(username=message.chat.id)
user.balance += float(message.text.split(' ')[1])
user.save()
bot.send_message(message.chat.id, to_fixed(user.balance, 2))
elif message.text.split(' ')[0] == '/rm' or message.text.split(' ')[0] == '-':
if len(message.text.split(' ')) < 2:
mes = None
if locale.LOCALE == 'ru':
mes = 'Используйте /rm [сумма] или - [сумма]'
elif locale.LOCALE == 'en':
mes = 'Use /rm [sum] or - [sum]'
bot.send_message(message.chat.id, mes)
else:
user = User.get(username=message.chat.id)
user.balance -= float(message.text.split(' ')[1])
user.save()
bot.send_message(message.chat.id, to_fixed(user.balance, 2))
else:
mes = None
if locale.LOCALE == 'ru':
+1
View File
@@ -1,6 +1,7 @@
certifi==2022.9.24
charset-normalizer==2.1.1
idna==3.4
peewee==3.15.3
pyTelegramBotAPI==4.7.0
requests==2.28.1
termcolor==2.0.1