guides:apps:discord

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
guides:apps:discord [2024-06-06 17:48] – v2 port examples geekguides:apps:discord [2024-06-06 18:18] (current) – add date geek
Line 21: Line 21:
 Send files/images: Send files/images:
 <code AutoHotkey> <code AutoHotkey>
-#Requires AutoHotkey v2+#Requires AutoHotkey v2.0
  
 data := CreateFormData(Map( data := CreateFormData(Map(
     "content", "A message with text and two images",     "content", "A message with text and two images",
-    "files[1]", "@image1.jpg", +    "files[1]", "@C:\Users\User\Desktop\nerd.png", 
-    "files[2]", "@image2.png",+    "files[2]", "@C:\Users\User\Desktop\drilling.png",
 )) ))
 http := ComObject("WinHTTP.WinHTTPRequest.5.1") http := ComObject("WinHTTP.WinHTTPRequest.5.1")
Line 40: Line 40:
  * Thanks to Coco: https://autohotkey.com/boards/viewtopic.php?p=41731#p41731    * Thanks to Coco: https://autohotkey.com/boards/viewtopic.php?p=41731#p41731  
  * Modified version by SKAN, 09/May/2016    * Modified version by SKAN, 09/May/2016  
- * Ported to AHKv2 and modified by G33kDude+ * Ported to AHKv2 and modified by G33kDude 06/June/2024
  *  *
  * @param {Map} formData The fields to encode in the form body  * @param {Map} formData The fields to encode in the form body
Line 58: Line 58:
  */  */
 CreateFormData(formData) { CreateFormData(formData) {
-    boundary := Format( +    boundary := Random(2**602**63-1) Random(2**602**63-1), buf := Buffer()
-        "{:016x}{:016x}", +
-        Random(00x7FFFFFFFFFFFFFFF)+
-        Random(00x7FFFFFFFFFFFFFFF) +
-    ) +
-    buf := Buffer() +
-    AppendStr(str) { +
-        oldSize := buf.Size +
-        buf.Size += strSize := StrPut(str, "UTF-8"- 1 +
-        StrPut(str, buf.Ptr + oldSize, strSize, "UTF-8"+
-    } +
-    AppendFile(file) { +
-        file.Pos := +
-        oldSize := buf.Size +
-        buf.Size += file.Length +
-        file.RawRead(buf.ptr + oldSize, file.Length) +
-    } +
     for k, v in formData {     for k, v in formData {
         if v ~= "^@" { ; A file, following the cURL convention         if v ~= "^@" { ; A file, following the cURL convention
-            filePath := LTrim(v, "@") +            filePath := LTrim(v, "@")SplitPath(filePath, &fileName)
-            SplitPath filePath, &fileName+
             file := FileOpen(filePath, "r")             file := FileOpen(filePath, "r")
-            AppendStr( +            AddStr "--" boundary '`r`nContent-Disposition: form-data; name="' k 
-                "--" boundary "`r`n" +                . '"; filename="' fileName '"`r`nContent-Type: MimeType(file) 
-                'Content-Disposition: form-data; name="' k '"; filename="' fileName '"`r`n' +                . "`r`n`r`n" 
-                "Content-Type: MimeType(file) "`r`n`r`n" +            AddFile(file), AddStr("`r`n") 
-            +        } else { ; Regular text 
-            AppendFile(file) +            AddStr "--" boundary '`r`nContent-Disposition: form-data; name="'
-            AppendStr("`r`n"+                . '"`r`n`r`n' v "`r`n"
-            continue+
         }         }
-         
-        AppendStr( 
-            "--" boundary "`r`n" 
-            'Content-Disposition: form-data; name="' k '"`r`n`r`n' 
-            v "`r`n" 
-        ) 
     }     }
-    AppendStr("--" boundary "--")+    AddStr "--" boundary "--"
          
     return {     return {
Line 104: Line 79:
     }     }
          
-    MimeType(file) { +    AddStr(str) { 
-        file.Pos := 0 +        oldSize := buf.Size, buf.Size +strSize := StrPut(str, "UTF-8"- 1 
-        n := file.ReadUInt() +        StrPut(str, buf.Ptr + oldSize, strSize, "UTF-8")
-        return ( +
-            (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" +
-        )+
     }     }
-    +    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) {     SafeArrayFromBuffer(buf) {
-        arr := ComObjArray(0x11, buf.Size) ; Create SAFEARRAY = VT_ARRAY|VT_UI1+        arr := ComObjArray(0x11, buf.Size) ; SAFEARRAY = VT_ARRAY|VT_UI1
         DllCall("RtlMoveMemory",         DllCall("RtlMoveMemory",
             "Ptr", NumGet(ComObjValue(arr), 8 + A_PtrSize, "Ptr"),             "Ptr", NumGet(ComObjValue(arr), 8 + A_PtrSize, "Ptr"),