Repository navigation
Answer failed commands and clicks, and tell hub errors apart #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
15efc13
Answer failed commands and clicks, and tell hub errors apart
rayakame d254325
Keep interaction tokens out of the error logs
rayakame 40ae75d
Say that an error after a public defer is public
rayakame bb886eb
Name the error extension's functions after what they do
rayakame File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,100 @@ | ||
| """Answers a failed command or click that no handler of the bot took, and logs it. | ||
|
|
||
| Needs a `Catalog` with kit's texts (`kit.LOCALES`) and a risa client in lightbulb's dependency injection. Its handlers | ||
| run last, so a bot's own error handlers at the default priority come first. | ||
|
|
||
| The answer is visible to the caller only, except after a public defer: it then replaces the "thinking" message, and | ||
| Discord keeps that message's visibility. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import re | ||
| import secrets | ||
| import traceback | ||
| from collections.abc import Sequence | ||
|
|
||
| import hikari | ||
| import lightbulb | ||
| import risa | ||
| from risa import ui | ||
|
|
||
| from librebots.kit.hub import HubUnavailableError | ||
| from librebots.kit.i18n import Catalog | ||
|
|
||
| __all__ = ["loader"] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
| _PRIORITY = -100 | ||
| _RED = 0xE24B4A | ||
| # hikari puts the request URL into its HTTP errors, and these URLs carry the interaction token. | ||
| _TOKEN_IN_URL = re.compile(r"(/(?:interactions|webhooks)/\d+/)[^/\s?#']+") | ||
|
|
||
| loader = lightbulb.Loader() | ||
|
|
||
|
|
||
| @loader.error_handler(priority=_PRIORITY) | ||
| async def _answer_failed_command( | ||
| exc: lightbulb.exceptions.ExecutionPipelineFailedException, catalog: Catalog = lightbulb.di.INJECTED | ||
| ) -> bool: | ||
| ctx = exc.context | ||
| layout = _build_error_reply( | ||
| catalog, ctx.interaction.locale, exc.causes, f"Command /{ctx.command_data.qualified_name}" | ||
| ) | ||
| try: | ||
| await ctx.respond(**ui.render(layout), ephemeral=True) | ||
| except hikari.HTTPError as error: | ||
| _LOGGER.warning("Could not tell the caller that a command failed: %s", _redact_tokens(str(error))) | ||
| return True | ||
|
|
||
|
|
||
| async def _answer_failed_click( | ||
| pipeline: risa.ExecutionPipeline, ctx: risa.ComponentContext, catalog: Catalog = risa.INJECTED | ||
| ) -> bool: | ||
| layout = _build_error_reply( | ||
| catalog, ctx.interaction.locale, [failure.error for failure in pipeline.failures], "A click" | ||
| ) | ||
| try: | ||
| await ctx.respond(**ui.render(layout), ephemeral=True) | ||
| except hikari.HTTPError as error: | ||
| _LOGGER.warning("Could not tell the clicker that a click failed: %s", _redact_tokens(str(error))) | ||
| return True | ||
|
|
||
|
|
||
| # Resolving risa's client while the extension loads would freeze the registry before the bot is done with it. | ||
| @loader.listener(hikari.StartedEvent) | ||
| async def _register_click_fallback(_: hikari.StartedEvent, views: risa.Client = lightbulb.di.INJECTED) -> None: | ||
| views.error_handler(_answer_failed_click, priority=_PRIORITY) | ||
|
|
||
|
|
||
| def _build_error_reply( | ||
| catalog: Catalog, locale: hikari.Locale | str, errors: Sequence[Exception], failed: str | ||
| ) -> ui.Container: | ||
| key, reference = _log_and_pick_text(errors, failed) | ||
| text = catalog.text(locale, key) if reference is None else catalog.text(locale, key, reference=reference) | ||
| return ui.Container( | ||
| ui.TextDisplay(f"### {catalog.text(locale, f'{key}.title')}"), | ||
| ui.TextDisplay(text), | ||
| accent_color=_RED, | ||
| ) | ||
|
|
||
|
|
||
| def _log_and_pick_text(errors: Sequence[Exception], failed: str) -> tuple[str, str | None]: | ||
| for error in errors: | ||
| if isinstance(error, HubUnavailableError): | ||
| _LOGGER.warning("%s failed because the hub is unavailable: %s", failed, _redact_tokens(str(error))) | ||
| return "kit-error-hub", None | ||
| for error in errors: | ||
| if isinstance(error, hikari.ForbiddenError): | ||
| _LOGGER.info("%s failed for a missing permission: %s", failed, _redact_tokens(str(error))) | ||
| return "kit-error-forbidden", None | ||
| reference = secrets.token_hex(3) | ||
| for error in errors: | ||
| described = _redact_tokens("".join(traceback.format_exception(error)).rstrip()) | ||
| _LOGGER.error("%s failed, reference %s\n%s", failed, reference, described) | ||
| return "kit-error-unknown", reference | ||
|
|
||
|
|
||
| def _redact_tokens(text: str) -> str: | ||
| return _TOKEN_IN_URL.sub(r"\1***", text) | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,6 @@ | ||
| kit-error-hub = Ich komme gerade nicht an meine Daten. Versuch es in einer Minute noch einmal. | ||
| .title = Gerade nicht erreichbar | ||
| kit-error-forbidden = Mir fehlt dafür eine Berechtigung. Bitte einen Server-Admin, meine Rolle zu prüfen. | ||
| .title = Berechtigung fehlt | ||
| kit-error-unknown = Versuch es später noch einmal. Wenn es wieder passiert, gib dem Betreiber des Bots diese Referenz: `{ $reference }` | ||
| .title = Etwas ist schiefgelaufen |
This file contains hidden or 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,6 @@ | ||
| kit-error-hub = I can't reach my data at the moment. Try again in a minute. | ||
| .title = Not reachable right now | ||
| kit-error-forbidden = I'm missing a permission for this. Ask a server admin to check my role. | ||
| .title = Missing permission | ||
| kit-error-unknown = Try again later. If it keeps happening, give the bot's operator this reference: `{ $reference }` | ||
| .title = Something went wrong |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.