Jump to content

Export Function


Recommended Posts

Eu tenho uma dúvida , eu peguei um script na internet de WebHook Discord para enviar mensagens para o Discord. Eu queria saber como posso usar as function do script de Webhook em um script server-side do MTA.

Obs = Eu pesquisei e parece que a váriavel require está desativa no mta. Pois se não tivesse desativada  eu acho que era somente usar "local DiscordHook = require "DiscordHook"" , que exportava as functions 

Script Discord Webhook.

--[[
  DiscordHook by HydroNitrogen
  Licenced under MIT
  Copyright (c) 2019 Wendelstein7 (a.k.a. HydroNitrogen)
  See: https://github.com/Wendelstein7/DiscordHook-CC
]]--

local DiscordHook = {
  ["name"] = "DiscordHook",
  ["author"] = "HydroNitrogen",
  ["date"] = "2019-01-30",
  ["version"] = 1,
  ["url"] = "https://github.com/Wendelstein7/DiscordHook-CC"
}

local function expect(func, arg, n, expected)
  if type(arg) ~= expected then
    return error(("%s: bad argument #%d (expected %s, got %s)"):format(func, n, expected, type(arg)), 2)
  end
end

local function send(url, data, headers)
  local request, message = http.post(url, data, headers)
  if not request then
    return false
  end
  return true
end

function DiscordHook.createWebhook(url)
  expect("createWebhook", url, 1, "string")
  local success, message = http.checkURL(url)
  if not success then
    return false, "createWebhook: Can't access invalid url - " .. message
  else
    local _ = {}
    _.url = url
    _.sentMessages = 0

    function _.send(message, username, avatar)
      expect("send", message, 1, "string")
      local data = "content=" .. textutils.urlEncode(message)
      if username then
        expect("send", username, 2, "string")
        data = data .. "&username=" .. textutils.urlEncode(username)
      end
      if avatar then
        expect("send", avatar, 3, "string")
        data = data .. "&avatar_url=" .. textutils.urlEncode(avatar)
      end

      
      local success = send(_.url, data, { ["Content-Type"] = "application/x-www-form-urlencoded", ["Source"] = "Minecraft/ComputerCraft/DiscordHook" })
      if success then _.sentMessages = _.sentMessages + 1 end
      return success
    end

    function _.sendJSON(json)
      expect("sendJSON", json, 1, "string")

      local success = send(_.url, json, { ["Content-Type"] = "application/json", ["Source"] = "Minecraft/ComputerCraft/DiscordHook" })
      if success then _.sentMessages = _.sentMessages + 1 end
      return success
    end

    function _.sendEmbed(message, title, description, link, colour, image_large, image_thumb, username, avatar)
      expect("sendEmbed", message, 1, "string")
      local data = { ["content"] = message, ["embeds"] = { {} } }

      if title then
        expect("sendEmbed", title, 2, "string")
        data["embeds"][1]["title"] = title
      end
      if description then
        expect("sendEmbed", description, 3, "string")
        data["embeds"][1]["description"] = description
      end
      if link then
        expect("sendEmbed", link, 4, "string")
        data["embeds"][1]["url"] = link
      end
      if colour then
        expect("sendEmbed", colour, 5, "number")
        data["embeds"][1]["color"] = colour
      end
      if image_large then
        expect("sendEmbed", image_large, 6, "string")
        data["embeds"][1]["image"] = { ["url"] = image_large }
      end
      if image_thumb then
        expect("sendEmbed", image_thumb, 7, "string")
        data["embeds"][1]["thumbnail"] = { ["url"] = image_thumb }
      end
      if username then
        expect("sendEmbed", username, 8, "string")
        data["username"] = username
      end
      if avatar then
        expect("sendEmbed", avatar, 9, "string")
        data["avatar_url"] = avatar
      end
      
      local success = send(_.url, textutils.serializeJSON(data), { ["Content-Type"] = "application/json", ["Source"] = "Minecraft/ComputerCraft/DiscordHook" })
      if success then _.sentMessages = _.sentMessages + 1 end
      return success
    end

    return true, _
  end
end

return DiscordHook

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...