[Utils] Add humanize_number() function to chat formatting (#2836)

This adds babel as a dependency, and also includes `redbot.core.i18n.get_babel_locale()`
This commit is contained in:
Draper
2019-08-27 23:44:52 +01:00
committed by Toby Harradine
parent 6c3a3fea66
commit 3c1b6ae4cf
15 changed files with 238 additions and 80 deletions

View File

@@ -10,7 +10,7 @@ import discord
from redbot.cogs.bank import check_global_setting_guildowner, check_global_setting_admin
from redbot.core import Config, bank, commands, errors
from redbot.core.i18n import Translator, cog_i18n
from redbot.core.utils.chat_formatting import box
from redbot.core.utils.chat_formatting import box, humanize_number
from redbot.core.utils.menus import menu, DEFAULT_CONTROLS
from redbot.core.bot import Red
@@ -162,7 +162,7 @@ class Economy(commands.Cog):
await ctx.send(
_("{user}'s balance is {num} {currency}").format(
user=user.display_name, num=bal, currency=currency
user=user.display_name, num=humanize_number(bal), currency=currency
)
)
@@ -179,7 +179,10 @@ class Economy(commands.Cog):
await ctx.send(
_("{user} transferred {num} {currency} to {other_user}").format(
user=from_.display_name, num=amount, currency=currency, other_user=to.display_name
user=from_.display_name,
num=humanize_number(amount),
currency=currency,
other_user=to.display_name,
)
)
@@ -203,7 +206,7 @@ class Economy(commands.Cog):
await bank.deposit_credits(to, creds.sum)
msg = _("{author} added {num} {currency} to {user}'s account.").format(
author=author.display_name,
num=creds.sum,
num=humanize_number(creds.sum),
currency=currency,
user=to.display_name,
)
@@ -211,7 +214,7 @@ class Economy(commands.Cog):
await bank.withdraw_credits(to, creds.sum)
msg = _("{author} removed {num} {currency} from {user}'s account.").format(
author=author.display_name,
num=creds.sum,
num=humanize_number(creds.sum),
currency=currency,
user=to.display_name,
)
@@ -219,7 +222,7 @@ class Economy(commands.Cog):
await bank.set_balance(to, creds.sum)
msg = _("{author} set {user}'s account balance to {num} {currency}.").format(
author=author.display_name,
num=creds.sum,
num=humanize_number(creds.sum),
currency=currency,
user=to.display_name,
)
@@ -271,7 +274,9 @@ class Economy(commands.Cog):
"You've reached the maximum amount of {currency}!"
"Please spend some more \N{GRIMACING FACE}\n\n"
"You currently have {new_balance} {currency}."
).format(currency=credits_name, new_balance=exc.max_balance)
).format(
currency=credits_name, new_balance=humanize_number(exc.max_balance)
)
)
return
next_payday = cur_time + await self.config.PAYDAY_TIME()
@@ -287,9 +292,9 @@ class Economy(commands.Cog):
).format(
author=author,
currency=credits_name,
amount=await self.config.PAYDAY_CREDITS(),
new_balance=await bank.get_balance(author),
pos=pos,
amount=humanize_number(await self.config.PAYDAY_CREDITS()),
new_balance=humanize_number(await bank.get_balance(author)),
pos=humanize_number(pos) if pos else pos,
)
)
@@ -319,7 +324,9 @@ class Economy(commands.Cog):
"You've reached the maximum amount of {currency}! "
"Please spend some more \N{GRIMACING FACE}\n\n"
"You currently have {new_balance} {currency}."
).format(currency=credits_name, new_balance=exc.max_balance)
).format(
currency=credits_name, new_balance=humanize_number(exc.max_balance)
)
)
return
next_payday = cur_time + await self.config.guild(guild).PAYDAY_TIME()
@@ -334,9 +341,9 @@ class Economy(commands.Cog):
).format(
author=author,
currency=credits_name,
amount=credit_amount,
new_balance=await bank.get_balance(author),
pos=pos,
amount=humanize_number(credit_amount),
new_balance=humanize_number(await bank.get_balance(author)),
pos=humanize_number(pos) if pos else pos,
)
)
else:
@@ -364,7 +371,7 @@ class Economy(commands.Cog):
else:
bank_sorted = await bank.get_leaderboard(positions=top, guild=guild)
try:
bal_len = len(str(bank_sorted[0][1]["balance"]))
bal_len = len(humanize_number(bank_sorted[0][1]["balance"]))
# first user is the largest we'll see
except IndexError:
return await ctx.send(_("There are no accounts in the bank."))
@@ -387,14 +394,17 @@ class Economy(commands.Cog):
if await ctx.bot.is_owner(ctx.author):
user_id = f"({str(acc[0])})"
name = f"{acc[1]['name']} {user_id}"
balance = acc[1]["balance"]
balance = humanize_number(acc[1]["balance"])
if acc[0] != author.id:
temp_msg += f"{f'{pos}.': <{pound_len+2}} {balance: <{bal_len + 5}} {name}\n"
temp_msg += (
f"{f'{humanize_number(pos)}.': <{pound_len+2}} "
f"{balance: <{bal_len + 5}} {name}\n"
)
else:
temp_msg += (
f"{f'{pos}.': <{pound_len+2}} "
f"{f'{humanize_number(pos)}.': <{pound_len+2}} "
f"{balance: <{bal_len + 5}} "
f"<<{author.display_name}>>\n"
)
@@ -503,8 +513,8 @@ class Economy(commands.Cog):
"Please spend some more \N{GRIMACING FACE}\n{old_balance} -> {new_balance}!"
).format(
currency=await bank.get_currency_name(getattr(channel, "guild", None)),
old_balance=then,
new_balance=exc.max_balance,
old_balance=humanize_number(then),
new_balance=humanize_number(exc.max_balance),
)
)
return
@@ -523,10 +533,10 @@ class Economy(commands.Cog):
slot=slot,
author=author,
phrase=phrase,
bid=bid,
old_balance=then,
new_balance=now,
pay=pay,
bid=humanize_number(bid),
old_balance=humanize_number(then),
new_balance=humanize_number(now),
pay=humanize_number(pay),
)
)
@@ -552,12 +562,12 @@ class Economy(commands.Cog):
"Payday cooldown: {payday_time}\n"
"Amount given at account registration: {register_amount}"
).format(
slot_min=await conf.SLOT_MIN(),
slot_max=await conf.SLOT_MAX(),
slot_time=await conf.SLOT_TIME(),
payday_time=await conf.PAYDAY_TIME(),
payday_amount=await conf.PAYDAY_CREDITS(),
register_amount=await bank.get_default_balance(guild),
slot_min=humanize_number(await conf.SLOT_MIN()),
slot_max=humanize_number(await conf.SLOT_MAX()),
slot_time=humanize_number(await conf.SLOT_TIME()),
payday_time=humanize_number(await conf.PAYDAY_TIME()),
payday_amount=humanize_number(await conf.PAYDAY_CREDITS()),
register_amount=humanize_number(await bank.get_default_balance(guild)),
)
)
)
@@ -575,7 +585,9 @@ class Economy(commands.Cog):
await self.config.guild(guild).SLOT_MIN.set(bid)
credits_name = await bank.get_currency_name(guild)
await ctx.send(
_("Minimum bid is now {bid} {currency}.").format(bid=bid, currency=credits_name)
_("Minimum bid is now {bid} {currency}.").format(
bid=humanize_number(bid), currency=credits_name
)
)
@economyset.command()
@@ -594,7 +606,9 @@ class Economy(commands.Cog):
else:
await self.config.guild(guild).SLOT_MAX.set(bid)
await ctx.send(
_("Maximum bid is now {bid} {currency}.").format(bid=bid, currency=credits_name)
_("Maximum bid is now {bid} {currency}.").format(
bid=humanize_number(bid), currency=credits_name
)
)
@economyset.command()
@@ -635,7 +649,7 @@ class Economy(commands.Cog):
await self.config.guild(guild).PAYDAY_CREDITS.set(creds)
await ctx.send(
_("Every payday will now give {num} {currency}.").format(
num=creds, currency=credits_name
num=humanize_number(creds), currency=credits_name
)
)
@@ -655,7 +669,7 @@ class Economy(commands.Cog):
_(
"Every payday will now give {num} {currency} "
"to people with the role {role_name}."
).format(num=creds, currency=credits_name, role_name=role.name)
).format(num=humanize_number(creds), currency=credits_name, role_name=role.name)
)
@economyset.command()
@@ -668,7 +682,7 @@ class Economy(commands.Cog):
await bank.set_default_balance(creds, guild)
await ctx.send(
_("Registering an account will now give {num} {currency}.").format(
num=creds, currency=credits_name
num=humanize_number(creds), currency=credits_name
)
)