From 0db4f62afc8a5087e52b76c7b46579d78af9fa62 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 9 Apr 2022 22:22:44 -0400 Subject: [PATCH] //WIP v3 Clear Messages Changes - Bug - More error checking - Limit the user to 100 messages - More help text --- modules/ClearChannelMessages/cog.py | 47 ++++++++++++++++------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/modules/ClearChannelMessages/cog.py b/modules/ClearChannelMessages/cog.py index 137884c..1b17f49 100644 --- a/modules/ClearChannelMessages/cog.py +++ b/modules/ClearChannelMessages/cog.py @@ -10,37 +10,42 @@ class ClearMessages(commands.Cog): @commands.command(name='clear', help="Use this command to clear a given number of messages from the channel it is called in.\n" + "There is a limit of 100 messages. Please be patient, it may take a while to process a large request." "Example command:\n" "\t@ clear\n" "\t@ clear 10", breif="Clear x messages in the channel it's called in") - async def clear(self, ctx, amount: int = 2): + async def clear(self, ctx, amount=2): member = ctx.author.display_name member_id = ctx.author.id mtn_member = f"<@{member_id}>" - if amount < 0: - ctx.send(f"{member}, the number needs to be positive...") + if isinstance(amount, int): + if not amount > 0: + ctx.channel.send(f"{member}, the number needs to be positive...") + + else: + LOGGER.info(f"Clear {amount} messages requested by {member}") + + authors = {} + async for message in ctx.channel.history(limit=amount): + if message.author not in authors: + authors[message.author] = 1 + else: + authors[message.author] += 1 + await message.delete() + + msg = f"{mtn_member}, I deleted {sum(authors.values())} messages from {len(authors.keys())} users:\n" + LOGGER.debug(f"Deleted {sum(authors.values())} messages from {ctx.message.channel}") + + for author in authors.keys(): + msg += f"\t{str(author).split('#', 1)[0]}: {authors[author]}\n" + LOGGER.debug(f"Deleted {authors[author]} messages from {author}") + + await ctx.channel.send(msg) else: - LOGGER.info(f"Clear {amount} messages requested by {member}") - - authors = {} - async for message in ctx.channel.history(limit=amount): - if message.author not in authors: - authors[message.author] = 1 - else: - authors[message.author] += 1 - await message.delete() - - msg = f"{mtn_member}, I deleted {sum(authors.values())} messages from {len(authors.keys())} users:\n" - LOGGER.debug(f"Deleted {sum(authors.values())} messages from {ctx.message.channel}") - - for author in authors.keys(): - msg += f"\t{str(author).split('#', 1)[0]}: {authors[author]}\n" - LOGGER.debug(f"Deleted {authors[author]} messages from {author}") - - await ctx.send(msg) + ctx.channel.send(f"{member}, you should check out the 'help' section...") def setup(bot: commands.Bot):