1
0
Fork 0
mirror of synced 2024-06-18 18:44:33 +12:00

Catch failures to send messages on exceptions

This commit is contained in:
Phxntxm 2017-04-08 21:52:10 -05:00
parent f99afec44f
commit 3f2f7e77a3

57
bot.py
View file

@ -91,33 +91,36 @@ async def on_command_error(error, ctx):
except AttributeError: except AttributeError:
pass pass
if isinstance(error, commands.BadArgument): try:
fmt = "Please provide a valid argument to pass to the command: {}".format(error) if isinstance(error, commands.BadArgument):
await ctx.message.channel.send(fmt) fmt = "Please provide a valid argument to pass to the command: {}".format(error)
elif isinstance(error, commands.CheckFailure): await ctx.message.channel.send(fmt)
fmt = "You can't tell me what to do!" elif isinstance(error, commands.CheckFailure):
await ctx.message.channel.send(fmt) fmt = "You can't tell me what to do!"
elif isinstance(error, commands.CommandOnCooldown): await ctx.message.channel.send(fmt)
m, s = divmod(error.retry_after, 60) elif isinstance(error, commands.CommandOnCooldown):
fmt = "This command is on cooldown! Hold your horses! >:c\nTry again in {} minutes and {} seconds" \ m, s = divmod(error.retry_after, 60)
.format(round(m), round(s)) fmt = "This command is on cooldown! Hold your horses! >:c\nTry again in {} minutes and {} seconds" \
await ctx.message.channel.send(fmt) .format(round(m), round(s))
elif isinstance(error, commands.NoPrivateMessage): await ctx.message.channel.send(fmt)
fmt = "This command cannot be used in a private message" elif isinstance(error, commands.NoPrivateMessage):
await ctx.message.channel.send(fmt) fmt = "This command cannot be used in a private message"
elif isinstance(error, commands.MissingRequiredArgument): await ctx.message.channel.send(fmt)
await ctx.message.channel.send(error) elif isinstance(error, commands.MissingRequiredArgument):
else: await ctx.message.channel.send(error)
now = datetime.datetime.now() else:
with open("error_log", 'a') as f: now = datetime.datetime.now()
print("In server '{0.message.guild}' at {1}\nFull command: `{0.message.content}`".format(ctx, str(now)), with open("error_log", 'a') as f:
file=f) print("In server '{0.message.guild}' at {1}\nFull command: `{0.message.content}`".format(ctx, str(now)),
try: file=f)
traceback.print_tb(error.original.__traceback__, file=f) try:
print('{0.__class__.__name__}: {0}'.format(error.original), file=f) traceback.print_tb(error.original.__traceback__, file=f)
except: print('{0.__class__.__name__}: {0}'.format(error.original), file=f)
traceback.print_tb(error.__traceback__, file=f) except:
print('{0.__class__.__name__}: {0}'.format(error), file=f) traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
except discord.HTTPException:
pass
if __name__ == '__main__': if __name__ == '__main__':