Jump to content

Is there a way to send a message to a discord webhook?


Wembbu

Recommended Posts

Posted

I tried to find something on stackoverflow but couldnt get it working
importing packages seems to not be an option in game guardian scripts
is there any other way to import packages that a script inside game guardian would recognize?
or do i just give up and move on with life?

explanation would be awesome but just a function would do the thing too

links i tried to use (i ended up taking the code from codepal which didnt work): 
https://codepal.ai/code-generator/query/uOnHU0FP/lua-function-discord-webhook
https://stackoverflow.com/questions/62601549/sending-a-message-to-discord-webhook-with-lua
https://stackoverflow.com/questions/37555226/luasocket-http-requests-always-respond-with-a-redirect-301-or-302

Posted

I wonder if you just copy pasted that code into your script, in case you have done that, please  note that gg scripting api is not native lua implementation, the interpreter is written in Java i even wonder if it can load C module. and yeah those module used in the code from the link you provided are C module they are not available in game guardian.
So bellow is how you can query discord web-hook from your gg script.
And also you may want to check the official documentation to see all possible fields, Also @BadCase did write a module about discord api, i have not tested it but you might also take a look,it is here.
In the code bellow i have left the url of the hook, you might want to join the dummy server and test, but be aware i will delete the server any time soon https://discord.gg/shDHr2Vv
 

local gg = gg

local webhook_url =
"https://discord.com/api/webhooks/1120842685004132472/jqZvAr8MyYShCdxzhgogYD39FnIFui_CSwsIG9U8t8jubZW1l9im5F6QcOx6V-WLzhUK"


local function toJson(tbl)
  local json = '{'

  for k, v in pairs(tbl) do
    local kType = type(k)

    if kType == 'string' then
      json = json .. '"' .. k .. '":'
    elseif kType == 'number' then
      json = json .. k .. ':'
    end

    local vType = type(v)

    if vType == 'string' then
      json = json .. '"' .. v .. '"'
    elseif vType == 'number' then
      json = json .. v
    elseif vType == 'table' then
      json = json .. toJson(v)
    end

    json = json .. ','
  end

  json = json:sub(1, -2) .. '}'

  return json
end

local function buildQuery(url, query)
  query = query or {}

  local query_string = ''
  for k, v in pairs(query) do
    query_string = query_string .. k .. '=' .. tostring(v) .. '&'
  end
  query_string = query_string:sub(1, -2)

  return url .. '?' .. query_string
end

local query = {
  wait = false,
}

local payload = {
  content = "Hello, World!",
}

local payload_json = toJson(payload)

local headers = {
  ["Content-Type"] = "application/json",
}

local res = gg.makeRequest(buildQuery(webhook_url, query), headers, payload_json)

if res.code == 204 then
  gg.alert("Success")
else
  gg.alert("Error")
end

print(res)

 

Posted
5 minutes ago, MAARS said:

I wonder if you just copy pasted that code into your script, in case you have done that, please  note that gg scripting api is not native lua implementation, the interpreter is written in Java i even wonder if it can load C module. and yeah those module used in the code from the link you provided are C module they are not available in game guardian.
So bellow is how you can query discord web-hook from your gg script.
And also you may want to check the official documentation to see all possible fieds, Also @BadCase did write a module about discord api, i have not tested it but you might also take a look,it is here.

In the code bellow i have left the url of the hook, you might want to join the dummy server and test, but be aware i will delete the server any time soon https://discord.gg/shDHr2Vv
 

local gg = gg

local webhook_url =
"https://discord.com/api/webhooks/1120842685004132472/jqZvAr8MyYShCdxzhgogYD39FnIFui_CSwsIG9U8t8jubZW1l9im5F6QcOx6V-WLzhUK"


local function toJson(tbl)
  local json = '{'

  for k, v in pairs(tbl) do
    local kType = type(k)

    if kType == 'string' then
      json = json .. '"' .. k .. '":'
    elseif kType == 'number' then
      json = json .. k .. ':'
    end

    local vType = type(v)

    if vType == 'string' then
      json = json .. '"' .. v .. '"'
    elseif vType == 'number' then
      json = json .. v
    elseif vType == 'table' then
      json = json .. toJson(v)
    end

    json = json .. ','
  end

  json = json:sub(1, -2) .. '}'

  return json
end

local function buildQuery(url, query)
  query = query or {}

  local query_string = ''
  for k, v in pairs(query) do
    query_string = query_string .. k .. '=' .. tostring(v) .. '&'
  end
  query_string = query_string:sub(1, -2)

  return url .. '?' .. query_string
end

local query = {
  wait = false,
}

local payload = {
  content = "Hello, World!",
}

local payload_json = toJson(payload)

local headers = {
  ["Content-Type"] = "application/json",
}

local res = gg.makeRequest(buildQuery(webhook_url, query), headers, payload_json)

if res.code == 204 then
  gg.alert("Success")
else
  gg.alert("Error")
end

print(res)

 

Thanks! 

I'm honestly afraid of those requests sockets and everything related to Internet connection in scripting, it's like a whole different world for me to Explore. 

All that mess with gg written in Java, C modules on codepal and lua even existing really confused me. 

I Will try to implement the function you wrote for me in my script, and i Will test it in a bit. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.