Skip to content

Commit

Permalink
Added Emojis, rewritten error embed, repaired error handling.
Browse files Browse the repository at this point in the history
Signed-off-by: robertsokola <[email protected]>
  • Loading branch information
TheXer committed Jun 29, 2023
1 parent 7d4b3e9 commit 965bffa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 55 deletions.
54 changes: 30 additions & 24 deletions cogs/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,7 @@
from discord.ext import commands
from loguru import logger


class PrettyError(CommandInvokeError):
def __init__(self, message: str, interaction: Interaction, inner_exception: Exception | None = None):
super().__init__(interaction.command, inner_exception)
self.message = message
self.interaction = interaction

async def send(self):
send_args = {
"content": f"{self.message}",
"ephemeral": False,
}
if not self.interaction.response.is_done():
await self.interaction.response.send_message(**send_args)
else:
await self.interaction.followup.send(**send_args)


class TooManyOptionsError(PrettyError):
pass


class TooFewOptionsError(PrettyError):
pass
from src.ui.embeds import ErrorMessage


class Error(commands.Cog):
Expand All @@ -47,6 +24,35 @@ async def on_app_command_error(self, interaction: Interaction, error: Exception)
await error.send()
case _:
logger.critical(error)
await interaction.response.send_message(
embed=ErrorMessage(
"Tato zpráva by se nikdy zobrazit správně neměla. "
"Jsi borec, že jsi mi dokázal rozbít Jáchyma, nechceš mi o tom napsat do issues na githubu?"
)
)


class PrettyError(CommandInvokeError):
"""Pretty errors useful for raise keyword"""

def __init__(self, message: str, interaction: Interaction, inner_exception: Exception | None = None):
super().__init__(interaction.command, inner_exception)
self.message = message
self.interaction = interaction

async def send(self):
if not self.interaction.response.is_done():
await self.interaction.response.send_message(embed=ErrorMessage(self.message))
else:
await self.interaction.followup.send(embed=ErrorMessage(self.message))


class TooManyOptionsError(PrettyError):
pass


class TooFewOptionsError(PrettyError):
pass


async def setup(bot):
Expand Down
9 changes: 4 additions & 5 deletions cogs/poll_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import discord
from discord import app_commands
from discord.app_commands import Transform, Transformer
from discord.ext import commands
from loguru import logger

Expand All @@ -13,7 +14,7 @@
from src.ui.poll_view import PollView


class OptionsTransformer(app_commands.Transformer):
class OptionsTransformer(Transformer):
async def transform(
self, interaction: discord.Interaction, option: str
) -> TooManyOptionsError | TooFewOptionsError | list[str]:
Expand Down Expand Up @@ -62,11 +63,9 @@ async def pool(
self,
interaction: discord.Interaction,
question: str,
answer: app_commands.Transform[list[str, ...], OptionsTransformer],
answer: Transform[list[str, ...], OptionsTransformer],
) -> discord.Message:
await interaction.response.send_message(
embed=PollEmbedBase("Nahrávám anketu..."),
)
await interaction.response.send_message(embed=PollEmbedBase("Nahrávám anketu..."))
message = await interaction.original_response()

poll = Poll(
Expand Down
45 changes: 19 additions & 26 deletions src/ui/embeds.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import json
import pathlib
from datetime import datetime, timedelta
from datetime import datetime

import discord
from discord.colour import Color
from discord.colour import Color, Colour

from src.ui.emojis import ScoutEmojis
from src.ui.poll import Poll


class CooldownErrorEmbed(discord.Embed):
LIMIT = 4
class ErrorMessage(discord.Embed):
def __init__(self, message: str):
title = "⚠️ Jejda, někde se stala chyba..."

def __init__(self, seconds: float):
self.seconds = round(seconds)
formatted_date = discord.utils.format_dt(
datetime.now() + timedelta(seconds=10),
"R",
description = (
f"{message}\n\n"
f"{ScoutEmojis.FLEUR_DE_LIS} *Pokud máš pocit, že tohle by chyba být neměla, "
f"napiš [sem](https://github.com/TheXer/Jachym/issues/new/choose)*"
)

self.set_footer(text="Uděláno s ♥!")

super().__init__(
title=f"⚠️ Vydrž! Další anketu můžeš založit {formatted_date}! ⚠️",
colour=Color.red(),
title=title,
description=description,
colour=Colour.red(),
timestamp=datetime.now(),
)

def correct_czech_writing(self) -> str:
if self.seconds > self.LIMIT:
return f"{self.seconds} sekund"
if self.LIMIT >= self.seconds > 1:
return f"{self.seconds} sekundy"
return "sekundu"


class PollEmbedBase(discord.Embed):
def __init__(self, question) -> None:
Expand All @@ -43,7 +41,9 @@ def __init__(self, poll: Poll):
super().__init__(poll.question)
self.answers = poll.options
self._add_options()
self._add_timestamp()

self.set_footer(text="Uděláno s ♥!")
self.timestamp = datetime.now()

def _add_options(self):
for index, option in enumerate(self.answers):
Expand All @@ -53,13 +53,6 @@ def _add_options(self):
inline=False,
)

def _add_timestamp(self):
self.add_field(
name="",
value=f"Anketa byla vytvořena {discord.utils.format_dt(datetime.now(), 'R')}",
inline=False,
)


class EmbedFromJSON(discord.Embed):
PATH = pathlib.Path("src/text_json/cz_text.json")
Expand Down
9 changes: 9 additions & 0 deletions src/ui/emojis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dataclasses import dataclass


@dataclass()
class ScoutEmojis:
FLEUR_DE_LIS = "<:lilie:814103053208649778>"
SCOUT_SCARF = "<:satek:814103053614972938>"
POTKANI = "<:Potkani:803954899250839582>"
SCOUTS = "<:skauti:814103051878531112>"

0 comments on commit 965bffa

Please sign in to comment.