Skip to content

Commit

Permalink
feat: use format_dt from discord.utils (#134)
Browse files Browse the repository at this point in the history
also added shard info and changed some things for consistency
  • Loading branch information
NiceAesth authored Feb 19, 2024
1 parent ec55a63 commit 32c9420
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 41 deletions.
24 changes: 0 additions & 24 deletions src/common/humanizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,6 @@ def number(n: float) -> str:
)


def seconds_to_long_text(secs: int) -> str:
if secs == 0:
return "0 seconds"

time_units = [
("day", secs // 86400),
("hour", (secs // 3600) % 24),
("minute", (secs // 60) % 60),
("second", secs % 60),
]

formatted_units = [
f"{value} {unit}s" if value != 1 else f"{value} {unit}"
for unit, value in time_units
if value > 0
]

return (
" ".join(formatted_units[:-1]) + " and " + formatted_units[-1]
if len(formatted_units) > 1
else formatted_units[0]
)


def seconds_to_text(secs: int) -> str:
if secs == 0:
return "0 seconds"
Expand Down
4 changes: 2 additions & 2 deletions src/models/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class LocationData(BaseModel):
id: int | None = None
type: int | None = None
country: str
sunrise: int
sunset: int
sunrise: datetime
sunset: datetime


class WeatherResponse(BaseModel):
Expand Down
7 changes: 5 additions & 2 deletions src/ui/embeds/information/botinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from typing import TYPE_CHECKING

from discord.utils import format_dt

from ui.embeds.generic import ContextEmbed

if TYPE_CHECKING:
Expand Down Expand Up @@ -34,6 +36,7 @@ def __init__(self, ctx: Context, *args: Any, **kwargs: Any) -> None:
self.add_field(name="Commands", value=len(ctx.bot.all_commands))
self.add_field(name="Cogs", value=len(ctx.bot.cogs))
self.add_field(
name="Created:",
value=f"<t:{ctx.bot.user.created_at.timestamp():.0f}:R>",
name="Shard Info",
value=f"`ID {ctx.guild.shard_id}`: `{ctx.bot.shard_count} total`",
)
self.add_field(name="Created", value=format_dt(ctx.bot.user.created_at))
10 changes: 4 additions & 6 deletions src/ui/embeds/information/severinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from typing import TYPE_CHECKING

import discord
from discord.channel import TextChannel
from discord.utils import format_dt

from ui.embeds.generic import ContextEmbed

Expand Down Expand Up @@ -40,15 +41,12 @@ def __init__(self, ctx: Context, *args: Any, **kwargs: Any) -> None:
value=len(
list(
filter(
lambda x: isinstance(x, discord.channel.TextChannel),
lambda x: isinstance(x, TextChannel),
ctx.guild.channels,
),
),
),
)
self.add_field(name="Roles", value=len(ctx.guild.roles))
self.add_field(name="Emotes", value=len(ctx.guild.emojis))
self.add_field(
name="Created On:",
value=f"<t:{ctx.guild.created_at.timestamp():.0f}:R>",
)
self.add_field(name="Created", value=format_dt(ctx.guild.created_at))
3 changes: 2 additions & 1 deletion src/ui/embeds/osu/beatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from aiosu.models import Beatmap
from aiosu.models import Beatmapset
from discord.utils import escape_markdown
from discord.utils import format_dt

from ui.embeds.generic import ContextEmbed
from ui.icons.beatmap import BeatmapDifficultyIcon
Expand Down Expand Up @@ -68,7 +69,7 @@ def __init__(
content = cleandoc(
f"""**▸** {beatmap.difficulty_rating:.2f}⭐ **▸Max Combo:** x{beatmap.max_combo}\n**▸AR:** {beatmap.ar} **▸OD:** {beatmap.accuracy} **▸CS:** {beatmap.cs} **▸HP:** {beatmap.drain}
{beatmap.status.name} | ❤️ {beatmapset.favourite_count} | ▶️ {beatmapset.play_count}
{footer_date_prefix}<t:{date.timestamp():.0f}:R>
{footer_date_prefix}{format_dt(date, style="R")}
""",
)

Expand Down
7 changes: 4 additions & 3 deletions src/ui/embeds/osu/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from aiosu.models import User
from discord.ext import commands
from discord.utils import escape_markdown
from discord.utils import format_dt

from common import humanizer
from ui.embeds.generic import ContextEmbed
Expand All @@ -30,7 +31,7 @@ def __init__(
) -> None:
peak_str = ""
if user.rank_highest:
peak_str = f"(peaked #{user.rank_highest.rank} <t:{user.rank_highest.updated_at.timestamp():.0f}:R>)\n"
peak_str = f"(peaked #{user.rank_highest.rank} {format_dt(user.rank_highest.updated_at)}\n"

content = (
f"{user.statistics.pp}pp (#{user.statistics.global_rank} | {user.country.flag_emoji}#{user.statistics.country_rank})\n"
Expand Down Expand Up @@ -73,7 +74,7 @@ def __init__(
) -> None:
peak_str = ""
if user.rank_highest:
peak_str = f"peak: **#{user.rank_highest.rank}** <t:{user.rank_highest.updated_at.timestamp():.0f}:R>\n"
peak_str = f"peak: **#{user.rank_highest.rank}** {format_dt(user.rank_highest.updated_at)}\n"

rank_content = (
f"rank: **#{user.statistics.global_rank}**\n"
Expand All @@ -94,7 +95,7 @@ def __init__(
max combo: **{user.statistics.maximum_combo}**
pp/hour: **{user.statistics.pp_per_playtime:.2f}**
avg. rank gain: **{user.rank_history.average_gain:.2f}**
joined: <t:{user.join_date.timestamp():.0f}:R>
joined: {format_dt(user.join_date)}
""",
)
self.add_field(name="", value=average_content)
Expand Down
3 changes: 2 additions & 1 deletion src/ui/embeds/osu/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from aiosu.models import Score
from aiosu.utils.performance import get_calculator
from discord.utils import escape_markdown
from discord.utils import format_dt

from ui.embeds.generic import ContextEmbed
from ui.icons.score import ScoreRankIcon
Expand Down Expand Up @@ -144,7 +145,7 @@ def _score_to_embed_strs(
f"""**{pp:.2f}pp**{weight}, accuracy: **{score.accuracy*100:.2f}%**, combo: **{score.max_combo}x/{max_combo}x**
{fc_text}score: **{score.score}** [**{statistics.count_300}**/**{statistics.count_100}**/**{statistics.count_50}**/**{statistics.count_miss}**]
bpm: {bpm_text} | mods: {mods_text} | {ScoreRankIcon[score.rank]}{fail_text}{mods_settings_text}
<t:{score.created_at.timestamp():.0f}:R>
{format_dt(score.created_at, style="R")}
{score_text}{user_text}[map]({beatmap.url})
""",
)
Expand Down
5 changes: 3 additions & 2 deletions src/ui/embeds/weather/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from inspect import cleandoc
from typing import TYPE_CHECKING

from discord.utils import format_dt
from emojiflags import lookup

from models.weather import Units
Expand All @@ -22,8 +23,8 @@ def __init__(self, ctx: commands.Context, response: WeatherResponse, units: Unit
description = cleandoc(
f"""
{latest_forecast.main.capitalize()} ({latest_forecast.description})
Sunrise <t:{response.location_data.sunrise:.0f}:R>
Sunset <t:{response.location_data.sunset:.0f}:R>
Sunrise {format_dt(response.location_data.sunrise, style="t")}
Sunset {format_dt(response.location_data.sunset, style="t")}
""",
)
flag = lookup.lookup(response.location_data.country)
Expand Down

0 comments on commit 32c9420

Please sign in to comment.