Lua Discord webhook 错误图像不显示

问题描述 投票:0回答:1

当我尝试使用下面的代码通过 Discord webhook 上传图像时,一切正常,除了 Discord 没有在服务器上正确上传我的文件。当我点击“image.png”或尝试下载它时,它会将我带到此无效链接https://cdn.discordapp.com/attachments/1086674608368931027/1149816147466780672/image.png。我知道这 99% 都是我的错,而不是 Discord 的错,但我就是找不到错误。如果这里有人能帮助我,那就太好了:D Screenshot of the Discord channel This is the image that I try to send (before I uploaded it it was .png now it is .jpg I dont know why)

function base64_encode(data)
    local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
    return ((data:gsub('.', function(x)
        local r, b = '', x:byte()
        for i = 8, 1, -1 do
            r = r .. (b % 2 ^ i - b % 2 ^ (i - 1) > 0 and '1' or '0')
        end
        return r
    end) .. '0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
        if #x < 6 then
            return ''
        end
        local c = 0
        for i = 1, 6 do
            c = c + (x:sub(i, i) == '1' and 2 ^ (6 - i) or 0)
        end
        return b64chars:sub(c + 1, c + 1)
    end) .. (#data % 3 == 1 and '==' or #data % 3 == 2 and '=' or ''))
end

function sendDiscordWebhook(webhookUrl, message, imageFilePath)
    local boundary = "----------------------------" .. os.time()

    local headers = {
        ["Content-Type"] = "multipart/form-data; boundary=" .. boundary
    }

    local body = {}
    table.insert(body, "--" .. boundary)
    table.insert(body, 'Content-Disposition: form-data; name="content"\r\n\r\n' .. message)

    local imageFileData
    if imageFilePath then
        local file = io.open(imageFilePath, "rb")
        if file then
            imageFileData = file:read("*all")
            file:close()
            local fileName = imageFilePath:match("^.+[\\/](.+)$")
            local fileExtension = fileName:match("%.([^%.]+)$")
            local contentType = "image/" .. fileExtension
            table.insert(body, "--" .. boundary)
            table.insert(body, 'Content-Disposition: form-data; name="file[0]"; filename="' .. fileName .. '"')
            table.insert(body, 'Content-Type: ' .. contentType)
            table.insert(body, "")
            table.insert(body, "data:" .. contentType .. ";base64," .. base64_encode(imageFileData))
        end
    end

    table.insert(body, "--" .. boundary .. "--")
    local requestBody = table.concat(body, "\r\n")

    PerformHttpRequest(webhookUrl, function(err, text, headers)
        print("TEXT:" .. text)
        print("ERROR:" .. err)
        print("HEADERS:" .. json.encode(headers))
    end, 'POST', requestBody, headers)
end

sendDiscordWebhook("https://discord.com/api/webhooks/MYWEBHOOK/MYWEBHOOK", "Test", "image.png")

我现在尝试了两个多月的时间来让它与数以千计的代码修改一起工作,但我认为我只是愚蠢。

post lua discord webhooks fivem
1个回答
0
投票

您面临的问题可能与您在 Lua Discord webhook 代码中编码和发送图像数据的方式有关。这是代码的修改版本,应正确处理图像附件:

function sendDiscordWebhook(webhookUrl, message, imageFilePath)
local boundary = "---------------------------" .. os.time()

local headers = {
    ["Content-Type"] = "multipart/form-data; boundary=" .. boundary
}

local body = {}
table.insert(body, "--" .. boundary)
table.insert(body, 'Content-Disposition: form-data; name="content"\r\n\r\n' .. message)

if imageFilePath then
    local fileName = imageFilePath:match("^.+[\\/](.+)$")
    local fileExtension = fileName:match("%.([^%.]+)$")
    local contentType = "image/" .. fileExtension

    table.insert(body, "--" .. boundary)
    table.insert(body, 'Content-Disposition: form-data; name="file"; filename="' .. fileName .. '"')
    table.insert(body, 'Content-Type: ' .. contentType)
    table.insert(body, "")

    local imageFileData
    local file = io.open(imageFilePath, "rb")
    if file then
        imageFileData = file:read("*all")
        file:close()
        table.insert(body, imageFileData)
    else
        print("Failed to open the image file: " .. imageFilePath)
        return
    end
end

table.insert(body, "--" .. boundary .. "--")
local requestBody = table.concat(body, "\r\n")

PerformHttpRequest(webhookUrl, function(err, text, headers)
    if err == 200 then
        print("Message sent successfully!")
    else
        print("Failed to send message. Error code: " .. err)
    end
end, 'POST', requestBody, headers)
© www.soinside.com 2019 - 2024. All rights reserved.