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:
parent
24b84c11f2
commit
7fd1b6657d
9 changed files with 68 additions and 205 deletions
|
@ -74,8 +74,9 @@ async def handle_error(
|
||||||
description = CONST.STRINGS["error_unknown_error_description"]
|
description = CONST.STRINGS["error_unknown_error_description"]
|
||||||
|
|
||||||
await ctx.send(
|
await ctx.send(
|
||||||
embed=builder.create_error_embed(
|
embed=builder.create_embed(
|
||||||
ctx,
|
theme="error",
|
||||||
|
user_name=ctx.author.name,
|
||||||
author_text=author_text,
|
author_text=author_text,
|
||||||
description=description,
|
description=description,
|
||||||
footer_text=footer_text,
|
footer_text=footer_text,
|
||||||
|
|
|
@ -85,6 +85,7 @@ class _constants:
|
||||||
CHECK_ICON: str = _fetch_url + _s["art"]["icons"]["check"]
|
CHECK_ICON: str = _fetch_url + _s["art"]["icons"]["check"]
|
||||||
CROSS_ICON: str = _fetch_url + _s["art"]["icons"]["cross"]
|
CROSS_ICON: str = _fetch_url + _s["art"]["icons"]["cross"]
|
||||||
EXCLAIM_ICON: str = _fetch_url + _s["art"]["icons"]["exclaim"]
|
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"]
|
HAMMER_ICON: str = _fetch_url + _s["art"]["icons"]["hammer"]
|
||||||
MONEY_BAG_ICON: str = _fetch_url + _s["art"]["icons"]["money_bag"]
|
MONEY_BAG_ICON: str = _fetch_url + _s["art"]["icons"]["money_bag"]
|
||||||
MONEY_COINS_ICON: str = _fetch_url + _s["art"]["icons"]["money_coins"]
|
MONEY_COINS_ICON: str = _fetch_url + _s["art"]["icons"]["money_coins"]
|
||||||
|
|
|
@ -28,8 +28,9 @@ class Sync(commands.Cog):
|
||||||
self.bot.tree.copy_global_to(guild=guild)
|
self.bot.tree.copy_global_to(guild=guild)
|
||||||
await self.bot.tree.sync(guild=guild)
|
await self.bot.tree.sync(guild=guild)
|
||||||
|
|
||||||
embed = builder.create_success_embed(
|
embed = builder.create_embed(
|
||||||
ctx,
|
theme="success",
|
||||||
|
user_name=ctx.author.name,
|
||||||
author_text=CONST.STRINGS["sync_author"],
|
author_text=CONST.STRINGS["sync_author"],
|
||||||
description=CONST.STRINGS["sync_description"],
|
description=CONST.STRINGS["sync_description"],
|
||||||
)
|
)
|
||||||
|
|
|
@ -29,18 +29,15 @@ class Info(commands.Cog):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
embed: discord.Embed = builder.create_success_embed(
|
embed: discord.Embed = builder.create_embed(
|
||||||
ctx,
|
theme="info",
|
||||||
|
user_name=ctx.author.name,
|
||||||
|
author_text=f"{CONST.TITLE} v{CONST.VERSION}",
|
||||||
|
author_url=CONST.REPO_URL,
|
||||||
description=description,
|
description=description,
|
||||||
footer_text=CONST.STRINGS["info_service_footer"],
|
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)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,9 @@ class Ping(commands.Cog):
|
||||||
usage="ping",
|
usage="ping",
|
||||||
)
|
)
|
||||||
async def ping(self, ctx: commands.Context[commands.Bot]) -> None:
|
async def ping(self, ctx: commands.Context[commands.Bot]) -> None:
|
||||||
embed = builder.create_success_embed(
|
embed = builder.create_embed(
|
||||||
ctx,
|
theme="success",
|
||||||
|
user_name=ctx.author.name,
|
||||||
author_text=CONST.STRINGS["ping_author"],
|
author_text=CONST.STRINGS["ping_author"],
|
||||||
description=CONST.STRINGS["ping_pong"],
|
description=CONST.STRINGS["ping_pong"],
|
||||||
footer_text=CONST.STRINGS["ping_footer"].format(
|
footer_text=CONST.STRINGS["ping_footer"].format(
|
||||||
|
|
|
@ -17,8 +17,9 @@ class Uptime(commands.Cog):
|
||||||
async def uptime(self, ctx: commands.Context[commands.Bot]) -> None:
|
async def uptime(self, ctx: commands.Context[commands.Bot]) -> None:
|
||||||
unix_timestamp: int = int(self.start_time.timestamp())
|
unix_timestamp: int = int(self.start_time.timestamp())
|
||||||
|
|
||||||
embed: Embed = builder.create_success_embed(
|
embed: Embed = builder.create_embed(
|
||||||
ctx,
|
theme="info",
|
||||||
|
user_name=ctx.author.name,
|
||||||
author_text=CONST.STRINGS["ping_author"],
|
author_text=CONST.STRINGS["ping_author"],
|
||||||
description=CONST.STRINGS["ping_uptime"].format(unix_timestamp),
|
description=CONST.STRINGS["ping_uptime"].format(unix_timestamp),
|
||||||
footer_text=CONST.STRINGS["ping_footer"].format(
|
footer_text=CONST.STRINGS["ping_footer"].format(
|
||||||
|
|
|
@ -23,8 +23,8 @@ async def print_comic(
|
||||||
comic = _xkcd.get_random_comic(raw_comic_image=True)
|
comic = _xkcd.get_random_comic(raw_comic_image=True)
|
||||||
|
|
||||||
await interaction.response.send_message(
|
await interaction.response.send_message(
|
||||||
embed=builder.create_success_embed(
|
embed=builder.create_embed(
|
||||||
interaction,
|
theme="info",
|
||||||
author_text=CONST.STRINGS["xkcd_title"].format(comic.id, comic.title),
|
author_text=CONST.STRINGS["xkcd_title"].format(comic.id, comic.title),
|
||||||
description=CONST.STRINGS["xkcd_description"].format(
|
description=CONST.STRINGS["xkcd_description"].format(
|
||||||
comic.explanation_url,
|
comic.explanation_url,
|
||||||
|
@ -32,14 +32,13 @@ async def print_comic(
|
||||||
),
|
),
|
||||||
footer_text=CONST.STRINGS["xkcd_footer"],
|
footer_text=CONST.STRINGS["xkcd_footer"],
|
||||||
image_url=comic.image_url,
|
image_url=comic.image_url,
|
||||||
show_name=False,
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
except HttpError:
|
except HttpError:
|
||||||
await interaction.response.send_message(
|
await interaction.response.send_message(
|
||||||
embed=builder.create_error_embed(
|
embed=builder.create_embed(
|
||||||
interaction,
|
theme="error",
|
||||||
author_text=CONST.STRINGS["xkcd_not_found_author"],
|
author_text=CONST.STRINGS["xkcd_not_found_author"],
|
||||||
description=CONST.STRINGS["xkcd_not_found"],
|
description=CONST.STRINGS["xkcd_not_found"],
|
||||||
footer_text=CONST.STRINGS["xkcd_footer"],
|
footer_text=CONST.STRINGS["xkcd_footer"],
|
||||||
|
|
|
@ -45,6 +45,7 @@ art:
|
||||||
check: lumi_check.png
|
check: lumi_check.png
|
||||||
cross: lumi_cross.png
|
cross: lumi_cross.png
|
||||||
exclaim: lumi_exclaim.png
|
exclaim: lumi_exclaim.png
|
||||||
|
info: lumi_info.png?_=2
|
||||||
hammer: lumi_hammer.png
|
hammer: lumi_hammer.png
|
||||||
money_bag: lumi_money_bag.png
|
money_bag: lumi_money_bag.png
|
||||||
money_coins: lumi_money_coins.png
|
money_coins: lumi_money_coins.png
|
||||||
|
|
227
ui/embeds.py
227
ui/embeds.py
|
@ -1,6 +1,6 @@
|
||||||
import datetime
|
from datetime import datetime
|
||||||
|
from typing import Optional, Literal
|
||||||
|
|
||||||
from discord.ext import commands
|
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
from lib.const import CONST
|
from lib.const import CONST
|
||||||
|
@ -9,199 +9,60 @@ from lib.const import CONST
|
||||||
class builder:
|
class builder:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_embed(
|
def create_embed(
|
||||||
ctx: commands.Context[commands.Bot],
|
user_name: Optional[str] = None,
|
||||||
title=None,
|
user_display_avatar_url: Optional[str] = None,
|
||||||
author_text=None,
|
theme: Optional[Literal["error", "success", "info", "warning"]] = None,
|
||||||
author_icon_url=None,
|
title: Optional[str] = None,
|
||||||
author_url=None,
|
author_text: Optional[str] = None,
|
||||||
description=None,
|
author_icon_url: Optional[str] = None,
|
||||||
color=None,
|
author_url: Optional[str] = None,
|
||||||
footer_text=None,
|
description: Optional[str] = None,
|
||||||
footer_icon_url=None,
|
color: Optional[int] = None,
|
||||||
show_name=True,
|
footer_text: Optional[str] = None,
|
||||||
image_url=None,
|
footer_icon_url: Optional[str] = None,
|
||||||
thumbnail_url=None,
|
image_url: Optional[str] = None,
|
||||||
timestamp=None,
|
thumbnail_url: Optional[str] = None,
|
||||||
hide_author=False,
|
timestamp: Optional[datetime] = None,
|
||||||
hide_author_icon=False,
|
hide_name_in_description: bool = False,
|
||||||
hide_timestamp=False,
|
hide_time: bool = False,
|
||||||
) -> discord.Embed:
|
) -> discord.Embed:
|
||||||
if not hide_author:
|
"""
|
||||||
if not author_text:
|
Create a standard Lumi embed with the given parameters.
|
||||||
author_text = ctx.author.name
|
"""
|
||||||
elif show_name:
|
|
||||||
description = f"**{ctx.author.name}** {description}"
|
|
||||||
|
|
||||||
if not hide_author_icon and not author_icon_url:
|
theme_settings = {
|
||||||
author_icon_url = ctx.author.display_avatar.url
|
"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:
|
if user_name and not hide_name_in_description:
|
||||||
footer_text = "Luminara"
|
description = f"**{user_name}** {description}"
|
||||||
if not footer_icon_url:
|
|
||||||
footer_icon_url = CONST.LUMI_LOGO_TRANSPARENT
|
|
||||||
|
|
||||||
embed = discord.Embed(
|
embed: discord.Embed = discord.Embed(
|
||||||
title=title,
|
title=title,
|
||||||
description=description,
|
description=description,
|
||||||
color=color or CONST.COLOR_DEFAULT,
|
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,
|
|
||||||
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_author(
|
||||||
|
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 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:
|
if image_url:
|
||||||
embed.set_image(url=image_url)
|
embed.set_image(url=image_url)
|
||||||
if thumbnail_url:
|
if thumbnail_url:
|
||||||
embed.set_thumbnail(url=thumbnail_url)
|
embed.set_thumbnail(url=thumbnail_url)
|
||||||
|
|
||||||
return embed
|
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,
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in a new issue