Advertisement
_DudeWhat_

Discord API

Jan 7th, 2022 (edited)
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.19 KB | None | 0 0
  1. local discord = {}
  2.  
  3. local function createFormBody(data)
  4.     first = true
  5.     body = ""
  6.     for key, value in pairs(data) do
  7.         if first then
  8.             first = false
  9.         else
  10.             body = body.."&"
  11.         end
  12.         body = body..textutils.urlEncode(tostring(key)).."="..textutils.urlEncode(tostring(value))
  13.     end
  14.     return body
  15. end
  16.  
  17. local function parseResponse(response)
  18.     local content = response.readAll()
  19.     local result, reason = textutils.unserializeJSON(content)
  20.     if not result then print("Deserialization failed:", reason) return nil end
  21.     return result
  22. end
  23.  
  24. function discord.webhookMessage(webhookId, webhookToken, content, username, avatar_url, tts)
  25.     local url = "https://discord.com/api/webhooks/"..webhookId.."/"..webhookToken.."?wait=true"
  26.     local headers = { ["Content-Type"] = "application/x-www-form-urlencoded" }
  27.     local body = { content = content }
  28.     if username then body.username = username end
  29.     if avatar_url then body.avatar_url = avatar_url end
  30.     if tts then body.tts = tts end
  31.     local response, reason, err = http.post({ url = url, headers = headers, body = createFormBody(body) })
  32.     if not response then print("Request failed:", reason) return nil end
  33.     return parseResponse(response)
  34. end
  35.  
  36. return discord
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement