-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide sane UI defaults + error handling
- Loading branch information
Showing
9 changed files
with
98 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from .embeds import Embed as Embed | ||
from .embeds import ErrorEmbed as ErrorEmbed | ||
from .embeds import Embed as Embed, ErrorEmbed as ErrorEmbed | ||
from .logger import RodhajLogger as RodhajLogger | ||
from .modals import RoboModal as RoboModal | ||
from .time import human_timedelta as human_timedelta | ||
from .tree import RodhajCommandTree as RodhajCommandTree | ||
from .views import RoboView as RoboView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import traceback | ||
|
||
import discord | ||
|
||
from .embeds import ErrorEmbed | ||
|
||
|
||
def produce_error_embed(error: Exception) -> ErrorEmbed: | ||
error_traceback = "\n".join(traceback.format_exception_only(type(error), error)) | ||
embed = ErrorEmbed() | ||
desc = f""" | ||
Uh oh! It seems like there was an issue. Ask the devs for help. | ||
**Error**: | ||
```{error_traceback}``` | ||
""" | ||
embed.description = desc | ||
embed.set_footer(text="Happened At") | ||
embed.timestamp = discord.utils.utcnow() | ||
return embed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import discord | ||
|
||
from .errors import produce_error_embed | ||
|
||
NO_CONTROL_MSG = "This modal cannot be controlled by you, sorry!" | ||
|
||
|
||
class RoboModal(discord.ui.Modal): | ||
"""Subclassed `discord.ui.Modal` that includes sane default configs""" | ||
|
||
def __init__(self, interaction: discord.Interaction, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.interaction = interaction | ||
|
||
async def interaction_check(self, interaction: discord.Interaction, /) -> bool: | ||
if interaction.user and interaction.user.id in ( | ||
self.interaction.client.application.owner.id, # type: ignore | ||
self.interaction.user.id, | ||
): | ||
return True | ||
await interaction.response.send_message(NO_CONTROL_MSG, ephemeral=True) | ||
return False | ||
|
||
async def on_error( | ||
self, interaction: discord.Interaction, error: Exception, / | ||
) -> None: | ||
await interaction.response.send_message( | ||
embed=produce_error_embed(error), ephemeral=True | ||
) | ||
self.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
from .paginator import HajPages as HajPages | ||
from .simple_pages import SimplePages as SimplePages | ||
from .sources import EmbedListSource as EmbedListSource | ||
from .sources import SimplePageSource as SimplePageSource | ||
from .sources import ( | ||
EmbedListSource as EmbedListSource, | ||
SimplePageSource as SimplePageSource, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,14 @@ | ||
import traceback | ||
|
||
import discord | ||
from discord import app_commands | ||
from discord.utils import utcnow | ||
from libs.utils import ErrorEmbed | ||
|
||
from .errors import produce_error_embed | ||
|
||
class RodhajCommandTree(app_commands.CommandTree): | ||
def build_error_embed(self, error: app_commands.AppCommandError) -> ErrorEmbed: | ||
error_traceback = "\n".join(traceback.format_exception_only(type(error), error)) | ||
embed = ErrorEmbed() | ||
embed.description = f""" | ||
Uh oh! It seems like the command ran into an issue! | ||
**Error**: | ||
``` | ||
{error_traceback} | ||
``` | ||
""" | ||
embed.set_footer(text="Happened At") | ||
embed.timestamp = utcnow() | ||
return embed | ||
|
||
# Later on if we needed global interaction checks, we can do it here | ||
class RodhajCommandTree(app_commands.CommandTree): | ||
async def on_error( | ||
self, interaction: discord.Interaction, error: app_commands.AppCommandError | ||
) -> None: | ||
await interaction.response.send_message(embed=self.build_error_embed(error)) | ||
await interaction.response.send_message(embed=produce_error_embed(error)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters