-
Notifications
You must be signed in to change notification settings - Fork 11
/
sv_discordchatrelay.lua
103 lines (88 loc) · 2.42 KB
/
sv_discordchatrelay.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- incredible-gmod.ru
-- Discord Chat Relay
-- !!!!! https://github.com/timschumi/gmod-chttp Required !!!!!
local Config = {
Webhook = "XXX", -- https://pastebin.com/Bdh5EgmM
SteamApiKey = "YYY" -- https://steamcommunity.com/dev/apikey
}
if pcall(require, "chttp") and CHTTP ~= nil then
HTTP = CHTTP
else
return MsgC(Color(255, 0, 0), "Discord Chat Relay ERROR!", Color(255, 255, 255), "Please install https://github.com/timschumi/gmod-chttp!")
end
local Avatars = {}
local AvatarsApi = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/"
local Red, White = Color(255, 0, 0), Color(255, 255, 255)
local function ParseJson(json, ...)
local tbl = util.JSONToTable(json)
if tbl == nil then return end
local args = {...}
for _, key in pairs(args) do
if tbl[key] then
tbl = tbl[key]
end
end
return tbl
end
local function GetAvatar(ply, callback, attempts)
attempts = attempts or 3
local sid = ply:SteamID64()
if Avatars[sid] then
return callback(Avatars[sid])
end
HTTP({
method = "get",
url = AvatarsApi,
parameters = {
key = Config.SteamApiKey,
steamids = sid
},
failed = function(error)
MsgC(Red, "Steam Avatar API HTTP Error:", White, error, "\n")
attempts = attempts - 1
if attempts > 0 then
GetAvatar(ply, callback, attempts)
else
Avatars[sid] = "https://i.imgur.com/up9fyXY.png"
callback(Avatars[sid])
end
end,
success = function(code, response)
local avatar = ParseJson(response, "response", "players", 1, "avatarfull")
if avatar then
Avatars[sid] = avatar
callback(avatar)
else
return MsgC(Red, "Steam Avatar API Error:", White, "Cant parse avatar\n")
end
end
})
end
hook.Add("PlayerAuthed", "DiscordChatRelay", function(ply)
GetAvatar(ply, function() end) -- pre-cache
end)
hook.Add("PlayerSay", "DiscordChatRelay", function(ply, text, isteam)
GetAvatar(ply, function(avatar)
HTTP({
method = "post",
type = "application/json; charset=utf-8",
headers = {
["User-Agent"] = "Discord Chat Relay",
},
url = Config.Webhook,
body = util.TableToJSON({
content = text,
username = ply:Nick(),
avatar_url = avatar
}),
failed = function(error)
MsgC(Red, "Discord API HTTP Error:", White, error, "\n")
end,
success = function(code, response)
if code ~= 204 then
MsgC(Red, "Discord API HTTP Error:", White, code, response, "\n")
end
end
})
end)
end)