Last active
November 16, 2024 06:04
-
-
Save QuartzWarrior/091d3229464366acb08202294f0b3cf5 to your computer and use it in GitHub Desktop.
Upload any file under your discord upload limit to a channel easily
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # run pip install -U curl_cffi | |
| import asyncio | |
| import os | |
| from datetime import datetime, timezone | |
| from curl_cffi.requests import AsyncSession | |
| if os.name == "nt": | |
| asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) | |
| session = AsyncSession(impersonate="chrome", timeout=180) | |
| async def upload_file(token: str, guild_id: str, channel_id: str, file_path: str): | |
| async with session as sesh: | |
| await sesh.get("https://discord.com") | |
| with open(file_path, "rb") as f: | |
| file = f.read() | |
| size_in_bytes = len(file) | |
| id = await sesh.post(f"https://discord.com/api/v9/channels/{channel_id}/attachments", headers={ | |
| "accept": "*/*", | |
| "accept-language": "en-US", | |
| "authorization": token, | |
| "content-type": "application/json", | |
| "origin": "https://discord.com", | |
| "priority": "u=1, i", | |
| "referer": f"https://discord.com/channels/{guild_id}/{channel_id}", | |
| "sec-ch-ua": "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"", | |
| "sec-ch-ua-mobile": "?0", | |
| "sec-ch-ua-platform": "Windows", | |
| "sec-fetch-dest": "empty", | |
| "sec-fetch-mode": "cors", | |
| "sec-fetch-site": "same-origin", | |
| "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) discord/1.0.9169 Chrome/128.0.6613.36 Electron/32.0.0 Safari/537.36", | |
| "x-debug-options": "bugReporterEnabled", | |
| "x-discord-locale": "en-US", | |
| "x-discord-timezone": "America/Denver", | |
| "x-super-properties": "eyJvcyI6IldpbmRvd3MiLCJicm93c2VyIjoiRGlzY29yZCBDbGllbnQiLCJyZWxlYXNlX2NoYW5uZWwiOiJzdGFibGUiLCJjbGllbnRfdmVyc2lvbiI6IjEuMC45MTY5Iiwib3NfdmVyc2lvbiI6IjEwLjAuMjI2MzEiLCJvc19hcmNoIjoieDY0IiwiYXBwX2FyY2giOiJ4NjQiLCJzeXN0ZW1fbG9jYWxlIjoiZW4tVVMiLCJicm93c2VyX3VzZXJfYWdlbnQiOiJNb3ppbGxhLzUuMCAoV2luZG93cyBOVCAxMC4wOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBkaXNjb3JkLzEuMC45MTY5IENocm9tZS8xMjguMC42NjEzLjM2IEVsZWN0cm9uLzMyLjAuMCBTYWZhcmkvNTM3LjM2IiwiYnJvd3Nlcl92ZXJzaW9uIjoiMzIuMC4wIiwib3Nfc2RrX3ZlcnNpb24iOiIyMjYzMSIsImNsaWVudF9idWlsZF9udW1iZXIiOjM0Mjk2OCwibmF0aXZlX2J1aWxkX251bWJlciI6NTQ4NzYsImNsaWVudF9ldmVudF9zb3VyY2UiOm51bGx9" | |
| }, json={ | |
| "files": [{ | |
| "filename": file_path.split("/")[-1].split("\\")[-1], | |
| "file_size": size_in_bytes, | |
| "id": "2377", | |
| "is_clip": False | |
| }] | |
| }) | |
| if id.status_code != 200: | |
| print("Failed to get upload url") | |
| return | |
| # print(id.json()) | |
| upload_url = id.json()["attachments"][0]["upload_url"] | |
| await sesh.options(upload_url, headers={ | |
| "accept": "*/*", | |
| "accept-language": "en-US", | |
| "access-control-request-headers": "content-type", | |
| "access-control-request-method": "PUT", | |
| "origin": "https://discord.com", | |
| "priority": "u=1, i", | |
| "referer": "https://discord.com/", | |
| "sec-fetch-dest": "empty", | |
| "sec-fetch-mode": "cors", | |
| "sec-fetch-site": "cross-site", | |
| "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) discord/1.0.9169 Chrome/128.0.6613.36 Electron/32.0.0 Safari/537.36" | |
| }) | |
| await sesh.put(upload_url, headers={ | |
| "accept": "*/*", | |
| "accept-language": "en-US", | |
| "content-length": size_in_bytes, | |
| "content-type": "application/octet-stream", | |
| "origin": "https://discord.com", | |
| "priority": "u=1, i", | |
| "referer": "https://discord.com/", | |
| "sec-ch-ua": "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"", | |
| "sec-ch-ua-mobile": "?0", | |
| "sec-ch-ua-platform": "Windows", | |
| "sec-fetch-dest": "empty", | |
| "sec-fetch-mode": "cors", | |
| "sec-fetch-site": "cross-site", | |
| "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) discord/1.0.9169 Chrome/128.0.6613.36 Electron/32.0.0 Safari/537.36" | |
| }, data=file) | |
| await sesh.post(f"https://discord.com/api/v9/channels/{channel_id}/messages", headers={ | |
| "accept": "*/*", | |
| "accept-language": "en-US", | |
| "authorization": token, | |
| "content-type": "application/json", | |
| "origin": "https://discord.com", | |
| "priority": "u=1, i", | |
| "referer": f"https://discord.com/channels/{guild_id}/{channel_id}", | |
| "sec-ch-ua": "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"", | |
| "sec-ch-ua-mobile": "?0", | |
| "sec-ch-ua-platform": "Windows", | |
| "sec-fetch-dest": "empty", | |
| "sec-fetch-mode": "cors", | |
| "sec-fetch-site": "same-origin", | |
| "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) discord/1.0.9169 Chrome/128.0.6613.36 Electron/32.0.0 Safari/537.36", | |
| "x-debug-options": "bugReporterEnabled", | |
| "x-discord-locale": "en-US", | |
| "x-discord-timezone": "America/Denver", | |
| "x-super-properties": "eyJvcyI6IldpbmRvd3MiLCJicm93c2VyIjoiRGlzY29yZCBDbGllbnQiLCJyZWxlYXNlX2NoYW5uZWwiOiJzdGFibGUiLCJjbGllbnRfdmVyc2lvbiI6IjEuMC45MTY5Iiwib3NfdmVyc2lvbiI6IjEwLjAuMjI2MzEiLCJvc19hcmNoIjoieDY0IiwiYXBwX2FyY2giOiJ4NjQiLCJzeXN0ZW1fbG9jYWxlIjoiZW4tVVMiLCJicm93c2VyX3VzZXJfYWdlbnQiOiJNb3ppbGxhLzUuMCAoV2luZG93cyBOVCAxMC4wOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBkaXNjb3JkLzEuMC45MTY5IENocm9tZS8xMjguMC42NjEzLjM2IEVsZWN0cm9uLzMyLjAuMCBTYWZhcmkvNTM3LjM2IiwiYnJvd3Nlcl92ZXJzaW9uIjoiMzIuMC4wIiwib3Nfc2RrX3ZlcnNpb24iOiIyMjYzMSIsImNsaWVudF9idWlsZF9udW1iZXIiOjM0Mjk2OCwibmF0aXZlX2J1aWxkX251bWJlciI6NTQ4NzYsImNsaWVudF9ldmVudF9zb3VyY2UiOm51bGx9" | |
| }, json={ | |
| "content": "", | |
| "nonce": _generate_nonce(), | |
| "channel_id": channel_id, | |
| "type": 0, | |
| "sticker_ids": [], | |
| "attachments": [{ | |
| "id": "0", | |
| "filename": file_path.split("/")[-1].split("\\")[-1], | |
| "uploaded_filename": id.json()["attachments"][0]["upload_filename"] | |
| }] | |
| }) | |
| print("Uploaded file successfully") | |
| async def main(): | |
| token = input("Enter your token: ").strip() | |
| guild_id = input("Enter the guild id: ").strip() | |
| channel_id = input("Enter the channel id: ").strip() | |
| file_path = input("Enter the file path: ").strip() | |
| await upload_file(token, guild_id, channel_id, file_path) | |
| def _generate_nonce() -> str: # stolen from discord.py-self | |
| return str(int(datetime.now(timezone.utc).timestamp() * 1000 - 1420070400000) << 22) | |
| asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment