delete_msg.py 1.3 KB

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