Skip to content

Instantly share code, notes, and snippets.

@py7hon
Created April 18, 2023 11:18
Show Gist options
  • Select an option

  • Save py7hon/7fc0ee5d9a6a1e1068efa6d575ed9ba1 to your computer and use it in GitHub Desktop.

Select an option

Save py7hon/7fc0ee5d9a6a1e1068efa6d575ed9ba1 to your computer and use it in GitHub Desktop.

Revisions

  1. py7hon created this gist Apr 18, 2023.
    4 changes: 4 additions & 0 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    #how to use
    1. put any file or voice with codec `audio/ogg`
    2. fill `token` and `channel_id` on `config.json`
    3. run `py main.py`
    4 changes: 4 additions & 0 deletions config.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    {
    "token":"",
    "channel_id":""
    }
    55 changes: 55 additions & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    import os, json
    import requests
    import base64


    voice_data = open(os.path.join(os.getcwd(), "voice-message.ogg"), "rb").read()

    def data():
    file = json.load(open("config.json"))
    data = {
    "token":file["token"],
    "channel_id":file["channel_id"]
    }
    return data

    json_data = data()

    attach_url = f'https://discord.com/api/v10/channels/{json_data["channel_id"]}/attachments'
    attach_headers = {
    'Content-Type': 'application/json',
    'Authorization': json_data["token"]
    }
    attach_data = {
    'files': [{
    'file_size': len(voice_data),
    'filename': 'voice-message.ogg',
    'id': '2'
    }]
    }
    attach_response = requests.post(attach_url, headers=attach_headers, data=json.dumps(attach_data))
    attach_json = attach_response.json()
    upload_url = attach_json['attachments'][0]['upload_url']
    upload_filename = attach_json['attachments'][0]['upload_filename']

    upload_headers = {'Content-Type': 'audio/ogg'}
    print(requests.put(upload_url, headers=upload_headers, data=voice_data).text)

    message_url = f'https://discord.com/api/v10/channels/{json_data["channel_id"]}/messages'
    message_headers = {
    'Content-Type': 'application/json',
    'Authorization': json_data["token"],
    'x-super-properties': 'eyJvcyI6IldpbmRvd3MiLCJjbGllbnRfYnVpbGRfbnVtYmVyIjo5OTk5OTk5fQ=='
    }
    message_data = {
    'flags': 8192,
    'attachments': [{
    'id': '0',
    'filename': 'voice-message.ogg',
    'uploaded_filename': upload_filename,
    'duration_secs': 0.000001,
    'waveform': base64.b64encode(voice_data[:100]).decode("utf-8")
    }]
    }

    print(requests.post(message_url, headers=message_headers, data=json.dumps(message_data)).status_code)