0

I'm new to LUA and tried learning coding this language with Garrys Mod.

I want to get the messages from the Garrys Mod chat and send them into a Discord channel with a webhook.

It works, but I tried expanding this project with embeded messages. I need JSON for this and used json.lua as a library.

But as soon as I send a message I retrieve the following error message:

attempt to index global 'json' (a nil value)

The code that causes the error is the following:

json.encode({ { 
        ["embeds"] = { 
            ["description"] = text, 
            ["author"] = { 
                ["name"] = ply:Nick()
            }, 
        }, 
    } }),

The complete code:

AddCSLuaFile()
json = require("json")

webhookURL = "https://discordapp.com/api/webhooks/XXX"
local DiscordWebhook = DiscordWebhook or {}

hook.Add( "PlayerSay", "SendMsg", function( ply, text )
    t_post = {
        content = json.encode({ { 
            ["embeds"] = { 
                ["description"] = text, 
                ["author"] = { 
                    ["name"] = ply:Nick()
                }, 
            }, 
        } }),
        username = "Log",
    } 
    http.Post(webhookURL, t_post)
end )

I hope somebody can help me

6
  • How have you loaded the json library? Commented Jul 17, 2018 at 16:34
  • json = require("json") Commented Jul 17, 2018 at 18:20
  • Then the global variable json should not be nil. Perhaps you wrote local json = require("json") in a different file? Commented Jul 17, 2018 at 19:20
  • I've updated this thread with the whole code. Hope this can help, because the error is still not fixed Commented Jul 17, 2018 at 21:13
  • @Lesh What if you try to load json to local using local json = require "json"? Commented Jul 18, 2018 at 15:52

1 Answer 1

1

Garry's Mod does provide two functions to work with json.

They are:

util.TableToJSON( table table, boolean prettyPrint=false )

and

util.JSONToTable( string json )

There is no need to import json and if I recall correctly it isn't even possible.

For what you want to do you need to build your arguments as a table like this:

local arguments = {
["key"] = "Some value",
["42"] = "Not always the answer",
["deeper"] = {
  ["my_age"] = 22,
  ["my_name"] = getMyName()
},
["even more"] = from_some_variable

and then call

local args_as_json = util.TableToJSON(arguments)

Now you can pass args_as_json to your

http.Post( string url, table parameters, function onSuccess=nil, function onFailure=nil, table headers={} )
Sign up to request clarification or add additional context in comments.

6 Comments

Well, now I recieve an output message in Discord, but its not formated like a normal embedded message
@Lesh Do you still need help with this?
yes, I'm sorry for late response. So basically what I've done was creating a local argument with all my JSON Table. Then I've used util.TableToJSON but my http.Post only contains this http.Post(webhookURL, { content = act_msg, username = "Hook"} ). But the output isn't formated and will return this {"embeds":{"author":{"name":"Lesh"},"description":"test"}}
@Lesh so you want to pretty print your json? right? util.TableToJSON has a second parameter, which you can set to true for this. You can also see more here: wiki.garrysmod.com/page/util/TableToJSON
I'm sorry, I meant instead of a formated json code an Embed output like this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.