74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
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
|
|
)
|
|
|
|
await ctx.respond(
|
|
self.client._("balance_own", "messages", "wallet", locale=ctx.locale).format(
|
|
balance=wallet.balance
|
|
)
|
|
if user is None
|
|
else self.client._("balance_user", "messages", "wallet", locale=ctx.locale).format(
|
|
balance=wallet.balance, user=user.display_name
|
|
)
|
|
)
|
|
|
|
@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:
|
|
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(
|
|
self.client._("transfer_success", "messages", "wallet", locale=ctx.locale).format(
|
|
amount=amount, recipient=user.display_name
|
|
)
|
|
)
|
|
|
|
|
|
def setup(client: PycordBot) -> None:
|
|
client.add_cog(WalletCog(client))
|