Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. """Adds standardised attributes to BadArgument exceptions in discord.py.
  2.  
  3. The default `BadArgument` exception does not have any extra arguments,
  4. and the error messages generated are too different to easily convert
  5. into user-facing error messages.
  6.  
  7. This extension fixes that by capturing any `BadArgument` exceptions
  8. raised and re-raising them with three extra arguments:
  9.  
  10. * argument {str} -- The string that could not be converted
  11. * annotation {Any} -- The object the argument was annotated with
  12. * type_name {str} -- A formatted name of the target type
  13.  
  14. The `type_name` argument will generally return the name of the class.
  15. If the class name ends in "Converter", this suffix is stripped off.
  16.  
  17. """
  18.  
  19. import inspect
  20. from typing import Any
  21. from discord.ext import commands
  22.  
  23.  
  24. async def _inject_argument_info(self, ctx: commands.Context,
  25. converter: commands.Converter, argument: str,
  26. param: inspect.Parameter) -> Any:
  27. """Add argument and converter info to `BadArgument` exceptions.
  28.  
  29. This method is a replacement for the original `_actual_conversion`
  30. method of the `commands.Command` class. It forwards the call to the
  31. original method, but injects any `commands.BadArgument` exceptions
  32. raised with additional attributes to facilitate error message
  33. generation.
  34.  
  35. """
  36. # NOTE: This method forwards the call to the original _actual_conversion
  37. # method. Whenever a BadArgument exception is raised, it is injected with
  38. # useful arguments before being re-raised.
  39. # This should be more-or-less invisible to any other modules, aside from
  40. # the additional call and the injected arguments.
  41. try:
  42. return await self._original_actual_conversion(ctx, converter,
  43. argument, param)
  44. except commands.BadArgument as err:
  45. err.argument = argument
  46. err.annotation = param.annotation
  47. # Remove the "Converter"-suffix of object-specific converters. This
  48. # turns "MemberConverter" into "Member", etc.
  49. err.type_name = param.annotation.__name__.split('Converter')[:-1]
  50. raise err
  51.  
  52.  
  53. def setup(bot: commands.Bot) -> None:
  54. """Entry point for the bot extension loader."""
  55. cmd_cls = commands.Command
  56. # Store the original converter
  57. cmd_cls._original_actual_conversion = cmd_cls._actual_conversion
  58. # Add the BadArgument attribute injector
  59. cmd_cls._actual_conversion = _inject_argument_info
  60.  
  61.  
  62. def teardown(bot: commands.Bot) -> None:
  63. """Clean-up utility when unloading the extension."""
  64. cmd_cls = commands.Command
  65. # Restore the original converter
  66. cmd_cls._actual_conversion = cmd_cls._original_actual_conversion
  67. # Remove the backup
  68. del cmd_cls._original_actual_conversion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement