Skip to content

Instantly share code, notes, and snippets.

@ethnh
Created March 22, 2025 23:51
Show Gist options
  • Save ethnh/42795700a94014f4ffed28e54b4017c0 to your computer and use it in GitHub Desktop.
Save ethnh/42795700a94014f4ffed28e54b4017c0 to your computer and use it in GitHub Desktop.
Discord.py-selfbot delete via discord data export archive messages index.json
import json
import os
import csv
import time
from datetime import datetime
from discord.ext import commands
# Load the index.json file
with open("index.json", "r") as index_file:
index_data = json.load(index_file)
# Your Discord token
TOKEN = "YOUR_DISCORD_TOKEN"
# Log file setup
log_file = "purge_log.csv"
# Create or clear the log file
if not os.path.exists(log_file):
with open(log_file, mode='w', newline='') as log:
log_writer = csv.writer(log)
log_writer.writerow(["Timestamp", "ChannelName", "ChannelId", "GuildId", "MessageId", "Status"])
# Function to check if a channel has already been marked as a failure
def is_channel_marked_as_failure(channel_id):
if not os.path.exists(log_file):
return False
with open(log_file, mode='r') as log:
log_reader = csv.reader(log)
next(log_reader) # Skip header
for row in log_reader:
if row[2] == channel_id and "Failure" in row[5]:
return True
return False
# Function to log the result
def log_result(timestamp, channel_name, channel_id, guild_id, message_id, status):
with open(log_file, mode='a', newline='') as log:
log_writer = csv.writer(log)
log_writer.writerow([timestamp, channel_name, channel_id, guild_id, message_id, status])
# Initialize bot
bot = commands.Bot(command_prefix="!", self_bot=True)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
# Iterate through each channel in the index.json
for channel_id, channel_name in index_data.items():
if is_channel_marked_as_failure(channel_id):
print(f"Skipping channel {channel_name} ({channel_id}) as it has already been marked as failure.")
continue
channel_folder = os.path.join("path_to_your_archive", channel_id)
# Load the channel.json file
with open(os.path.join(channel_folder, "channel.json"), "r") as channel_file:
channel_data = json.load(channel_file)
guild_id = channel_data.get("guild", {}).get("id", None)
# Load the messages.json file
with open(os.path.join(channel_folder, "messages.json"), "r") as messages_file:
messages_data = json.load(messages_file)
# Iterate through each message and send a delete request
for message in messages_data:
message_id = message["ID"]
try:
channel = bot.get_channel(int(channel_id))
if guild_id:
guild = bot.get_guild(int(guild_id))
if channel and guild:
msg = await channel.fetch_message(int(message_id))
await msg.delete()
status = "Success"
else:
status = f"Failure: Unknown channel or guild in {channel_name}"
else:
if channel:
msg = await channel.fetch_message(int(message_id))
await msg.delete()
status = "Success"
else:
status = f"Failure: Unknown DM channel in {channel_name}"
except Exception as e:
status = f"Failure: {str(e)}"
# Log the result
log_result(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), channel_name, channel_id, guild_id, message_id, status)
if status == "Success":
print(f"Deleted message {message_id} in channel {channel_name}")
else:
print(f"Failed to delete message {message_id} in channel {channel_name}: {status}")
print("Purge complete.")
await bot.close()
bot.run(TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment