refactor(utils): add surround function

This commit is contained in:
Ryan 2025-02-25 17:55:07 -05:00
parent 0a8482c030
commit 5430f7c632
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 21 additions and 18 deletions

View File

@ -8,9 +8,9 @@ from yt_dlp import version
import arguments import arguments
import commands import commands
import utils
from constants import EMBED_COLOR from constants import EMBED_COLOR
from state import client, start_time from state import client, start_time
from utils import format_duration, reply, surround
async def status(message): async def status(message):
@ -25,41 +25,41 @@ async def status(message):
embed = disnake.Embed(color=EMBED_COLOR) embed = disnake.Embed(color=EMBED_COLOR)
embed.add_field( embed.add_field(
name="Latency", name="Latency",
value=f"```{round(client.latency * 1000, 1)} ms```", value=surround(f"{round(client.latency * 1000, 1)} ms"),
) )
embed.add_field( embed.add_field(
name="Memory", name="Memory",
value=f"```{round(memory_usage, 1)} MiB```", value=surround(f"{round(memory_usage, 1)} MiB"),
) )
embed.add_field( embed.add_field(
name="Threads", name="Threads",
value=f"```{threading.active_count()}```", value=surround(threading.active_count()),
) )
embed.add_field( embed.add_field(
name="Guilds", name="Guilds",
value=f"```{len(client.guilds)}```", value=surround(len(client.guilds)),
) )
embed.add_field( embed.add_field(
name="Members", name="Members",
value=f"```{member_count}```", value=surround(member_count),
) )
embed.add_field( embed.add_field(
name="Channels", name="Channels",
value=f"```{channel_count}```", value=surround(channel_count),
) )
embed.add_field( embed.add_field(
name="Disnake", name="Disnake",
value=f"```{disnake.__version__}```", value=surround(disnake.__version__),
) )
embed.add_field( embed.add_field(
name="yt-dlp", name="yt-dlp",
value=f"```{version.__version__}```", value=surround(version.__version__),
) )
embed.add_field( embed.add_field(
name="Uptime", name="Uptime",
value=f"```{utils.format_duration(int(time.time() - start_time), short=True)}```", value=surround(format_duration(int(time.time() - start_time), short=True)),
) )
await utils.reply(message, embed=embed) await reply(message, embed=embed)
async def uptime(message): async def uptime(message):
@ -78,15 +78,13 @@ async def uptime(message):
return return
if args.since: if args.since:
await utils.reply(message, f"{round(start_time)}") await reply(message, f"{round(start_time)}")
else: else:
await utils.reply( await reply(message, f"up {format_duration(int(time.time() - start_time))}")
message, f"up {utils.format_duration(int(time.time() - start_time))}"
)
async def ping(message): async def ping(message):
await utils.reply( await reply(
message, message,
embed=disnake.Embed( embed=disnake.Embed(
title="Pong :ping_pong:", title="Pong :ping_pong:",
@ -97,7 +95,7 @@ async def ping(message):
async def help(message): async def help(message):
await utils.reply( await reply(
message, message,
", ".join( ", ".join(
[f"`{command.value}`" for command in commands.Command.__members__.values()] [f"`{command.value}`" for command in commands.Command.__members__.values()]

View File

@ -1,4 +1,4 @@
from .common import LimitedSizeDict, filter_secrets, format_duration from .common import LimitedSizeDict, filter_secrets, format_duration, surround
from .discord import ( from .discord import (
ChannelResponseWrapper, ChannelResponseWrapper,
MessageInteractionWrapper, MessageInteractionWrapper,
@ -24,4 +24,5 @@ __all__ = [
"MessageInteractionWrapper", "MessageInteractionWrapper",
"reply", "reply",
"snowflake_timestamp", "snowflake_timestamp",
"surround",
] ]

View File

@ -3,6 +3,10 @@ from collections import OrderedDict
from constants import SECRETS from constants import SECRETS
def surround(inner, outer="```"):
return outer + str(inner) + outer
def format_duration(duration: int, natural: bool = False, short: bool = False): def format_duration(duration: int, natural: bool = False, short: bool = False):
def format_plural(noun, count): def format_plural(noun, count):
if short: if short: