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

chore: Refactor trigger command outputs

This commit is contained in:
wlinator 2024-08-15 06:07:50 -04:00
parent 4af860a05e
commit b3fbb144dc
4 changed files with 84 additions and 140 deletions

View file

@ -1,7 +1,18 @@
{
"triggers_no_reactions_title": "No Custom Reactions Found",
"triggers_no_reactions_description": "There are no custom reactions set up yet.\n\nTo create a new custom reaction, use the following commands:\n`/trigger add emoji` - Add a new custom emoji reaction.\n`/trigger add response` - Add a new custom text reaction.\n\n**Emoji Reaction:**\nAn emoji reaction will react with a specific emoji when the trigger text is detected.\n\n**Text Reaction:**\nA text reaction will respond with a specific text message when the trigger text is detected.",
"triggers_add_description": "**Trigger Text:** `{0}`\n**Reaction Type:** {1}\n**Full Match:** `{2}`\n",
"triggers_type_emoji": "Emoji",
"triggers_type_text": "Text",
"triggers_add_emoji_details": "**Emoji ID:** `{0}`",
"triggers_add_text_details": "**Response:** `{0}`",
"triggers_add_author": "Custom Reaction Created",
"trigger_limit_reached": "Failed to add custom reaction. You have reached the limit of 100 custom reactions for this server.",
"trigger_already_exists": "Failed to add custom reaction. This text already contains another trigger. To avoid unexpected behavior, please delete it before adding a new one.",
"triggers_delete_not_found_author": "Custom Reaction Not Found",
"triggers_not_found": "no custom reaction found with the provided ID.",
"triggers_not_deleted": "something went wrong while trying to delete this trigger.",
"triggers_not_added": "failed to add custom reaction. Please try again.",
"triggers_delete_author": "Custom Reaction Deleted",
"triggers_delete_description": "custom reaction has been successfully deleted.",
"triggers_list_trigger_text": "**Trigger Text:** `{0}`",

View file

@ -1,95 +0,0 @@
import datetime
from typing import Optional
import discord
from config.parser import JsonCache
from lib import formatter
resources = JsonCache.read_json("art")
check_icon = resources["icons"]["check"]
cross_icon = resources["icons"]["cross"]
exclaim_icon = resources["icons"]["exclaim"]
logo = resources["logo"]["transparent"]
def create_embed(
title: str,
description: str,
color: int,
icon_url: str,
) -> discord.Embed:
embed = discord.Embed(
color=color,
description=description,
)
embed.set_author(name=title, icon_url=icon_url)
embed.set_footer(text="Reaction Service", icon_url=logo)
embed.timestamp = datetime.datetime.now(datetime.timezone.utc)
return embed
def create_creation_embed(
trigger_text: str,
response: Optional[str],
emoji_id: Optional[int],
is_emoji: bool,
is_full_match: bool,
) -> discord.Embed:
trigger_text = formatter.shorten(trigger_text, 50)
if response:
response = formatter.shorten(response, 50)
description = (
f"**Trigger Text:** `{trigger_text}`\n"
f"**Reaction Type:** {'Emoji' if is_emoji else 'Text'}\n"
f"{f'**Emoji ID:** `{emoji_id}`' if is_emoji else f'**Response:** `{response}`'}\n"
f"**Full Match:** `{is_full_match}`"
)
return create_embed("Custom Reaction Created", description, 0xFF8C00, check_icon)
def create_failure_embed(
trigger_text: str,
is_emoji: bool,
limit_reached: bool = False,
trigger_already_exists: bool = False,
) -> discord.Embed:
trigger_text = formatter.shorten(trigger_text, 50)
description = f"**Trigger Text:** `{trigger_text}`\n"
if limit_reached:
description += "Failed to add custom reaction. You have reached the limit of 100 custom reactions for this server."
elif trigger_already_exists:
description += "Failed to add custom reaction. This text already contains another trigger. To avoid unexpected behavior, please delete it before adding a new one."
else:
description += "Failed to add custom reaction."
return create_embed(
"Custom Reaction Creation Failed",
description,
0xFF4500,
cross_icon,
)
def create_no_triggers_embed() -> discord.Embed:
description = (
"There are no custom reactions set up yet.\n\n"
"To create a new custom reaction, use the following commands:\n"
"`/trigger add emoji` - Add a new custom emoji reaction.\n"
"`/trigger add response` - Add a new custom text reaction.\n\n"
"**Emoji Reaction:**\n"
"An emoji reaction will react with a specific emoji when the trigger text is detected.\n\n"
"**Text Reaction:**\n"
"A text reaction will respond with a specific text message when the trigger text is detected."
)
return create_embed(
"No Custom Reactions Found",
description,
0xFF8C00,
exclaim_icon,
)

View file

@ -1,9 +1,11 @@
from typing import Optional
from discord.ext import bridge
from lib.embeds.triggers import create_creation_embed, create_failure_embed
from services.reactions_service import CustomReactionsService
from lib.exceptions.LumiExceptions import LumiException
from lib.constants import CONST
from lib.embed_builder import EmbedBuilder
from lib import formatter
async def add_reaction(
@ -22,20 +24,15 @@ async def add_reaction(
creator_id: int = ctx.author.id
if not await check_reaction_limit(
ctx,
reaction_service,
guild_id,
trigger_text,
is_emoji,
):
return
if not await check_existing_trigger(
ctx,
reaction_service,
guild_id,
trigger_text,
is_emoji,
):
return
@ -47,51 +44,63 @@ async def add_reaction(
emoji_id=emoji_id,
is_emoji=is_emoji,
is_full_match=is_full_match,
is_global=False, # only bot admins can create global custom reactions
is_global=False,
)
if success:
embed = create_creation_embed(
trigger_text,
response,
if not success:
raise LumiException(CONST.STRINGS["triggers_not_added"])
trigger_text = formatter.shorten(trigger_text, 50)
if response:
response = formatter.shorten(response, 50)
embed = EmbedBuilder.create_success_embed(
ctx,
author_text=CONST.STRINGS["triggers_add_author"],
description="",
footer_text=CONST.STRINGS["triggers_reaction_service_footer"],
show_name=False,
)
embed.description += CONST.STRINGS["triggers_add_description"].format(
trigger_text,
CONST.STRINGS["triggers_type_emoji"]
if is_emoji
else CONST.STRINGS["triggers_type_text"],
is_full_match,
)
if is_emoji:
embed.description += CONST.STRINGS["triggers_add_emoji_details"].format(
emoji_id,
is_emoji,
is_full_match,
)
else:
embed = create_failure_embed(trigger_text, is_emoji)
embed.description += CONST.STRINGS["triggers_add_text_details"].format(response)
await ctx.respond(embed=embed)
async def check_reaction_limit(
ctx: bridge.Context,
reaction_service: CustomReactionsService,
guild_id: int,
trigger_text: str,
is_emoji: bool,
) -> bool:
if await reaction_service.count_custom_reactions(guild_id) >= 100:
embed = create_failure_embed(trigger_text, is_emoji, limit_reached=True)
await ctx.respond(embed=embed)
return False
limit_reached = await reaction_service.count_custom_reactions(guild_id) >= 100
if limit_reached:
raise LumiException(CONST.STRINGS["trigger_limit_reached"])
return True
async def check_existing_trigger(
ctx: bridge.Context,
reaction_service: CustomReactionsService,
guild_id: int,
trigger_text: str,
is_emoji: bool,
) -> bool:
existing_trigger = await reaction_service.find_trigger(guild_id, trigger_text)
if existing_trigger:
embed = create_failure_embed(
trigger_text,
is_emoji,
trigger_already_exists=True,
)
await ctx.respond(embed=embed)
return False
raise LumiException(CONST.STRINGS["trigger_already_exists"])
return True

View file

@ -1,9 +1,9 @@
from discord.ext import bridge, pages
from lib.constants import CONST
from lib.embed_builder import EmbedBuilder
from lib.embeds.triggers import create_no_triggers_embed
from services.reactions_service import CustomReactionsService
from typing import Any, Dict, List
from lib import formatter
import discord
@ -17,7 +17,13 @@ async def list_reactions(ctx: bridge.Context) -> None:
reactions: List[Dict[str, Any]] = await reaction_service.find_all_by_guild(guild_id)
if not reactions:
embed: discord.Embed = create_no_triggers_embed()
embed: discord.Embed = EmbedBuilder.create_warning_embed(
ctx,
author_text=CONST.STRINGS["triggers_no_reactions_title"],
description=CONST.STRINGS["triggers_no_reactions_description"],
footer_text=CONST.STRINGS["triggers_reaction_service_footer"],
show_name=False,
)
await ctx.respond(embed=embed)
return
@ -33,27 +39,40 @@ async def list_reactions(ctx: bridge.Context) -> None:
show_name=False,
)
embed.description = "\n".join(
[
CONST.STRINGS["triggers_list_trigger_text"].format(
reaction["trigger_text"],
),
CONST.STRINGS["triggers_list_reaction_type"].format(
"Emoji" if reaction["is_emoji"] else "Text",
),
CONST.STRINGS["triggers_list_emoji_id"].format(reaction["emoji_id"])
description_lines = [
CONST.STRINGS["triggers_list_trigger_text"].format(
formatter.shorten(reaction["trigger_text"], 50),
),
CONST.STRINGS["triggers_list_reaction_type"].format(
CONST.STRINGS["triggers_type_emoji"]
if reaction["is_emoji"]
else CONST.STRINGS["triggers_list_response"].format(
reaction["response"],
else CONST.STRINGS["triggers_type_text"],
),
]
if reaction["is_emoji"]:
description_lines.append(
CONST.STRINGS["triggers_list_emoji_id"].format(reaction["emoji_id"]),
)
else:
description_lines.append(
CONST.STRINGS["triggers_list_response"].format(
formatter.shorten(reaction["response"], 50),
),
)
description_lines.extend(
[
CONST.STRINGS["triggers_list_full_match"].format(
reaction["is_full_match"],
"True" if reaction["is_full_match"] else "False",
),
CONST.STRINGS["triggers_list_usage_count"].format(
reaction["usage_count"],
),
],
)
embed.description = "\n".join(description_lines)
pages_list.append(embed)
paginator: pages.Paginator = pages.Paginator(pages=pages_list, timeout=180.0)