Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

code works with one function but not another (DISCORDIA)?

Asked by
Imperialy 149
5 years ago
Edited 5 years ago

so I have my minify function, but when I try to minify the same thing but one is from an http request from pastebin, it outputs differently

local function minify(msg)
    local scr = {
        msg

    }

    local result = ""
    local result = ""

    function getLines(src)
        local result = {}
        local start = 1
        for i = 1,#src do
            if string.sub(src, i, i) == "\n" then
                table.insert(result, #result + 1, src:sub(start, i - 1))
                start = i + 1
            end
        end
        table.insert(result, #result + 1, src:sub(start, #src))
        return result
    end

    for i,v in pairs(getLines(unpack(scr))) do
        local s,e = string.find(v, "%-%-")
        if string.find(v, "%-%-") then
            result = result..v:sub(1, s - 1)
        else
            result = result..v.." "
        end
        result=result.."\n" 
    end

    result = {result}

    local new = string.gsub(unpack(result)," +"," ")
    new = string.gsub(new,"(\r?\n)%s*\r?\n","%1")
    new = string.gsub(new," \n","; ")
    new = string.gsub(new,"\t","")
    new = string.gsub(new,"do;","do")
    new = string.gsub(new,"do ;","do")
    new = string.gsub(new,"then;","then")
    new = string.gsub(new,"then ;","then")
    new = string.gsub(new,"else;","else")
    new = string.gsub(new,"else ;","else")
    new = string.gsub(new,"repeat;","repeat")
    new = string.gsub(new,"repeat ;","repeat")
    new = string.gsub(new,";;",";")
    new = string.gsub(new,",,",",")
    new = string.gsub(new,"};","}") 
    new = string.gsub(new,"{;","{")
    new = string.gsub(new,"%);","%)")
    new = string.gsub(new,"%(;","%(")
    new = string.gsub(new," ;","")
    new = string.gsub(new,"%);",")")
    if new:sub(1,1) == ";" then
        new = new:gsub("; ","",1)
    end
    return new:sub(1,new:len() - 1)
end

lets say the code I want to minify is this

for i = 1,50 do
    print(i)
end

when I minify the actual message from discord, it outputs for i = 1,50 do print(i) end; but, If that code was in a pastebin, it outputs

for i = 1,50 do
 ; print(i)
 ; end;

here is the way I formatted it

    if content:lower():sub(1,8) == prefix.."minify " then
        if content:lower():find("https://pastebin.com") then
            coroutine.wrap(function()
                local link = content:sub(9)
                if link:find("raw") then message:reply("remove the raw from the script") return end
                link = link:gsub("https://pastebin.com","https://pastebin.com/raw")
                local result, body = http.request("GET", link)
                message:reply(minify(body)) -- returns that weird version of the code
                print(body) -- prints the same exact code
                local channel = message.channel
                channel:send{
                    file = {'script.txt',minify(body)}
                }
            end)()
        else
            local text = minify(content:sub(16,content:len() - 3))
            local digits = "```lua\n"..text.."\n```"
            local t = "!minify "
            if t..digits ~= content then
                message:reply(digits) -- works
            else
                message:reply("this can not be minified")
            end
        end
    end

Answer this question