Skip to content

Instantly share code, notes, and snippets.

@max-4-3
Created March 31, 2025 21:07
Show Gist options
  • Save max-4-3/6704422045b1c21a4a3e9226fca38f68 to your computer and use it in GitHub Desktop.
Save max-4-3/6704422045b1c21a4a3e9226fca38f68 to your computer and use it in GitHub Desktop.
Upload any ( i think ) audio file to discord as a voice-message
from fake_useragent import UserAgent # pip install fake-useragent or fake_useragent
from consts import TOKEN # Make a consts.py file in same dir and put your token there ( TOKEN = "YOUR TOKEN" )
import requests # pip install requests
import os, json, base64, subprocess # Comes pre installed
# User inputs
channel_id = int(input("Enter a Channel ID to send the message: "))
voice_uri = input("Enter the Audio File path: ").strip()
# Validate file path
if not os.path.exists(voice_uri):
raise FileNotFoundError(f"File Not Found: {voice_uri}")
# Convert MP3 to OGG (Opus) using ffmpeg
ogg_filename = "voice-message.ogg"
try:
subprocess.run([
"ffmpeg",
'-hide_banner',
"-y",
"-i", voice_uri,
"-c:a", "libopus",
"-b:a", "64k",
"-ar", "48000",
ogg_filename
], check=True, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
# Extract metadata using ffprobe
def get_audio_metadata(file_path):
""" Extracts audio duration and waveform data using ffprobe. """
# Get duration
cmd = [
"ffprobe", "-v", "error", '-hide_banner',
"-select_streams", "a:0",
"-show_entries", "format=duration",
"-of", "json",
file_path
]
result = subprocess.run(cmd, capture_output=True, text=True)
duration = float(json.loads(result.stdout)["format"]["duration"])
# Generate a simple waveform (instead of real analysis)
waveform = base64.b64encode(os.urandom(256)).decode('utf-8')
return round(duration), waveform
# Get required metadata
duration, waveform = get_audio_metadata(ogg_filename)
file_size = os.path.getsize(ogg_filename)
# Step 1: Request an upload URL
url = f"https://discord.com/api/v10/channels/{channel_id}/attachments"
headers = {
"Authorization": TOKEN,
"Content-Type": "application/json",
"User-Agent": UserAgent().random
}
payload = {
"files": [{
"filename": "voice-message.ogg",
"file_size": file_size,
"id": 0
}]
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code != 200:
print("Failed to get upload URL:", response.text)
exit()
upload_data = response.json()["attachments"][0]
upload_url = upload_data["upload_url"]
uploaded_filename = upload_data["upload_filename"]
# Step 2: Upload the audio file
with open(ogg_filename, "rb") as file:
upload_response = requests.put(upload_url, headers={"Content-Type": "audio/ogg"}, data=file)
if upload_response.status_code != 200:
print("Failed to upload file:", upload_response.text)
exit()
# Step 3: Send the voice message
message_url = f"https://discord.com/api/v10/channels/{channel_id}/messages"
message_payload = {
"flags": 8192, # IS_VOICE_MESSAGE
"attachments": [{
"id": "0",
"filename": "voice-message.ogg",
"uploaded_filename": uploaded_filename,
"duration_secs": duration,
"waveform": waveform
}]
}
message_response = requests.post(message_url, headers=headers, json=message_payload)
print(message_response.status_code, f"Error Response: \n{json.dumps(message_response.text, indent=4, ensure_ascii=False)}" if message_response.status_code != 200 else "Upload Succesfull!")
finally:
if os.path.exists(ogg_filename): os.remove(ogg_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment