Advertisement
HappySunChild

WebhookModule

Oct 10th, 2022 (edited)
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | Source Code | 0 0
  1. local webhook = {}
  2.  
  3. webhook._contentType = { ["Content-Type"] = "application/json" }
  4.  
  5. local function send(url, data)
  6.     local request, message = http.post(url, textutils.serializeJSON(data), webhook._contentType)
  7.  
  8.     if not request then
  9.         return false, message
  10.     end
  11.  
  12.     return true
  13. end
  14.  
  15. function webhook:createWebhook(url, username, avatar_url)
  16.     local hook = {}
  17.  
  18.     hook.url = url
  19.     hook.username = username
  20.     hook.avatar = avatar_url
  21.  
  22.     hook.sent = 0
  23.     hook.createdEmbeds = {}
  24.  
  25.     function hook:sendMessage(message)
  26.         return send(hook.url, {
  27.             content = message,
  28.             username = hook.username,
  29.             avatar_url = hook.avatar
  30.         })
  31.     end
  32.  
  33.     function hook:sendEmbed(embed)
  34.         if embed then
  35.             send(hook.url, embed.data)
  36.         end
  37.     end
  38.  
  39.     function hook:createEmbed(title, description)
  40.         local embed = {}
  41.  
  42.         embed.data = {
  43.             title = title,
  44.             description = description,
  45.             color = 0xFFFFFF,
  46.             fields = {}
  47.         }
  48.  
  49.         function embed:setAuthor(author)
  50.             embed.data.author = { name = author }
  51.         end
  52.  
  53.         function embed:setFooter(text)
  54.             embed.data.footer = { text = text }
  55.         end
  56.  
  57.         function embed:addField(name, value, inline)
  58.             local field = { name = name, value = value, inline = inline ~= nil and inline or true }
  59.  
  60.             table.insert(embed.data.fields, field)
  61.         end
  62.  
  63.         function embed:removeField(index)
  64.             if embed.data.fields[index] then
  65.                 table.remove(embed.data.fields, index)
  66.             end
  67.         end
  68.  
  69.         function embed:addImage(url)
  70.             embed.data.image = { url = url }
  71.         end
  72.  
  73.         table.insert(hook.createdEmbeds, embed)
  74.  
  75.         return embed
  76.     end
  77.  
  78.     return hook
  79. end
  80.  
  81. function webhook:getUrlFromFile(path)
  82.     if fs.exists(path) then
  83.         local file = fs.open(path, "r")
  84.         local url = file.readLine()
  85.         file.close()
  86.  
  87.         if url and http.checkURL(url) then
  88.             return url
  89.         end
  90.     end
  91.  
  92.     return false
  93. end
  94.  
  95. function webhook:saveUrlToFile(url, path)
  96.     if url then
  97.         if not fs.exists(path) then
  98.             local file = fs.open(path, "w")
  99.             file.write(url)
  100.             file.close()
  101.  
  102.             return true
  103.         end
  104.     end
  105.  
  106.     return false
  107. end
  108.  
  109. function webhook:update()
  110.     if fs.exists("webhook") then
  111.         fs.delete("webhook")
  112.     end
  113.  
  114.     shell.run("pastebin get ybU4xHv6 webhook")
  115. end
  116.  
  117. return webhook
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement