mirror of https://github.com/stijndcl/didier
Use abbreviated numbers in award
parent
96916d2abd
commit
c294bc8da5
|
@ -1,3 +1,5 @@
|
||||||
|
import typing
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
|
@ -5,6 +7,7 @@ from database.crud import currency as crud
|
||||||
from database.exceptions.currency import DoubleNightly
|
from database.exceptions.currency import DoubleNightly
|
||||||
from didier import Didier
|
from didier import Didier
|
||||||
from didier.utils.discord.checks import is_owner
|
from didier.utils.discord.checks import is_owner
|
||||||
|
from didier.utils.discord.converters import abbreviated_number
|
||||||
from didier.utils.types.string import pluralize
|
from didier.utils.types.string import pluralize
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,17 +22,19 @@ class Currency(commands.Cog):
|
||||||
|
|
||||||
@commands.command(name="Award")
|
@commands.command(name="Award")
|
||||||
@commands.check(is_owner)
|
@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"""
|
"""Award a user a given amount of Didier Dinks"""
|
||||||
|
amount = typing.cast(int, amount)
|
||||||
|
|
||||||
async with self.client.db_session as session:
|
async with self.client.db_session as session:
|
||||||
await crud.add_dinks(session, user.id, amount)
|
await crud.add_dinks(session, user.id, amount)
|
||||||
plural = pluralize("Didier Dink", amount)
|
plural = pluralize("Didier Dink", amount)
|
||||||
await ctx.reply(
|
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):
|
async def bank(self, ctx: commands.Context):
|
||||||
"""Show your Didier Bank information"""
|
"""Show your Didier Bank information"""
|
||||||
async with self.client.db_session as session:
|
async with self.client.db_session as session:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
from .numbers import *
|
|
@ -2,6 +2,9 @@ import math
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ["abbreviated_number"]
|
||||||
|
|
||||||
|
|
||||||
def abbreviated_number(argument: str) -> int:
|
def abbreviated_number(argument: str) -> int:
|
||||||
"""Custom converter to allow numbers to be abbreviated
|
"""Custom converter to allow numbers to be abbreviated
|
||||||
Examples:
|
Examples:
|
||||||
|
@ -17,26 +20,27 @@ def abbreviated_number(argument: str) -> int:
|
||||||
units = {"k": 3, "m": 6, "b": 9, "t": 12}
|
units = {"k": 3, "m": 6, "b": 9, "t": 12}
|
||||||
|
|
||||||
# Get the unit if there is one, then chop it off
|
# 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 not argument[-1].isdigit():
|
||||||
if argument[-1].lower() not in units:
|
if argument[-1].lower() not in units:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
unit = argument[-1].lower()
|
unit = argument[-1].lower()
|
||||||
|
value = units.get(unit)
|
||||||
argument = argument[:-1]
|
argument = argument[:-1]
|
||||||
|
|
||||||
# [int][unit]
|
# [int][unit]
|
||||||
if "." not in argument and unit is not None:
|
if "." not in argument and value is not None:
|
||||||
return int(argument) * (10 ** units.get(unit))
|
return int(argument) * (10**value)
|
||||||
|
|
||||||
# [float][unit]
|
# [float][unit]
|
||||||
if "." in argument:
|
if "." in argument:
|
||||||
# Floats themselves are not supported
|
# Floats themselves are not supported
|
||||||
if unit is None:
|
if value is None:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
as_float = float(argument)
|
as_float = float(argument)
|
||||||
return math.floor(as_float * (10 ** units.get(unit)))
|
return math.floor(as_float * (10**value))
|
||||||
|
|
||||||
# Unparseable
|
# Unparseable
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
Loading…
Reference in New Issue