Use abbreviated numbers in award

pull/118/head
stijndcl 2022-07-01 14:05:00 +02:00
parent 96916d2abd
commit c294bc8da5
3 changed files with 19 additions and 9 deletions

View File

@ -1,3 +1,5 @@
import typing
import discord
from discord.ext import commands
@ -5,6 +7,7 @@ from database.crud import currency as crud
from database.exceptions.currency import DoubleNightly
from didier import Didier
from didier.utils.discord.checks import is_owner
from didier.utils.discord.converters import abbreviated_number
from didier.utils.types.string import pluralize
@ -19,17 +22,19 @@ class Currency(commands.Cog):
@commands.command(name="Award")
@commands.check(is_owner)
async def award(self, ctx: commands.Context, user: discord.User, amount: int):
async def award(self, ctx: commands.Context, user: discord.User, amount: abbreviated_number): # type: ignore
"""Award a user a given amount of Didier Dinks"""
amount = typing.cast(int, amount)
async with self.client.db_session as session:
await crud.add_dinks(session, user.id, amount)
plural = pluralize("Didier Dink", amount)
await ctx.reply(
f"**{ctx.author.display_name}** heeft **{user.display_name}** **{amount}** {plural} geschonken."
f"**{ctx.author.display_name}** heeft **{user.display_name}** **{amount}** {plural} geschonken.",
mention_author=False,
)
await self.client.confirm_message(ctx.message)
@commands.hybrid_command(name="bank")
@commands.hybrid_group(name="bank", case_insensitive=True, invoke_without_command=True)
async def bank(self, ctx: commands.Context):
"""Show your Didier Bank information"""
async with self.client.db_session as session:

View File

@ -0,0 +1 @@
from .numbers import *

View File

@ -2,6 +2,9 @@ import math
from typing import Optional
__all__ = ["abbreviated_number"]
def abbreviated_number(argument: str) -> int:
"""Custom converter to allow numbers to be abbreviated
Examples:
@ -17,26 +20,27 @@ def abbreviated_number(argument: str) -> int:
units = {"k": 3, "m": 6, "b": 9, "t": 12}
# Get the unit if there is one, then chop it off
unit: Optional[str] = None
value: Optional[int] = None
if not argument[-1].isdigit():
if argument[-1].lower() not in units:
raise ValueError
unit = argument[-1].lower()
value = units.get(unit)
argument = argument[:-1]
# [int][unit]
if "." not in argument and unit is not None:
return int(argument) * (10 ** units.get(unit))
if "." not in argument and value is not None:
return int(argument) * (10**value)
# [float][unit]
if "." in argument:
# Floats themselves are not supported
if unit is None:
if value is None:
raise ValueError
as_float = float(argument)
return math.floor(as_float * (10 ** units.get(unit)))
return math.floor(as_float * (10**value))
# Unparseable
raise ValueError