Reputation: 25
I'm creating a clear command, and I want the bot after clearing confirming that it has cleared the messages.
My code :
@client.command(aliases=["del","delete","cl","clearmessages","deletemessages","deletemessage","clearmessage","cm"])
async def clear(ctx, amount=5):
if ctx.message.author.guild_permissions.manage_messages:
await ctx.channel.purge(limit=amount+1)
msg = await ctx.send(f"{amount} messages have been deleted!")
import asyncio
asyncio.sleep(15)
await msg.delete()
else:
await ctx.send("Your don't have manage messages perms!")
I also want the bot to delete its own message after some time
Upvotes: 0
Views: 66
Reputation:
A better way to write this command would be:
@client.command(aliases=['put alternative names here'])
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=5):
try:
await ctx.channel.purge(limit=amount+1)
await ctx.send(f"{amount} messages have been deleted!", delete_after=5)
except:
await ctx.send("Your don't have manage messages perms!")
Note - you will need the follow import statement:
from discord.ext import commands
Edit: You can get rid of the try, except loop and use the error handler instead
@client.event
async def on_command_error(ctx, error):
error = getattr(error, 'original', error)
if isinstance(error, commands.MissingPermissions):
await ctx.send("You don't have the required perms to carry out this command")
If you want it to be command specific so that you can add unique error messages:
@client.event
async def on_command_error(ctx, error):
error = getattr(error, 'original', error)
if isinstance(error, commands.MissingPermissions):
if ctx.command.name == "clear":
await ctx.send("You don't have manage messages perm")
Edit #2: Adding a limit
async def clear(ctx, amount: int):
if amount <= 500:
await ctx.channel.purge(limit=amount + 1)
await ctx.send(f"{amount} messages have been deleted!", delete_after=5)
else:
#error message
Upvotes: 1