Advertisement
AssortedBrunoz

DiscordHook

Nov 26th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.12 KB | None | 0 0
  1. --[[
  2.   DiscordHook by HydroNitrogen
  3.   Licenced under MIT
  4.   Copyright (c) 2019 Wendelstein7 (a.k.a. HydroNitrogen)
  5.   See: https://github.com/Wendelstein7/DiscordHook-CC
  6. ]]--
  7.  
  8. local DiscordHook = {
  9.   ["name"] = "DiscordHook",
  10.   ["author"] = "HydroNitrogen",
  11.   ["date"] = "2019-01-30",
  12.   ["version"] = 1,
  13.   ["url"] = "https://github.com/Wendelstein7/DiscordHook-CC"
  14. }
  15.  
  16. local function expect(func, arg, n, expected)
  17.   if type(arg) ~= expected then
  18.     return error(("%s: bad argument #%d (expected %s, got %s)"):format(func, n, expected, type(arg)), 2)
  19.   end
  20. end
  21.  
  22. function http.patch(url, body, headers)
  23.   headers = headers or {}
  24.   headers["Content-Type"] = headers["Content-Type"] or "application/json"
  25.  
  26.   -- Make the PATCH request
  27.   local response, err = http.request({
  28.       url = url,
  29.       method = "PATCH",
  30.       headers = headers,
  31.       body = body,
  32.       timeout = 2
  33.   })
  34.  
  35.   if response then
  36.       return true, err
  37.   else
  38.       return false, err
  39.   end
  40. end
  41.  
  42. local function getMessage(url)
  43.   -- Construct the URL to fetch the message
  44.   local apiUrl = url
  45.  
  46.   -- Prepare the headers (authentication may be required in some cases)
  47.   local headers = {
  48.     ["Content-Type"] = "application/json",
  49.     ["Authorization"] = "Bot " .. "YOUR_BOT_TOKEN" -- Optional, if needed
  50.   }
  51.  
  52.   -- Send the GET request to fetch the message
  53.   local response, err = http.get(apiUrl, headers)
  54.  
  55.   -- Check if the request was successful
  56.   if response then
  57.     local data = response.readAll() -- Read the entire response body
  58.     response.close()  -- Always close the response after reading
  59.  
  60.     -- Parse the response JSON (this is the message data)
  61.     local messageData = textutils.unserializeJSON(data)
  62.  
  63.     -- Return the message data
  64.     return messageData
  65.   else
  66.     -- If the request failed, print the error
  67.     print("Error fetching message: " .. (err or "Unknown error"))
  68.     return nil
  69.   end
  70. end
  71.  
  72.  
  73. local function send(url, data, headers)
  74.   local request, message = http.post(url, data, headers)
  75.   print(message)
  76.   if not request then
  77.     return false
  78.   end
  79.   return true
  80. end
  81.  
  82.  
  83. function DiscordHook.createWebhook(url)
  84.   expect("createWebhook", url, 1, "string")
  85.   local success, message = http.checkURL(url)
  86.   if not success then
  87.     return false, "createWebhook: Can't access invalid url - " .. message
  88.   else
  89.     local _ = {}
  90.     _.url = url
  91.     _.sentMessages = 0
  92.  
  93.     function _.send(message, username, avatar)
  94.       expect("send", message, 1, "string")
  95.       local data = "content=" .. textutils.urlEncode(message)
  96.       if username then
  97.         expect("send", username, 2, "string")
  98.         data = data .. "&username=" .. textutils.urlEncode(username)
  99.       end
  100.       if avatar then
  101.         expect("send", avatar, 3, "string")
  102.         data = data .. "&avatar_url=" .. textutils.urlEncode(avatar)
  103.       end
  104.  
  105.      
  106.       local success = send(_.url, data, { ["Content-Type"] = "application/x-www-form-urlencoded", ["Source"] = "Minecraft/ComputerCraft/DiscordHook" })
  107.       if success then _.sentMessages = _.sentMessages + 1 end
  108.       return success
  109.     end
  110.     function _.edit(messageId, message, username, avatar)
  111.       expect("edit", messageId, 1, "string")
  112.       expect("edit", message, 2, "string")
  113.  
  114.       -- Construct the URL for editing the specific message
  115.       local url = _.url.."/messages/"..messageId
  116.  
  117.       -- Build the JSON payload
  118.       local data = { content = message }
  119.       if username then
  120.           expect("edit", username, 3, "string")
  121.           data.username = username
  122.       end
  123.       if avatar then
  124.           expect("edit", avatar, 4, "string")
  125.           data.avatar_url = avatar
  126.       end
  127.  
  128.       -- Convert the payload to JSON
  129.       local jsonPayload = textutils.serializeJSON(data)
  130.  
  131.       -- Send the PATCH request
  132.       local success = http.patch(url, jsonPayload, {
  133.           ["Content-Type"] = "application/json",
  134.           ["Source"] = "Minecraft/ComputerCraft/DiscordHook"
  135.       })
  136.  
  137.       if success then _.sentMessages = _.sentMessages + 1 end
  138.       return success
  139.   end
  140.  
  141.     function _.get(messageId)
  142.       expect("get", messageId, 1, "string")
  143.  
  144.       local url = _.url.."/messages/"..messageId
  145.  
  146.       return getMessage(url).content
  147.     end
  148.  
  149.     function _.sendJSON(json)
  150.       expect("sendJSON", json, 1, "string")
  151.  
  152.       local success = send(_.url, json, { ["Content-Type"] = "application/json", ["Source"] = "Minecraft/ComputerCraft/DiscordHook" })
  153.       if success then _.sentMessages = _.sentMessages + 1 end
  154.       return success
  155.     end
  156.  
  157.     function _.sendEmbed(message, title, description, link, colour, image_large, image_thumb, username, avatar)
  158.       expect("sendEmbed", message, 1, "string")
  159.       local data = { ["content"] = message, ["embeds"] = { {} } }
  160.  
  161.       if title then
  162.         expect("sendEmbed", title, 2, "string")
  163.         data["embeds"][1]["title"] = title
  164.       end
  165.       if description then
  166.         expect("sendEmbed", description, 3, "string")
  167.         data["embeds"][1]["description"] = description
  168.       end
  169.       if link then
  170.         expect("sendEmbed", link, 4, "string")
  171.         data["embeds"][1]["url"] = link
  172.       end
  173.       if colour then
  174.         expect("sendEmbed", colour, 5, "number")
  175.         data["embeds"][1]["color"] = colour
  176.       end
  177.       if image_large then
  178.         expect("sendEmbed", image_large, 6, "string")
  179.         data["embeds"][1]["image"] = { ["url"] = image_large }
  180.       end
  181.       if image_thumb then
  182.         expect("sendEmbed", image_thumb, 7, "string")
  183.         data["embeds"][1]["thumbnail"] = { ["url"] = image_thumb }
  184.       end
  185.       if username then
  186.         expect("sendEmbed", username, 8, "string")
  187.         data["username"] = username
  188.       end
  189.       if avatar then
  190.         expect("sendEmbed", avatar, 9, "string")
  191.         data["avatar_url"] = avatar
  192.       end
  193.      
  194.       local success = send(_.url, textutils.serializeJSON(data), { ["Content-Type"] = "application/json", ["Source"] = "Minecraft/ComputerCraft/DiscordHook" })
  195.       if success then _.sentMessages = _.sentMessages + 1 end
  196.       return success
  197.     end
  198.  
  199.     return true, _
  200.   end
  201. end
  202.  
  203. return DiscordHook
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement