Skip to content
Permalink
Browse files

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

This adds babel as a dependency, and also includes `redbot.core.i18n.get_babel_locale()`
  • Loading branch information...
Drapersniper authored and Tobotimus committed Aug 27, 2019
1 parent 6c3a3fe commit 3c1b6ae4cf56c516fa8d2b1ea778a7242db33aa6
@@ -0,0 +1 @@
New :func:`humanize_number` in :module:`redbot.core.utils.chat_formatting` function to convert numbers into text which respect locale.
@@ -0,0 +1 @@
New :func:`humanize_number` is used throughout the bot.
@@ -19,7 +19,7 @@
from redbot.core import Config, commands, checks, bank
from redbot.core.data_manager import cog_data_path
from redbot.core.i18n import Translator, cog_i18n
from redbot.core.utils.chat_formatting import bold, box, pagify
from redbot.core.utils.chat_formatting import bold, box, pagify, humanize_number
from redbot.core.utils.menus import (
menu,
DEFAULT_CONTROLS,
@@ -442,7 +442,7 @@ def _restart_connect(self):
await self._embed_msg(
ctx,
_("Track queueing command price set to {price} {currency}.").format(
price=price, currency=await bank.get_currency_name(ctx.guild)
price=humanize_number(price), currency=await bank.get_currency_name(ctx.guild)
),
)

@@ -613,7 +613,9 @@ def _restart_connect(self):
msg += _("DJ Role: [{role.name}]\n").format(role=dj_role_obj)
if jukebox:
msg += _("Jukebox: [{jukebox_name}]\n").format(jukebox_name=jukebox)
msg += _("Command price: [{jukebox_price}]\n").format(jukebox_price=jukebox_price)
msg += _("Command price: [{jukebox_price}]\n").format(
jukebox_price=humanize_number(jukebox_price)
)
if maxlength > 0:
msg += _("Max track length: [{tracklength}]\n").format(
tracklength=self._dynamic_time(maxlength)
@@ -762,11 +764,15 @@ def _restart_connect(self):
em = discord.Embed(
colour=await ctx.embed_colour(),
title=_("Playing in {num}/{total} servers:").format(
num=server_num, total=total_num
num=humanize_number(server_num), total=humanize_number(total_num)
),
description=page,
)
em.set_footer(text="Page {}/{}".format(pages, (math.ceil(len(msg) / 1500))))
em.set_footer(
text="Page {}/{}".format(
humanize_number(pages), humanize_number((math.ceil(len(msg) / 1500)))
)
)
pages += 1
servers_embed.append(em)

@@ -933,7 +939,9 @@ def _restart_connect(self):
embed = discord.Embed(
colour=await ctx.embed_colour(), description=(f"{header}\n{formatted_page}")
)
embed.set_footer(text=_("{num} preset(s)").format(num=len(list(eq_presets.keys()))))
embed.set_footer(
text=_("{num} preset(s)").format(num=humanize_number(len(list(eq_presets.keys()))))
)
page_list.append(embed)
if len(page_list) == 1:
return await ctx.send(embed=page_list[0])
@@ -1869,6 +1877,7 @@ def _restart_connect(self):
song_info = "{} {}".format(i["name"], i["artists"][0]["name"])
else:
song_info = "{} {}".format(i["track"]["name"], i["track"]["artists"][0]["name"])

try:
track_url = await self._youtube_api_search(yt_key, song_info)
except (RuntimeError, aiohttp.client_exceptions.ServerDisconnectedError):
@@ -1878,7 +1887,6 @@ def _restart_connect(self):
)
await playlist_msg.edit(embed=error_embed)
return None
pass
try:
yt_track = await player.get_tracks(track_url)
except (RuntimeError, aiohttp.client_exceptions.ServerDisconnectedError):
@@ -3416,8 +3424,8 @@ def _restart_connect(self):
" Votes: {num_votes}/{num_members}"
" ({cur_percent}% out of {required_percent}% needed)"
).format(
num_votes=num_votes,
num_members=num_members,
num_votes=humanize_number(num_votes),
num_members=humanize_number(num_members),
cur_percent=vote,
required_percent=percent,
)
@@ -3869,7 +3877,7 @@ def _restart_connect(self):
await self._embed_msg(
ctx,
_("Not enough {currency} ({required_credits} required).").format(
currency=credits_name, required_credits=jukebox_price
currency=credits_name, required_credits=humanize_number(jukebox_price)
),
)
return False
@@ -1,5 +1,5 @@
import discord
from redbot.core.utils.chat_formatting import box
from redbot.core.utils.chat_formatting import box, humanize_number

from redbot.core import checks, bank, commands
from redbot.core.i18n import Translator, cog_i18n
@@ -88,7 +88,9 @@ def __init__(self, bot: Red):
"Bank settings:\n\nBank name: {bank_name}\nCurrency: {currency_name}\n"
"Default balance: {default_balance}"
).format(
bank_name=bank_name, currency_name=currency_name, default_balance=default_balance
bank_name=bank_name,
currency_name=currency_name,
default_balance=humanize_number(default_balance),
)
await ctx.send(box(settings))

@@ -8,6 +8,7 @@
from redbot.core import checks, commands
from redbot.core.bot import Red
from redbot.core.i18n import Translator, cog_i18n
from redbot.core.utils.chat_formatting import humanize_number
from redbot.core.utils.mod import slow_deletion, mass_purge
from redbot.core.utils.predicates import MessagePredicate
from .converters import RawMessageIds
@@ -39,7 +40,9 @@ def __init__(self, bot: Red):
return True

prompt = await ctx.send(
_("Are you sure you want to delete {number} messages? (y/n)").format(number=number)
_("Are you sure you want to delete {number} messages? (y/n)").format(
number=humanize_number(number)
)
)
response = await ctx.bot.wait_for("message", check=MessagePredicate.same_context(ctx))

@@ -152,7 +155,11 @@ def check(m):
to_delete.append(ctx.message)

reason = "{}({}) deleted {} messages containing '{}' in channel {}.".format(
author.name, author.id, len(to_delete), text, channel.id
author.name,
author.id,
humanize_number(len(to_delete), override_locale="en_us"),
text,
channel.id,
)
log.info(reason)

@@ -208,7 +215,14 @@ def check(m):
reason = (
"{}({}) deleted {} messages "
" made by {}({}) in channel {}."
"".format(author.name, author.id, len(to_delete), member or "???", _id, channel.name)
"".format(
author.name,
author.id,
humanize_number(len(to_delete), override_locale="en_US"),
member or "???",
_id,
channel.name,
)
)
log.info(reason)

@@ -240,7 +254,10 @@ def check(m):
)

reason = "{}({}) deleted {} messages in channel {}.".format(
author.name, author.id, len(to_delete), channel.name
author.name,
author.id,
humanize_number(len(to_delete), override_locale="en_US"),
channel.name,
)
log.info(reason)

@@ -277,7 +294,10 @@ def check(m):
to_delete.append(ctx.message)

reason = "{}({}) deleted {} messages in channel {}.".format(
author.name, author.id, len(to_delete), channel.name
author.name,
author.id,
humanize_number(len(to_delete), override_locale="en_US"),
channel.name,
)
log.info(reason)

@@ -319,7 +339,10 @@ def check(m):
)
to_delete.append(ctx.message)
reason = "{}({}) deleted {} messages in channel {}.".format(
author.name, author.id, len(to_delete), channel.name
author.name,
author.id,
humanize_number(len(to_delete), override_locale="en_US"),
channel.name,
)
log.info(reason)

@@ -420,7 +443,12 @@ def check(m):
reason = (
"{}({}) deleted {} "
" command messages in channel {}."
"".format(author.name, author.id, len(to_delete), channel.name)
"".format(
author.name,
author.id,
humanize_number(len(to_delete), override_locale="en_US"),
channel.name,
)
)
log.info(reason)

@@ -500,7 +528,12 @@ def check(m):
reason = (
"{}({}) deleted {} messages "
"sent by the bot in {}."
"".format(author.name, author.id, len(to_delete), channel_name)
"".format(
author.name,
author.id,
humanize_number(len(to_delete), override_locale="en_US"),
channel_name,
)
)
log.info(reason)

0 comments on commit 3c1b6ae

Please sign in to comment.
You can’t perform that action at this time.