You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.5KB

  1. import string
  2. import discord
  3. import datetime
  4. import traceback
  5. from discord.ext import commands
  6. from Roxbot.settings import guild_settings
  7. class ErrHandle:
  8. def __init__(self, bot_client):
  9. self.bot = bot_client
  10. self.dev = False # For debugging
  11. async def on_error(self, event, *args, **kwargs):
  12. if self.dev:
  13. traceback.print_exc()
  14. else:
  15. embed = discord.Embed(title="Roxbot Error", colour=0xe74c3c) # Red
  16. embed.add_field(name='Event', value=event)
  17. embed.description = '```py\n{}\n```'.format(traceback.format_exc())
  18. embed.timestamp = datetime.datetime.utcnow()
  19. await self.owner.send(embed=embed)
  20. async def on_command_error(self, ctx, error):
  21. self.owner = self.bot.get_user(self.bot.owner_id)
  22. err_colour = 0x992d22
  23. if self.dev:
  24. raise error
  25. elif isinstance(error, commands.CommandInvokeError):
  26. embed = discord.Embed(title='Command Error', colour=err_colour)
  27. embed.description = str(error)
  28. embed.add_field(name='Server', value=ctx.guild)
  29. embed.add_field(name='Channel', value=ctx.channel.mention)
  30. embed.add_field(name='User', value=ctx.author)
  31. embed.add_field(name='Message', value=ctx.message.content)
  32. embed.timestamp = datetime.datetime.utcnow()
  33. await ctx.send(embed=embed)
  34. else:
  35. if isinstance(error, commands.NoPrivateMessage):
  36. embed = discord.Embed(description="This command cannot be used in private messages.")
  37. elif isinstance(error, commands.DisabledCommand):
  38. embed = discord.Embed(description="This command is disabled.")
  39. elif isinstance(error, commands.MissingRequiredArgument):
  40. embed = discord.Embed(description="Argument missing. {}".format(error.args[0]))
  41. elif isinstance(error, commands.BadArgument):
  42. embed = discord.Embed(description="Invalid Argument given. {}".format(error.args[0]))
  43. elif isinstance(error, commands.TooManyArguments):
  44. embed = discord.Embed(description="Too many arguments given.")
  45. elif isinstance(error, commands.CommandNotFound):
  46. cc = guild_settings.get(ctx.guild).custom_commands
  47. if ctx.invoked_with in cc["1"]:
  48. embed = None
  49. elif any(x in string.punctuation for x in ctx.message.content.strip(ctx.prefix)[0]):
  50. # Should avoid punctuation emoticons. Checks all of the command for punctuation in the string.
  51. embed = None
  52. else:
  53. embed = discord.Embed(description="That Command doesn't exist.")
  54. elif isinstance(error, commands.BotMissingPermissions):
  55. embed = discord.Embed(description="{}".format(error.args[0].replace("Bot", "Roxbot")))
  56. elif isinstance(error, commands.MissingPermissions):
  57. embed = discord.Embed(description="{}".format(error.args[0]))
  58. elif isinstance(error, commands.NotOwner):
  59. embed = discord.Embed(description="You do not have permission to do this. You are not Roxie!")
  60. elif isinstance(error, commands.CommandOnCooldown):
  61. embed = discord.Embed(description="This command is on cooldown, please wait {:.2f} seconds before trying again.".format(error.retry_after))
  62. elif isinstance(error, commands.CheckFailure):
  63. embed = discord.Embed(description="You do not have permission to do this. Back off, thot!")
  64. elif isinstance(error, commands.CommandError):
  65. embed = discord.Embed(description="Command Error. {}".format(error.args[0]))
  66. else:
  67. embed = discord.Embed(
  68. description="Placeholder embed. If you see this please message {}.".format(str(self.owner)))
  69. if embed:
  70. embed.colour = err_colour
  71. await ctx.send(embed=embed, delete_after=8)
  72. def setup(bot_client):
  73. bot_client.add_cog(ErrHandle(bot_client))