1
0
mirror of https://github.com/troit5ky/gmod_discord_relay.git synced 2025-03-04 03:03:15 -05:00
This commit is contained in:
Dmitry Troitsky 2022-10-06 12:47:22 +03:00
parent 2822b76483
commit 7518bc2226
3 changed files with 37 additions and 37 deletions

View File

@ -3,5 +3,5 @@ local chat_AddText = chat.AddText
net.Receive("!!discord-receive", function()
local msg = net.ReadTable()
chat_AddText( Discord.prefixClr, "["..Discord.prefix.."] ", Color(255, 255, 255), msg.author..": ", msg.content)
chat_AddText( Discord.prefixClr, "["..Discord.prefix.."] ", Color(255, 255, 255), msg.author..": ", msg.content )
end)

View File

@ -9,10 +9,10 @@ Discord.socket = Discord.socket or GWSockets.createWebSocket("wss://gateway.disc
local socket = Discord.socket
local function broadcastMsg(msg)
print('[Discord] ' .. msg.author..': '.. msg.content)
print( '[Discord] ' .. msg.author..': '.. msg.content )
net.Start("!!discord-receive")
net.WriteTable(msg)
net.Start( '!!discord-receive' )
net.WriteTable( msg )
net.Broadcast()
end
@ -26,46 +26,46 @@ local function heartbeat()
end
local function createHeartbeat()
timer.Create('!!discord_hearbeat', 10, 0, function()
timer.Create( '!!discord_hearbeat', 10, 0, function()
heartbeat()
end)
end )
end
function socket:onMessage(txt)
local resp = util.JSONToTable(txt)
function socket:onMessage( txt )
local resp = util.JSONToTable( txt )
if not resp then return end
if Discord.debug then
print("[Discord] Received: ")
print( '[Discord] Received: ' )
PrintTable(resp)
end
if resp.op == 10 and resp.t == nil then createHeartbeat() end
if resp.op == 1 then heartbeat() end
if resp.d then
if resp.t == "MESSAGE_CREATE" && resp.d.channel_id == Discord.readChannelID && resp.d.content != '' then
if resp.t == 'MESSAGE_CREATE' && resp.d.channel_id == Discord.readChannelID && resp.d.content != '' then
if resp.d.author.bot == true then return end
if string.sub(resp.d.content, 0, 1) == Discord.botPrefix then
command = string.sub(resp.d.content, 2)
if string.sub( resp.d.content, 0, 1 ) == Discord.botPrefix then
command = string.sub( resp.d.content, 2 )
if Discord.commands[command] then Discord.commands[command]() end
return
end
broadcastMsg({
['author'] = resp.d.author.username,
['content'] = resp.d.content
[ 'author' ] = resp.d.author.username,
[ 'content' ] = resp.d.content
})
end
end
end
function socket:onError(txt)
print("[Discord] Error: ", txt)
function socket:onError( txt )
print( '[Discord] Error: ', txt )
end
function socket:onConnected()
print("[Discord] connected to Discord server")
print( '[Discord] connected to Discord server' )
local req = [[
{
"op": 2,
@ -89,21 +89,21 @@ function socket:onConnected()
]]
heartbeat()
timer.Simple(3, function() socket:write(req) end)
timer.Simple( 3, function() socket:write(req) end )
end
function socket:onDisconnected()
print("[Discord] WebSocket disconnected")
timer.Remove('!!discord_hearbeat')
print( '[Discord] WebSocket disconnected' )
timer.Remove( '!!discord_hearbeat' )
if Discord.isSocketReloaded != true then
print('[Discord] WebSocket reload in 5 sec...')
timer.Simple(5, function() socket:open() end)
print( '[Discord] WebSocket reload in 5 sec...' )
timer.Simple( 5, function() socket:open() end )
end
end
print('[Discord] Socket init...')
timer.Simple(3, function()
print( '[Discord] Socket init...' )
timer.Simple( 3, function()
socket:open()
Discord.isSocketReloaded = false
end)
end )

View File

@ -9,13 +9,13 @@ local coroutine_resume = coroutine.resume
local coroutine_create = coroutine.create
function Discord.send(form)
if type(form) ~= "table" then Error("[Discord] invalid type!") return end
if type( form ) ~= "table" then Error( '[Discord] invalid type!' ) return end
local json = util_TableToJSON(form)
CHTTP({
["failed"] = function(msg)
print("[Discord] "..msg)
["failed"] = function( msg )
print( "[Discord] "..msg )
end,
["method"] = "POST",
["url"] = Discord.webhook,
@ -25,7 +25,7 @@ function Discord.send(form)
end
local function getAvatar(id, co)
http_Fetch("https://steamcommunity.com/profiles/"..id.."?xml=1",
http_Fetch( "https://steamcommunity.com/profiles/"..id.."?xml=1",
function(body)
local _, _, url = string.find(body, '<avatarFull>.*.(https://.*)]].*\n.*<vac')
tmpAvatars[id] = url
@ -34,13 +34,13 @@ local function getAvatar(id, co)
end,
function (msg)
Error("[Discord] error getting avatar ("..msg..")")
end)
end )
end
local function formMsg(ply, str)
local id = tostring(ply:SteamID64())
local function formMsg( ply, str )
local id = tostring( ply:SteamID64() )
local co = coroutine_create(function()
local co = coroutine_create( function()
local form = {
["username"] = ply:Nick(),
["content"] = str,
@ -51,16 +51,16 @@ local function formMsg(ply, str)
}
Discord.send(form)
end)
end )
if tmpAvatars[id] == nil then
getAvatar(id, co)
getAvatar( id, co )
else
coroutine_resume(co)
coroutine_resume( co )
end
end
local function playerConnect(ply)
local function playerConnect( ply )
local form = {
["username"] = Discord.hookname,
["embeds"] = {{