Skip to content

Instantly share code, notes, and snippets.

@TrudeEH
Created September 1, 2024 17:14
Show Gist options
  • Save TrudeEH/b28264ad4ce2704d7f60c0e82b76bc40 to your computer and use it in GitHub Desktop.
Save TrudeEH/b28264ad4ce2704d7f60c0e82b76bc40 to your computer and use it in GitHub Desktop.
# Delete all messages in a channel/DM sent from a Discord bot.
# Useful to delete bot responses for testing/privacy/security reasons.
# To delete all messages, type !clear
# Uncomment the excluded code to add discord actions support.
import discord
from discord.ext import commands
intents = discord.Intents.default() # Use default intents for now
client = commands.Bot(command_prefix="!", intents=intents) # Use commands.Bot
@client.event
async def on_ready():
print(f"Logged in as {client.user}")
# try:
# synced = await client.tree.sync()
# print(f"Synced {len(synced)} command(s)")
# except Exception as e:
# print(e)
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == "!clear":
channel = message.channel
async for msg in channel.history(limit=100):
if msg.author == client.user:
await msg.delete()
# @client.tree.command(
# name="clear", description="Delete all messages in the current channel"
# )
# async def clear(interaction: discord.Interaction):
# await interaction.response.defer()
# channel = interaction.channel
# async for msg in channel.history(limit=100):
# if msg.author == client.user:
# await msg.delete()
# await interaction.response.send_message("All messages deleted.", ephemeral=True)
client.run("SECRET TOKEN HERE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment