1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 20:23:12 +00:00

Refactor embed_builder to not require ctx

This commit is contained in:
wlinator 2024-08-28 09:53:21 -04:00
parent 24b84c11f2
commit 7fd1b6657d
9 changed files with 68 additions and 205 deletions

View file

@ -74,8 +74,9 @@ async def handle_error(
description = CONST.STRINGS["error_unknown_error_description"]
await ctx.send(
embed=builder.create_error_embed(
ctx,
embed=builder.create_embed(
theme="error",
user_name=ctx.author.name,
author_text=author_text,
description=description,
footer_text=footer_text,

View file

@ -85,6 +85,7 @@ class _constants:
CHECK_ICON: str = _fetch_url + _s["art"]["icons"]["check"]
CROSS_ICON: str = _fetch_url + _s["art"]["icons"]["cross"]
EXCLAIM_ICON: str = _fetch_url + _s["art"]["icons"]["exclaim"]
INFO_ICON: str = _fetch_url + _s["art"]["icons"]["info"]
HAMMER_ICON: str = _fetch_url + _s["art"]["icons"]["hammer"]
MONEY_BAG_ICON: str = _fetch_url + _s["art"]["icons"]["money_bag"]
MONEY_COINS_ICON: str = _fetch_url + _s["art"]["icons"]["money_coins"]

View file

@ -28,8 +28,9 @@ class Sync(commands.Cog):
self.bot.tree.copy_global_to(guild=guild)
await self.bot.tree.sync(guild=guild)
embed = builder.create_success_embed(
ctx,
embed = builder.create_embed(
theme="success",
user_name=ctx.author.name,
author_text=CONST.STRINGS["sync_author"],
description=CONST.STRINGS["sync_description"],
)

View file

@ -29,18 +29,15 @@ class Info(commands.Cog):
],
)
embed: discord.Embed = builder.create_success_embed(
ctx,
embed: discord.Embed = builder.create_embed(
theme="info",
user_name=ctx.author.name,
author_text=f"{CONST.TITLE} v{CONST.VERSION}",
author_url=CONST.REPO_URL,
description=description,
footer_text=CONST.STRINGS["info_service_footer"],
show_name=False,
thumbnail_url=CONST.LUMI_LOGO_OPAQUE,
)
embed.set_author(
name=f"{CONST.TITLE} v{CONST.VERSION}",
url=CONST.REPO_URL,
icon_url=CONST.CHECK_ICON,
)
embed.set_thumbnail(url=CONST.LUMI_LOGO_OPAQUE)
await ctx.send(embed=embed)

View file

@ -12,8 +12,9 @@ class Ping(commands.Cog):
usage="ping",
)
async def ping(self, ctx: commands.Context[commands.Bot]) -> None:
embed = builder.create_success_embed(
ctx,
embed = builder.create_embed(
theme="success",
user_name=ctx.author.name,
author_text=CONST.STRINGS["ping_author"],
description=CONST.STRINGS["ping_pong"],
footer_text=CONST.STRINGS["ping_footer"].format(

View file

@ -17,8 +17,9 @@ class Uptime(commands.Cog):
async def uptime(self, ctx: commands.Context[commands.Bot]) -> None:
unix_timestamp: int = int(self.start_time.timestamp())
embed: Embed = builder.create_success_embed(
ctx,
embed: Embed = builder.create_embed(
theme="info",
user_name=ctx.author.name,
author_text=CONST.STRINGS["ping_author"],
description=CONST.STRINGS["ping_uptime"].format(unix_timestamp),
footer_text=CONST.STRINGS["ping_footer"].format(

View file

@ -23,8 +23,8 @@ async def print_comic(
comic = _xkcd.get_random_comic(raw_comic_image=True)
await interaction.response.send_message(
embed=builder.create_success_embed(
interaction,
embed=builder.create_embed(
theme="info",
author_text=CONST.STRINGS["xkcd_title"].format(comic.id, comic.title),
description=CONST.STRINGS["xkcd_description"].format(
comic.explanation_url,
@ -32,14 +32,13 @@ async def print_comic(
),
footer_text=CONST.STRINGS["xkcd_footer"],
image_url=comic.image_url,
show_name=False,
),
)
except HttpError:
await interaction.response.send_message(
embed=builder.create_error_embed(
interaction,
embed=builder.create_embed(
theme="error",
author_text=CONST.STRINGS["xkcd_not_found_author"],
description=CONST.STRINGS["xkcd_not_found"],
footer_text=CONST.STRINGS["xkcd_footer"],

View file

@ -45,6 +45,7 @@ art:
check: lumi_check.png
cross: lumi_cross.png
exclaim: lumi_exclaim.png
info: lumi_info.png?_=2
hammer: lumi_hammer.png
money_bag: lumi_money_bag.png
money_coins: lumi_money_coins.png

View file

@ -1,6 +1,6 @@
import datetime
from datetime import datetime
from typing import Optional, Literal
from discord.ext import commands
import discord
from lib.const import CONST
@ -9,199 +9,60 @@ from lib.const import CONST
class builder:
@staticmethod
def create_embed(
ctx: commands.Context[commands.Bot],
title=None,
author_text=None,
author_icon_url=None,
author_url=None,
description=None,
color=None,
footer_text=None,
footer_icon_url=None,
show_name=True,
image_url=None,
thumbnail_url=None,
timestamp=None,
hide_author=False,
hide_author_icon=False,
hide_timestamp=False,
user_name: Optional[str] = None,
user_display_avatar_url: Optional[str] = None,
theme: Optional[Literal["error", "success", "info", "warning"]] = None,
title: Optional[str] = None,
author_text: Optional[str] = None,
author_icon_url: Optional[str] = None,
author_url: Optional[str] = None,
description: Optional[str] = None,
color: Optional[int] = None,
footer_text: Optional[str] = None,
footer_icon_url: Optional[str] = None,
image_url: Optional[str] = None,
thumbnail_url: Optional[str] = None,
timestamp: Optional[datetime] = None,
hide_name_in_description: bool = False,
hide_time: bool = False,
) -> discord.Embed:
if not hide_author:
if not author_text:
author_text = ctx.author.name
elif show_name:
description = f"**{ctx.author.name}** {description}"
"""
Create a standard Lumi embed with the given parameters.
"""
if not hide_author_icon and not author_icon_url:
author_icon_url = ctx.author.display_avatar.url
theme_settings = {
"error": (CONST.COLOR_ERROR, CONST.CROSS_ICON),
"success": (CONST.COLOR_DEFAULT, CONST.CHECK_ICON),
"info": (CONST.COLOR_DEFAULT, CONST.INFO_ICON),
"warning": (CONST.COLOR_WARNING, CONST.WARNING_ICON),
}
if theme in theme_settings:
color, author_icon_url = theme_settings[theme]
if not footer_text:
footer_text = "Luminara"
if not footer_icon_url:
footer_icon_url = CONST.LUMI_LOGO_TRANSPARENT
if user_name and not hide_name_in_description:
description = f"**{user_name}** {description}"
embed = discord.Embed(
embed: discord.Embed = discord.Embed(
title=title,
description=description,
color=color or CONST.COLOR_DEFAULT,
)
if not hide_author:
embed.set_author(
name=author_text,
icon_url=None if hide_author_icon else author_icon_url,
name=author_text or user_name or None,
icon_url=author_icon_url or user_display_avatar_url or None,
url=author_url,
)
embed.set_footer(text=footer_text, icon_url=footer_icon_url)
if not hide_timestamp:
embed.timestamp = timestamp or datetime.datetime.now()
embed.set_footer(
text=footer_text or CONST.TITLE,
icon_url=footer_icon_url or CONST.LUMI_LOGO_TRANSPARENT,
)
embed.timestamp = None if hide_time else (timestamp or datetime.now())
if image_url:
embed.set_image(url=image_url)
if thumbnail_url:
embed.set_thumbnail(url=thumbnail_url)
return embed
@staticmethod
def create_error_embed(
ctx,
title=None,
author_text=None,
author_icon_url=None,
author_url=None,
description=None,
footer_text=None,
show_name=True,
image_url=None,
thumbnail_url=None,
timestamp=None,
hide_author=False,
hide_author_icon=False,
hide_timestamp=False,
) -> discord.Embed:
return builder.create_embed(
ctx,
title=title,
author_text=author_text,
author_icon_url=author_icon_url or CONST.CROSS_ICON,
author_url=author_url,
description=description,
color=CONST.COLOR_ERROR,
footer_text=footer_text,
footer_icon_url=CONST.LUMI_LOGO_TRANSPARENT,
show_name=show_name,
image_url=image_url,
thumbnail_url=thumbnail_url,
timestamp=timestamp,
hide_author=hide_author,
hide_author_icon=hide_author_icon,
hide_timestamp=hide_timestamp,
)
@staticmethod
def create_success_embed(
ctx,
title=None,
author_text=None,
author_icon_url=None,
author_url=None,
description=None,
footer_text=None,
show_name=True,
image_url=None,
thumbnail_url=None,
timestamp=None,
hide_author=False,
hide_author_icon=False,
hide_timestamp=False,
) -> discord.Embed:
return builder.create_embed(
ctx,
title=title,
author_text=author_text,
author_icon_url=author_icon_url or CONST.CHECK_ICON,
author_url=author_url,
description=description,
color=CONST.COLOR_DEFAULT,
footer_text=footer_text,
footer_icon_url=CONST.LUMI_LOGO_TRANSPARENT,
show_name=show_name,
image_url=image_url,
thumbnail_url=thumbnail_url,
timestamp=timestamp,
hide_author=hide_author,
hide_author_icon=hide_author_icon,
hide_timestamp=hide_timestamp,
)
@staticmethod
def create_info_embed(
ctx,
title=None,
author_text=None,
author_icon_url=None,
author_url=None,
description=None,
footer_text=None,
show_name=True,
image_url=None,
thumbnail_url=None,
timestamp=None,
hide_author=False,
hide_author_icon=False,
hide_timestamp=False,
) -> discord.Embed:
return builder.create_embed(
ctx,
title=title,
author_text=author_text,
author_icon_url=author_icon_url or CONST.EXCLAIM_ICON,
author_url=author_url,
description=description,
color=CONST.COLOR_DEFAULT,
footer_text=footer_text,
footer_icon_url=CONST.LUMI_LOGO_TRANSPARENT,
show_name=show_name,
image_url=image_url,
thumbnail_url=thumbnail_url,
timestamp=timestamp,
hide_author=hide_author,
hide_author_icon=hide_author_icon,
hide_timestamp=hide_timestamp,
)
@staticmethod
def create_warning_embed(
ctx,
title=None,
author_text=None,
author_icon_url=None,
author_url=None,
description=None,
footer_text=None,
show_name=True,
image_url=None,
thumbnail_url=None,
timestamp=None,
hide_author=False,
hide_author_icon=False,
hide_timestamp=False,
) -> discord.Embed:
return builder.create_embed(
ctx,
title=title,
author_text=author_text,
author_icon_url=author_icon_url or CONST.WARNING_ICON,
author_url=author_url,
description=description,
color=CONST.COLOR_WARNING,
footer_text=footer_text,
footer_icon_url=CONST.LUMI_LOGO_TRANSPARENT,
show_name=show_name,
image_url=image_url,
thumbnail_url=thumbnail_url,
timestamp=timestamp,
hide_author=hide_author,
hide_author_icon=hide_author_icon,
hide_timestamp=hide_timestamp,
)