guides:apps:discord

Discord

Send a message:

#Requires AutoHotkey v2
webhookURL := "https://discord.com/api/webhooks/..."
http := ComObject("WinHttp.WinHttpRequest.5.1")
http.Open("POST", webhookURL)
http.SetRequestHeader("Content-Type", "application/json")
http.Send('{"content": "test"}')

Send files/images:

#Requires AutoHotkey v2.0
 
data := CreateFormData(Map(
    "content", "A message with text and two images",
    "files[1]", "@C:\Users\User\Desktop\nerd.png",
    "files[2]", "@C:\Users\User\Desktop\drilling.png",
))
http := ComObject("WinHTTP.WinHTTPRequest.5.1")
http.Open("POST", "https://discord.com/api/webhooks/.../...")
http.SetRequestHeader("Content-Type", data.contentType)
http.Send(data.body)
 
/**
 * Creates a COM WinHttpRequest compatible `multipart/form-data` body,
 * supporting file attachments like cURL.
 *
 * CreateFormData() by tmplinshi, AHK Topic: https://autohotkey.com/boards/viewtopic.php?t=7647  
 * Thanks to Coco: https://autohotkey.com/boards/viewtopic.php?p=41731#p41731  
 * Modified version by SKAN, 09/May/2016  
 * Ported to AHKv2 and modified by G33kDude 06/June/2024
 *
 * @param {Map} formData The fields to encode in the form body
 *
 * @return {Object} An object with properties `body` and `contentType`
 *
 * @example
 * data := CreateFormData(Map(
 *     "content", "A message with text and two images",
 *     "files[1]", "@image1.jpg",
 *     "files[2]", "@image2.png",
 * ))
 * http := ComObject("WinHTTP.WinHTTPRequest.5.1")
 * http.Open("POST", "https://discord.com/api/webhooks/.../...")
 * http.SetRequestHeader("Content-Type", data.contentType)
 * http.Send(data.body)
 */
CreateFormData(formData) {
    boundary := Random(2**60, 2**63-1) Random(2**60, 2**63-1), buf := Buffer()
    for k, v in formData {
        if v ~= "^@" { ; A file, following the cURL convention
            filePath := LTrim(v, "@"), SplitPath(filePath, &fileName)
            file := FileOpen(filePath, "r")
            AddStr "--" boundary '`r`nContent-Disposition: form-data; name="' k
                . '"; filename="' fileName '"`r`nContent-Type: ' MimeType(file)
                . "`r`n`r`n"
            AddFile(file), AddStr("`r`n")
        } else { ; Regular text
            AddStr "--" boundary '`r`nContent-Disposition: form-data; name="' k
                . '"`r`n`r`n' v "`r`n"
        }
    }
    AddStr "--" boundary "--"
 
    return {
        body: SafeArrayFromBuffer(buf),
        contentType: "multipart/form-data; boundary=" boundary,
    }
 
    AddStr(str) {
        oldSize := buf.Size, buf.Size += strSize := StrPut(str, "UTF-8") - 1
        StrPut(str, buf.Ptr + oldSize, strSize, "UTF-8")
    }
    AddFile(file) {
        oldSize := buf.Size, buf.Size += file.Length
        file.Pos := 0, file.RawRead(buf.ptr + oldSize, file.Length)
    }
    MimeType(file) => (
        file.Pos := 0, n := file.ReadUInt(),
        (n          = 0x474E5089) ? "image/png" :
        (n          = 0x38464947) ? "image/gif" :
        (n & 0xFFFF = 0x4D42    ) ? "image/bmp" :
        (n & 0xFFFF = 0xD8FF    ) ? "image/jpeg" :
        (n & 0xFFFF = 0x4949    ) ? "image/tiff" :
        (n & 0xFFFF = 0x4D4D    ) ? "image/tiff" :
        "application/octet-stream"
    )
    SafeArrayFromBuffer(buf) {
        arr := ComObjArray(0x11, buf.Size) ; SAFEARRAY = VT_ARRAY|VT_UI1
        DllCall("RtlMoveMemory",
            "Ptr", NumGet(ComObjValue(arr), 8 + A_PtrSize, "Ptr"),
            "Ptr", buf, "Ptr", buf.Size)
        return arr
    }
}

Send a message:

#Requires AutoHotkey v1
webhookURL := "https://discord.com/api/webhooks/..."
http := ComObject("WinHttp.WinHttpRequest.5.1")
http.Open("POST", webhookURL)
http.SetRequestHeader("Content-Type", "application/json")
http.Send("{""content"": ""test""}")

Send files/images: https://www.autohotkey.com/boards/viewtopic.php?t=86273