JaredScar / Badger_Discord_API

A FiveM API Script [created by Badger]
MIT License
56 stars 48 forks source link

Using POST or PATCH requests #20

Closed ghost closed 2 years ago

ghost commented 2 years ago

When I use the DiscordRequest function with POST or PATCH methods, and try to pass jsondata with it, I get error code 400 (bad request). Example for changing user nickname:

function SetNickname(user, nickname)
    local discordId = nil
    for _, id in ipairs(GetPlayerIdentifiers(user)) do
        if string.match(id, "discord:") then
            discordId = string.gsub(id, "discord:", "")
            break
        end
    end

    if discordId then
        local name = nickname or ""
        local endpoint = ("guilds/%s/members/%s"):format(Config.GuildId, discordId)
        local member = DiscordRequest("PATCH", endpoint, {nick = name})
        print(member.code) --Returns 400
    end
end

By printing out jsondata in the DiscordRequest function, I get a table. My guess is that the Discord API can't read that, hence why it returns error code 400. After trying to solve this issue for a while now with no success, could anyone tell me a way to fix it without doing the HTTP request in my SetNickname function itself? image

JaredScar commented 2 years ago

Interesting, I may have to fix this at some point unless someone is lovely enough to make a PR for it down the line. I will keep this open until then

fsocietystudios commented 2 years ago

Hey!

I strongly believe that line 84 in server.lua causing this problem:

#jsondata > 0 and json.encode(jsondata) or ""

Because, when trying to execute:

local jsonData = { 
    nick = 'foo'
}

print(#jsonData > 0 and json.encode(jsonData) or "")

For example, (using the same statement used at the mentioned line above), it returns the empty string. (Because #jsonData returns 0, even though the actual length isn't 0)

Will add a fix to my current PR Request.

gegelendvay commented 2 years ago

The above-mentioned part of the code was indeed one of the causes for this issue and after thorough testing based on the initial description, I found a solution:

--Remove json.encode() from line 84:
#jsondata > 0 and jsondata or ""

--Add json.encode() to the calling function(s):
local member = DiscordRequest("PATCH", endpoint, json.encode({nick = tostring(name)}))

You may see that I also added tostring(name) to the code as using variables also returned error code 400 before. During the test of said changes, print(#jsondata) returned the correct length.

JaredScar commented 2 years ago

The above-mentioned part of the code was indeed one of the causes for this issue and after thorough testing based on the initial description, I found a solution:

--Remove json.encode() from line 84:
#jsondata > 0 and jsondata or ""

--Add json.encode() to the calling function(s):
local member = DiscordRequest("PATCH", endpoint, json.encode({nick = tostring(name)}))

You may see that I also added tostring(name) to the code as using variables also returned error code 400 before. During the test of said changes, print(#jsondata) returned the correct length.

I could implement these changes myself, but what's the fun in that. Do you mind making a PR so you get credit for finding the fix? 😄 Thanks!

gegelendvay commented 2 years ago

Done. Additionally, I also updated the Discord API version as the current (default) one is said to be deprecated. I tested that too earlier, but since I only use one or two functions from this resource, I cannot guarantee that it won't interfere with some of the API endpoints found in this repo.

JaredScar commented 2 years ago

@gegelendvay If you get rid of v10, I'll merge it. Probably don't need to worry about changing from v6 until it's really needed

gegelendvay commented 2 years ago

Reverted it back to how it was before. If needed, I can test v10 with the complete repo.

JaredScar commented 2 years ago

Reverted it back to how it was before. If needed, I can test v10 with the complete repo.

Sure!