From bd2411289359f2103f7f946ca83c24bab50a2d1c Mon Sep 17 00:00:00 2001 From: sgoudham Date: Fri, 24 Jul 2020 21:24:23 +0100 Subject: [PATCH] Adding error handling for purge command --- cogs/moderation.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cogs/moderation.py b/cogs/moderation.py index 50c99e0b..f43609ff 100644 --- a/cogs/moderation.py +++ b/cogs/moderation.py @@ -1,3 +1,5 @@ +import asyncio + from discord import Member from discord.ext import commands from discord.ext.commands import command, guild_only, has_guild_permissions @@ -84,9 +86,17 @@ class Moderation(commands.Cog): async for message in channel.history(limit=amount): messages.append(message) - - await channel.delete_messages(messages) - msg = await ctx.send(f"{ctx.author.mention} {amount} messages deleted!") + try: + await channel.delete_messages(messages) + msg = await ctx.send(f"{ctx.author.mention} {amount} messages deleted!") + except Exception as ex: + print(ex) + await ctx.send("Error! {}".format(ex)) + + # Let the user read the message for 5 seconds + await asyncio.sleep(5) + # Delete the message + await msg.delete() def setup(bot):