delete_msg.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import discord
  2. from discord.ext import commands
  3. from discord.commands import Option
  4. from discord.commands import slash_command
  5. class DeleteMessage(commands.Cog):
  6. def __init__(self, bot: discord.Bot):
  7. self.bot = bot
  8. #Command initialization
  9. @slash_command(name="delete", description="Delete a amount messages from this server")
  10. async def delete(
  11. self,
  12. ctx,
  13. amount: int = Option(int, "Select the amount of the messages to delete", required=True),
  14. ):
  15. if not ctx.author.guild_permissions.manage_messages:
  16. await ctx.respond("You don't have the permission to use this command!", ephemeral=True)
  17. return
  18. else:
  19. if amount is None or amount <= 0:
  20. await ctx.respond("Please provide a valid number greater than 0.", ephemeral=True)
  21. return
  22. elif amount > 100:
  23. await ctx.respond("You can only delete up to 100 messages at a time.", ephemeral=True)
  24. return
  25. deleted_messages = []
  26. async for msg in ctx.channel.history(limit=amount):
  27. deleted_messages.append(msg)
  28. if len(deleted_messages) == 0:
  29. await ctx.respond("No messages found to delete.", ephemeral=True)
  30. return
  31. await ctx.channel.delete_messages(deleted_messages)
  32. await ctx.respond(f"Deleted {len(deleted_messages)} messages.", ephemeral=True)
  33. def setup(bot: discord.Bot):
  34. bot.add_cog(DeleteMessage(bot))