WIP: Wallet Cog and i18n

This commit is contained in:
Profitroll 2025-02-23 23:41:42 +01:00
parent 1c8365b11f
commit fcb09303ec
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
5 changed files with 99 additions and 1 deletions

15
classes/bot_locale.py Normal file
View File

@ -0,0 +1,15 @@
import logging
from logging import Logger
from typing import override, Any
from libbot.i18n.classes import BotLocale as LibBotLocale
logger: Logger = logging.getLogger(__name__)
class BotLocale(LibBotLocale):
@override
def _(self, key: str, *args: str, locale: str | None = None) -> Any:
locale = None if locale is None else locale.split("-")[0]
super()._(key, *args, locale=locale)

View File

@ -1,3 +1,4 @@
from pathlib import Path
from typing import Any
from aiohttp import ClientSession
@ -6,6 +7,8 @@ from libbot.cache.manager import create_cache_client
from libbot.pycord.classes import PycordBot as LibPycordBot
from classes import PycordUser
from classes.bot_locale import BotLocale
# from modules.tracking.dhl import update_tracks_dhl
@ -21,6 +24,13 @@ class PycordBot(LibPycordBot):
if self.scheduler is None:
return
self.bot_locale: BotLocale = BotLocale(
default_locale=self.config["locale"],
locales_root=(Path("locale")),
)
self._ = self.bot_locale._
# Scheduler job for DHL parcel tracking
# self.scheduler.add_job(
# update_tracks_dhl,

View File

@ -1,5 +1,70 @@
import logging
from logging import Logger
from discord import ApplicationContext, SlashCommandGroup, option, User
from discord.ext import commands
from classes.errors import WalletInsufficientFunds
from classes.pycord_bot import PycordBot
from classes.wallet import Wallet
logger: Logger = logging.getLogger(__name__)
class WalletCog(commands.Cog):
def __init__(self, client: PycordBot):
self.client: PycordBot = client
command_group: SlashCommandGroup = SlashCommandGroup("wallet", "Wallet management")
@command_group.command(
name="balance",
description="View wallet's balance",
)
@option("user", description="User whose balance to check (if not your own)", required=False)
async def command_wallet_balance(self, ctx: ApplicationContext, user: User = None) -> None:
wallet: Wallet = await Wallet.from_id(
ctx.user.id if not user else user.id, ctx.guild_id
)
# TODO Replace with BotLocale calls
await ctx.respond(
f"{'Your' if user is None else user.display_name + '\'s'} balance is `{wallet.balance}`"
)
@command_group.command(
name="transfer",
description="View wallet's balance",
)
@option("user", description="Recipient")
@option("amount", description="Amount", min_value=0.01)
async def command_wallet_transfer(
self, ctx: ApplicationContext, user: User, amount: float
) -> None:
amount = round(amount, 2)
# Guild will be needed for overdraft options
# guild: PycordGuild = await PycordGuild.from_id(ctx.guild_id)
wallet: Wallet = await Wallet.from_id(ctx.user.id, ctx.guild_id)
try:
await wallet.transfer(user.id, ctx.guild_id, amount)
except WalletInsufficientFunds:
# TODO Replace with valid BotLocale calls
await ctx.respond(
self.client._(
"transfer_insufficient_funds", "messages", "wallet", locale=ctx.locale
).format(amount=round(abs(wallet.balance - amount), 2))
)
return
await ctx.respond(
# TODO Replace with valid BotLocale calls
self.client._("transfer_success", "messages", "wallet", locale=ctx.locale).format(
amount=amount, recipient=user.display_name
)
)
def setup(client: PycordBot) -> None:
pass
client.add_cog(WalletCog(client))

View File

@ -1,5 +1,9 @@
{
"messages": {
"wallet": {
"transfer_success": "You have transferred `{amount}` to `{recipient}`.",
"transfer_insufficient_funds": "Insufficient funds. `{amount}` more is needed for this transaction."
},
"welcome": {
"morning": [
"{0} Good morning and welcome! {1}",

View File

@ -1,5 +1,9 @@
{
"messages": {
"wallet": {
"transfer_success": "Ви перевели `{amount}` на рахунок `{recipient}`.",
"transfer_insufficient_funds": "Недостатньо коштів. Потрібно ще `{amount}` для цієї транзакції."
},
"welcome": {
"morning": [
"{0} Добрий ранок та ласкаво просимо! {1}",