From c294bc8da50fdb1ba24db10a21b3457bfcf1e00f Mon Sep 17 00:00:00 2001 From: stijndcl Date: Fri, 1 Jul 2022 14:05:00 +0200 Subject: [PATCH] Use abbreviated numbers in award --- didier/cogs/currency.py | 13 +++++++++---- didier/utils/discord/converters/__init__.py | 1 + didier/utils/discord/converters/numbers.py | 14 +++++++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/didier/cogs/currency.py b/didier/cogs/currency.py index 3283ed4..606a910 100644 --- a/didier/cogs/currency.py +++ b/didier/cogs/currency.py @@ -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: diff --git a/didier/utils/discord/converters/__init__.py b/didier/utils/discord/converters/__init__.py index e69de29..3f47753 100644 --- a/didier/utils/discord/converters/__init__.py +++ b/didier/utils/discord/converters/__init__.py @@ -0,0 +1 @@ +from .numbers import * diff --git a/didier/utils/discord/converters/numbers.py b/didier/utils/discord/converters/numbers.py index bd0f432..8019fa5 100644 --- a/didier/utils/discord/converters/numbers.py +++ b/didier/utils/discord/converters/numbers.py @@ -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