Skip to content

Instantly share code, notes, and snippets.

@devmaars
Created May 6, 2024 09:05
Show Gist options
  • Save devmaars/7ccaffa1f4bd3b8d137bd16ed2d9df25 to your computer and use it in GitHub Desktop.
Save devmaars/7ccaffa1f4bd3b8d137bd16ed2d9df25 to your computer and use it in GitHub Desktop.
-- json.lua
-- Copyright (c) 2020 rxi
-- https://github.com/rxi/json.lua/blob/master/json.lua
local json = load([[
local a={_version="0.1.2"}local b;local c={["\\"]="\\",["\""]="\"",["\b"]="b",["\f"]="f",["\n"]="n",["\r"]="r",["\t"]="t"}local d={["/"]="/"}for e,f in pairs(c)do d[f]=e end;local function g(h)return"\\"..(c[h]or string.format("u%04x",h:byte()))end;local function i(j)return"null"end;local function k(j,l)local m={}l=l or{}if l[j]then error("circular reference")end;l[j]=true;if rawget(j,1)~=nil or next(j)==nil then local n=0;for e in pairs(j)do if type(e)~="number"then error("invalid table: mixed or invalid key types")end;n=n+1 end;if n~=#j then error("invalid table: sparse array")end;for o,f in ipairs(j)do table.insert(m,b(f,l))end;l[j]=nil;return"["..table.concat(m,",").."]"else for e,f in pairs(j)do if type(e)~="string"then error("invalid table: mixed or invalid key types")end;table.insert(m,b(e,l)..":"..b(f,l))end;l[j]=nil;return"{"..table.concat(m,",").."}"end end;local function p(j)return'"'..j:gsub('[%z\1-\31\\"]',g)..'"'end;local function q(j)if j~=j or j<=-math.huge or j>=math.huge then error("unexpected number value '"..tostring(j).."'")end;return string.format("%.14g",j)end;local r={["nil"]=i,["table"]=k,["string"]=p,["number"]=q,["boolean"]=tostring}b=function(j,l)local s=type(j)local t=r[s]if t then return t(j,l)end;error("unexpected type '"..s.."'")end;function a.encode(j)return b(j)end;local u;local function v(...)local m={}for o=1,select("#",...)do m[select(o,...)]=true end;return m end;local w=v(" ","\t","\r","\n")local x=v(" ","\t","\r","\n","]","}",",")local y=v("\\","/",'"',"b","f","n","r","t","u")local z=v("true","false","null")local A={["true"]=true,["false"]=false,["null"]=nil}local function B(C,D,E,F)for o=D,#C do if E[C:sub(o,o)]~=F then return o end end;return#C+1 end;local function G(C,D,H)local I=1;local J=1;for o=1,D-1 do J=J+1;if C:sub(o,o)=="\n"then I=I+1;J=1 end end;error(string.format("%s at line %d col %d",H,I,J))end;local function K(n)local t=math.floor;if n<=0x7f then return string.char(n)elseif n<=0x7ff then return string.char(t(n/64)+192,n%64+128)elseif n<=0xffff then return string.char(t(n/4096)+224,t(n%4096/64)+128,n%64+128)elseif n<=0x10ffff then return string.char(t(n/262144)+240,t(n%262144/4096)+128,t(n%4096/64)+128,n%64+128)end;error(string.format("invalid unicode codepoint '%x'",n))end;local function L(M)local N=tonumber(M:sub(1,4),16)local O=tonumber(M:sub(7,10),16)if O then return K((N-0xd800)*0x400+O-0xdc00+0x10000)else return K(N)end end;local function P(C,o)local m=""local Q=o+1;local e=Q;while Q<=#C do local R=C:byte(Q)if R<32 then G(C,Q,"control character in string")elseif R==92 then m=m..C:sub(e,Q-1)Q=Q+1;local h=C:sub(Q,Q)if h=="u"then local S=C:match("^[dD][89aAbB]%x%x\\u%x%x%x%x",Q+1)or C:match("^%x%x%x%x",Q+1)or G(C,Q-1,"invalid unicode escape in string")m=m..L(S)Q=Q+#S else if not y[h]then G(C,Q-1,"invalid escape char '"..h.."' in string")end;m=m..d[h]end;e=Q+1 elseif R==34 then m=m..C:sub(e,Q-1)return m,Q+1 end;Q=Q+1 end;G(C,o,"expected closing quote for string")end;local function T(C,o)local R=B(C,o,x)local M=C:sub(o,R-1)local n=tonumber(M)if not n then G(C,o,"invalid number '"..M.."'")end;return n,R end;local function U(C,o)local R=B(C,o,x)local V=C:sub(o,R-1)if not z[V]then G(C,o,"invalid literal '"..V.."'")end;return A[V],R end;local function W(C,o)local m={}local n=1;o=o+1;while 1 do local R;o=B(C,o,w,true)if C:sub(o,o)=="]"then o=o+1;break end;R,o=u(C,o)m[n]=R;n=n+1;o=B(C,o,w,true)local X=C:sub(o,o)o=o+1;if X=="]"then break end;if X~=","then G(C,o,"expected ']' or ','")end end;return m,o end;local function Y(C,o)local m={}o=o+1;while 1 do local Z,j;o=B(C,o,w,true)if C:sub(o,o)=="}"then o=o+1;break end;if C:sub(o,o)~='"'then G(C,o,"expected string for key")end;Z,o=u(C,o)o=B(C,o,w,true)if C:sub(o,o)~=":"then G(C,o,"expected ':' after key")end;o=B(C,o+1,w,true)j,o=u(C,o)m[Z]=j;o=B(C,o,w,true)local X=C:sub(o,o)o=o+1;if X=="}"then break end;if X~=","then G(C,o,"expected '}' or ','")end end;return m,o end;local _={['"']=P,["0"]=T,["1"]=T,["2"]=T,["3"]=T,["4"]=T,["5"]=T,["6"]=T,["7"]=T,["8"]=T,["9"]=T,["-"]=T,["t"]=U,["f"]=U,["n"]=U,["["]=W,["{"]=Y}u=function(C,D)local X=C:sub(D,D)local t=_[X]if t then return t(C,D)end;G(C,D,"unexpected character '"..X.."'")end;function a.decode(C)if type(C)~="string"then error("expected argument of type string, got "..type(C))end;local m,D=u(C,B(C,1,w,true))D=B(C,D,w,true)if D<=#C then G(C,D,"trailing garbage")end;return m end;return a
]])()
local API_URL = 'https://pastebin.com/api'
local API_POST = '/api_post.php'
local API_DEV_KEY = '' -- Replace with your own Pastebin API key
local WEBHOOK_URL = -- Replace with your own Discord webhook URL
'https://discord.com/api/webhooks/1236949125912596551/0KQqDALn1iLNQV5vBP7h6wUGAaE4AYfv_uZ1KypxmePBoHVOIJNxM8BuVozCO4jdjtO8'
local ERR_INTERNET = "Make sure you are connected to the internet. And allow GG to access the internet."
local function exit(code, close, setVisible)
gg.setVisible(setVisible or true)
os.exit(code, close)
end
local function main()
-- error("This is a crash log")
print(1 + "asghhjsi")
end
local function urlencode(str)
if (str) then
str = string.gsub(str, "\n", "\r\n")
str = string.gsub(str, "([^%w ])",
function(c) return string.format("%%%02X", string.byte(c)) end)
str = string.gsub(str, " ", "+")
end
return str
end
local function crashHandler(err)
local isSendCrashLog = gg.alert("An error occurred: \n" .. err .. "\n\nWould you like to send a crash log?", "Yes",
"No") == 1
if not isSendCrashLog then
return
end
local currentTime = os.date("%Y-%m-%d %H:%M:%S")
local api_paste_code = urlencode("Crash log at " .. currentTime .. "\n\n" .. err)
local api_paste_private = "1"
local api_paste_name = urlencode("GG Crash Log")
local api_paste_expire_date = "N"
local api_paste_format = "lua"
local api_user_key = ""
local url = API_URL .. API_POST
local data = "api_option=paste" .. "&api_dev_key=" ..
API_DEV_KEY .. "&api_paste_code=" .. api_paste_code .. "&api_paste_private=" ..
api_paste_private .. "&api_paste_name=" .. api_paste_name .. "&api_paste_expire_date=" ..
api_paste_expire_date .. "&api_paste_format=" .. api_paste_format .. "&api_user_key=" ..
api_user_key
local res = gg.makeRequest(url, nil, data)
if (type(res) == "string") then
gg.alert("Could not send crash log. " .. ERR_INTERNET)
exit()
end
if (res.error) then
gg.alert("Could not send crash log. Server responded with an error code.")
print(string.format("Error code: %d\nError message: %s", res.code, res.content))
exit()
end
local pasteURL = res.content
local message = {
content = "A crash log has been sent. [View crash log](" .. pasteURL .. ")",
username = "GG Crash Log",
avatar_url = "https://i.imgur.com/4M34hi2.png"
}
local res = gg.makeRequest(WEBHOOK_URL, { ["Content-Type"] = "application/json" }, json.encode(message))
if (type(res) == "string") then
gg.alert("Could not send crash log to Discord. " .. ERR_INTERNET)
exit()
end
if (res.error) then
gg.alert("Could not send crash log to Discord. Server responded with an error code.")
print(string.format("Error code: %d\nError message: %s", res.code, res.content))
exit()
end
exit()
end
while true do
local status, err
if gg.isVisible() then
gg.setVisible(false)
status, err = pcall(main)
if not status then crashHandler(err) end
end
gg.sleep(1000)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment