Created
March 18, 2018 18:48
-
-
Save CapsAdmin/eeb47b008fdf430e7636b961088ae342 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local discord = {} | |
local config = assert(file.Read("discordrelay_config.json"), "Discord config file not found...") | |
config = assert(util.JSONToTable(config), "Invalid Discord config!") | |
local endpoints = endpoints or {} | |
endpoints.base = "https://discordapp.com/api" | |
endpoints.channels = endpoints.base .. "/channels" | |
endpoints.guilds = endpoints.base .. "/guilds" | |
endpoints.users = endpoints.base .. "/users" | |
endpoints.webhooks = endpoints.base .. "/webhooks" | |
local function getCode(code) | |
local codes = { | |
[200] = "OK", | |
[201] = "CREATED", | |
[204] = "NO CONTENT", | |
[304] = "NOT MODIFIED", | |
[400] = "BAD REQUEST", | |
[401] = "UNAUTHORIZED", | |
[403] = "FORBIDDEN", | |
[404] = "NOT FOUND", | |
[405] = "METHOD NOT ALLOWED", | |
[429] = "TOO MANY REQUESTS", | |
[502] = "GATEWAY UNAVAILABLE", | |
} | |
return string.format("[%s] %s", tostring(code), code > 502 and "INTERNAL SERVER ERROR" or codes[code] or "Unknown Error") | |
end | |
local function getJsonCode(code) | |
local jsoncodes = { | |
[10001] = "Unknown account", | |
[10002] = "Unknown application", | |
[10003] = "Unknown channel", | |
[10004] = "Unknown guild", | |
[10005] = "Unknown integration", | |
[10006] = "Unknown invite", | |
[10007] = "Unknown member", | |
[10008] = "Unknown message", | |
[10009] = "Unknown overwrite", | |
[10010] = "Unknown provider", | |
[10011] = "Unknown role", | |
[10012] = "Unknown token", | |
[10013] = "Unknown user", | |
[10014] = "Unknown Emoji", | |
[20001] = "Bots cannot use this endpoint", | |
[20002] = "Only bots can use this endpoint", | |
[30001] = "Maximum number of guilds reached (100)", | |
[30002] = "Maximum number of friends reached (1000)", | |
[30003] = "Maximum number of pins reached (50)", | |
[30005] = "Maximum number of guild roles reached (250)", | |
[30010] = "Too many reactions", | |
[30013] = "Maximum number of guild channels reached (500)", | |
[40001] = "Unauthorized", | |
[50001] = "Missing access", | |
[50002] = "Invalid account type", | |
[50003] = "Cannot execute action on a DM channel", | |
[50004] = "Widget Disabled", | |
[50005] = "Cannot edit a message authored by another user", | |
[50006] = "Cannot send an empty message", | |
[50007] = "Cannot send messages to this user", | |
[50008] = "Cannot send messages in a voice channel", | |
[50009] = "Channel verification level is too high", | |
[50010] = "OAuth2 application does not have a bot", | |
[50011] = "OAuth2 application limit reached", | |
[50012] = "Invalid OAuth state", | |
[50013] = "Missing permissions", | |
[50014] = "Invalid authentication token", | |
[50015] = "Note is too long", | |
[50016] = "Provided too few or too many messages to delete. Must provide at least 2 and fewer than 100 messages to delete.", | |
[50019] = "A message can only be pinned to the channel it was sent in", | |
[50021] = "Cannot execute action on a system message", | |
[50034] = "A message provided was too old to bulk delete", | |
[50035] = "Invalid Form Body", | |
[50036] = "An invite was accepted to a guild the application's bot is not in", | |
[50041] = "Invalid API version", | |
[90001] = "Reaction blocked" | |
} | |
return string.format("[%s] %s", tostring(code), jsoncodes[code]) | |
end | |
local function isValidHTTPMethod(method) | |
local valid = { | |
["GET"] = true, | |
["POST"] = true, | |
["HEAD"] = true, | |
["PUT"] = true, | |
["DELETE"] = true, | |
["PATCH"] = true, | |
["OPTIONS"] = true | |
} | |
return valid[method:upper()] or false | |
end | |
local function check(cnd, f) | |
return cnd and (cnd(f) and f) | |
end | |
function discord.HTTPRequest(x) | |
assert(istable(x), "Invalid HTTP Request") | |
--[ A Glua HTTP Request looks like this: | |
---- method : string | |
---- url : string | |
---- type : string | |
---- headers : string | |
---- parameters : kv table | |
---- body : string | |
---- success : function | |
---- failed : function | |
--] | |
-- validation | |
local method = assert(x.method and check(isstring, x.method) and check(isValidHTTPMethod, x.method), "Invalid HTTP Method: " .. tostring(x.method or "")) | |
local uri = assert(x.uri and check(isstring, x.uri), "Invalid URI: " .. tostring(x.uri or "")) | |
local ctype = check(isstring, x.type) or "application/json" | |
local parameters = check(istable, x.parameters) or "" | |
local body = x.body and (check(istable, x.body) and util.TableToJSON(x.body)) or util.TableToJSON({ | |
["content"] = tostring(x.body) | |
}) or "" | |
local headers = x.headers and assert(check(istable, x.headers), "Invalid Headers: " .. tostring(x.headers)) or assert(config.token and { | |
["Authorization"] = "Bot " .. config.token, | |
["Content-Type"] = "application/json", | |
["Content-Length"] = #body, | |
["User-Agent"] = "DiscordBot (https://github.com/PAC3-Server/gm-http-discordrelay, 1.0.0)" | |
}, "Missing Discord Token...") | |
local success = check(isfunction, x.success) or nil | |
local failed = check(isfunction, x.failed) or nil | |
local request = { | |
method = method, | |
url = uri, | |
type = ctype, | |
headers = headers, | |
parameters = parameters, | |
body = body, | |
success = success, | |
failed = failed, | |
} | |
return HTTP(request) | |
end | |
function discord.GetChannel(id) | |
local a = nil | |
discord.HTTPRequest({ | |
method = "get", | |
uri = endpoints.channels .. "/" .. tostring(id), | |
success = function(...) a = ... end | |
}) | |
while true do | |
coroutine.yield() | |
if a then | |
return a | |
end | |
end | |
end | |
local co = coroutine.create(function() | |
print(discord.GetChannel("278624981192146944")) | |
end) | |
hook.Add("Think", "coroutine_test", function() | |
local ok, err = coroutine.resume(co) | |
if not ok then | |
print(ok, err) | |
hook.Remove("Think", "coroutine_test") | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment