Skip to content

Commit

Permalink
Merge pull request #1
Browse files Browse the repository at this point in the history
Economics system improvements
  • Loading branch information
usbtypec1 authored Sep 24, 2023
2 parents 3576b9c + a19edff commit 5b9696d
Show file tree
Hide file tree
Showing 42 changed files with 612 additions and 67 deletions.
5 changes: 5 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ traces_sample_rate = 1.0 # 100% of traces to be sent to Sentry
cloud_name = ""
api_key = ""
api_secret = ""

[mirror]
is_enabled = true
chat_id = -1001938259721
ignored_chat_ids = []
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[tool.poetry]
name = "duck-duck-bot"
version = "1.8.0"
version = "1.9.0"
description = ""
authors = ["Eldos <[email protected]>"]
readme = "README.md"
packages = [{ include = "telegram_bot_template" }]

[tool.poetry.dependencies]
python = "^3.11"
aiogram = "^3.0.0"
aiogram = "^3.1.0"
redis = "^5.0.0"
pydantic = "^2.1.1"
sentry-sdk = "^1.29.2"
Expand Down
13 changes: 13 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
)


@dataclass(frozen=True, slots=True)
class MirrorConfig:
is_enabled: bool
chat_id: int
ignored_chat_ids: set[int]


@dataclass(frozen=True, slots=True)
class LoggingConfig:
level: int
Expand Down Expand Up @@ -50,6 +57,7 @@ class Config:
server_api_base_url: str
main_chat_id: int | str
timezone: ZoneInfo
mirror: MirrorConfig


def parse_config(config: Mapping) -> Config:
Expand All @@ -76,6 +84,11 @@ def parse_config(config: Mapping) -> Config:
server_api_base_url=config['server_api']['base_url'],
main_chat_id=config['main_chat_id'],
timezone=ZoneInfo(config['timezone']),
mirror=MirrorConfig(
is_enabled=config['mirror']['is_enabled'],
chat_id=config['mirror']['chat_id'],
ignored_chat_ids=set(config['mirror']['ignored_chat_ids']),
),
)


Expand Down
4 changes: 4 additions & 0 deletions src/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ class UserHasNoPremiumSubscriptionError(Exception):

class ThemeDoesNotExistError(ServerAPIError):
pass


class InsufficientFundsForWithdrawalError(ServerAPIError):
pass
2 changes: 2 additions & 0 deletions src/filters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .bot import *
from .secret_messages import *
from .text import *
from .themes import *
from .transfers import *
10 changes: 10 additions & 0 deletions src/filters/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from aiogram.types import Message, User

__all__ = ('reply_message_from_bot_filter',)


async def reply_message_from_bot_filter(
message: Message,
bot_user: User,
) -> bool:
return message.reply_to_message.from_user.id == bot_user.id
11 changes: 11 additions & 0 deletions src/filters/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from aiogram.types import Message

__all__ = ('integer_filter',)


def integer_filter(message: Message) -> bool | dict:
"""Filter that checks if message text is integer."""
try:
return {'number': int(message.text)}
except ValueError:
return False
4 changes: 4 additions & 0 deletions src/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from . import (
balance,
contacts,
food_menu,
secret_messages,
Expand All @@ -13,9 +14,11 @@
countdown,
premium,
server,
work,
)

__all__ = (
'balance',
'contacts',
'food_menu',
'secret_medias',
Expand All @@ -30,4 +33,5 @@
'countdown',
'premium',
'server',
'work',
)
1 change: 0 additions & 1 deletion src/handlers/anonymous_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from aiogram.fsm.context import FSMContext
from aiogram.types import Message

from exceptions import UserHasNoPremiumSubscriptionError
from models import User
from services import determine_media_file_id_and_answer_method
from states import AnonymousMessagingStates
Expand Down
29 changes: 29 additions & 0 deletions src/handlers/balance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from aiogram import Router, F
from aiogram.filters import StateFilter, Command
from aiogram.types import CallbackQuery, Message

from repositories import BalanceRepository
from views import UserBalanceView, render_message_or_callback_query

router = Router(name=__name__)


@router.message(
Command('balance'),
StateFilter('*'),
)
@router.callback_query(
F.data == 'show-user-balance',
StateFilter('*'),
)
async def on_show_user_balance(
message_or_callback_query: Message | CallbackQuery,
balance_repository: BalanceRepository,
) -> None:
user_id = message_or_callback_query.from_user.id
user_balance = await balance_repository.get_user_balance(user_id)
view = UserBalanceView(user_balance)
await render_message_or_callback_query(
message_or_callback_query=message_or_callback_query,
view=view,
)
4 changes: 1 addition & 3 deletions src/handlers/contacts/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

from callback_data import ContactDetailCallbackData
from repositories import ContactRepository
from repositories import HTTPClientFactory
from views import edit_message_by_view
from views.contacts import ContactDetailView
from views import edit_message_by_view, ContactDetailView

__all__ = ('register_handlers',)

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/contacts/update/is_hidden.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def on_toggle_is_hidden_status_command(
await contact_repository.update(
contact_id=contact_to_update.id,
public_name=contact_to_update.public_name,
private_name=contact.private_name,
private_name=contact_to_update.private_name,
is_hidden=is_hidden,
)
text = '🙈 Контакт скрыт' if is_hidden else '🙉 Контакт больше не скрыт'
Expand Down
27 changes: 25 additions & 2 deletions src/handlers/food_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,40 @@
from aiogram.filters import StateFilter, Command
from aiogram.types import Message, InputMediaPhoto

from services import get_food_menu_html, parse_food_menu_html
from exceptions import InsufficientFundsForWithdrawalError
from repositories import BalanceRepository
from services import (
get_food_menu_html, parse_food_menu_html,
PrivateChatNotifier
)

__all__ = ('router',)

router = Router(name=__name__)


async def on_show_food_menu(message: Message) -> None:
async def on_show_food_menu(
message: Message,
balance_repository: BalanceRepository,
private_chat_notifier: PrivateChatNotifier,
) -> None:
food_menu_html = await get_food_menu_html()
food_menu_items = parse_food_menu_html(food_menu_html)

try:
withdrawal = await balance_repository.create_withdrawal(
user_id=message.from_user.id,
amount=80,
description='Просмотр йемека на сегодня',
)
except InsufficientFundsForWithdrawalError:
await message.reply(
'❌ Недостаточно средств для списания\n'
'💸 Стоимость просмотра йемека: 80 дак-дак коинов'
)
return
await private_chat_notifier.send_withdrawal_notification(withdrawal)

caption: list[str] = [f'🍽️ <b>Меню на сегодня</b>\n']
for food_menu_item in food_menu_items:
caption.append(f'▻ {food_menu_item.name}')
Expand Down
4 changes: 1 addition & 3 deletions src/handlers/secret_medias/detail.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import re

from aiogram import Dispatcher, Router, F
from aiogram import Router, F
from aiogram.enums import ChatType
from aiogram.exceptions import TelegramAPIError
from aiogram.filters import StateFilter, CommandStart
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/teams/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from aiogram.types import CallbackQuery

from callback_data import TeamDeleteAskForConfirmationCallbackData
from repositories import HTTPClientFactory, TeamRepository
from repositories import TeamRepository
from states import TeamDeleteStates
from views import (
TeamDeleteAskForConfirmationView,
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/transfers/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aiogram.types import Message

from filters import transfer_operation_amount_filter
from repositories import TransferRepository, UserRepository
from repositories import TransferRepository, BalanceRepository

__all__ = ('register_handlers',)

Expand All @@ -25,12 +25,12 @@ async def on_create_transfer_in_group_chat(
message: Message,
amount: int,
transfer_repository: TransferRepository,
user_repository: UserRepository,
balance_repository: BalanceRepository,
) -> None:
sender_id = message.from_user.id
recipient_id = message.reply_to_message.from_user.id

sender_balance = await user_repository.get_balance(sender_id)
sender_balance = await balance_repository.get_user_balance(sender_id)
if sender_balance.balance < amount:
await message.reply('❌ Недостаточно средств на балансе')
return
Expand All @@ -41,7 +41,7 @@ async def on_create_transfer_in_group_chat(
)
await message.reply(f'✅ Перевод на сумму ${amount} успешно выполнен')

recipient_balance = await user_repository.get_balance(recipient_id)
recipient_balance = await balance_repository.get_user_balance(recipient_id)
with contextlib.suppress(TelegramAPIError):
await message.bot.send_message(
recipient_id,
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/users/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from aiogram.types import Message, CallbackQuery

from models import User
from repositories import UserRepository
from repositories import BalanceRepository
from services import is_anonymous_messaging_enabled
from views import (
UserSettingsCalledInGroupChatView,
Expand Down Expand Up @@ -41,11 +41,11 @@ async def on_show_settings(
message_or_callback_query: Message | CallbackQuery,
state: FSMContext,
user: User,
user_repository: UserRepository,
balance_repository: BalanceRepository,
) -> None:
await state.clear()
state_name = await state.get_state()
user_balance = await user_repository.get_balance(user_id=user.id)
user_balance = await balance_repository.get_user_balance(user.id)
view = UserMenuView(
user=user,
is_anonymous_messaging_enabled=is_anonymous_messaging_enabled(
Expand Down
2 changes: 0 additions & 2 deletions src/handlers/users/update/can_be_added_to_contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ async def on_toggle_can_be_added_to_contacts(
born_at=user.born_at,
)
user = await user_repository.get_by_id(user.id)

state_name = await state.get_state()
view = UserPersonalSettingsView(user)
await edit_message_by_view(message=callback_query.message, view=view)

Expand Down
2 changes: 0 additions & 2 deletions src/handlers/users/update/can_receive_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ async def on_toggle_can_receive_notifications(
born_at=user.born_at,
)
user = await user_repository.get_by_id(user.id)

state_name = await state.get_state()
view = UserPersonalSettingsView(user)
await edit_message_by_view(message=callback_query.message, view=view)

Expand Down
Loading

0 comments on commit 5b9696d

Please sign in to comment.