Skip to content

Commit

Permalink
Adding test connection button to settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
manix84 committed Jan 26, 2021
1 parent 25a93ad commit 75ae71e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
40 changes: 38 additions & 2 deletions lua/autorun/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ util.AddNetworkString("drawMute")
util.AddNetworkString("connectDiscordID")
util.AddNetworkString("discordPlayerTable")
util.AddNetworkString("request_discordPlayerTable")
util.AddNetworkString("discordTestConnection")
util.AddNetworkString("request_discordTestConnection")

CreateConVar("discord_endpoint", "http://localhost:37405", 1, "Sets the node bot endpoint.")
CreateConVar("discord_api_key", "", 1, "Sets the node bot api-key.")
Expand Down Expand Up @@ -121,8 +123,26 @@ function commonRoundState()
end

function joinMessage(target_ply)
playerMessage('JOIN_DISCORD_PROMPT', target_ply, GetConVar("discord_server_link"):GetString())
playerMessage('CONNECTION_INSTRUCTIONS', target_ply)
playerMessage("JOIN_DISCORD_PROMPT", target_ply, GetConVar("discord_server_link"):GetString())
playerMessage("CONNECTION_INSTRUCTIONS", target_ply)
end

function testConnection(callback)
timer.Create("testConnectionTimeout", 2.0, 0, function()
local responseTable = {}
responseTable['success'] = false
responseTable['error'] = "host connection failure"
responseTable['errorMsg'] = "host connection failure"
responseTable['errorId'] = "HOST_MISSCONFIGURED"

callback(responseTable)
timer.Remove("testConnectionTimeout")
end)
timer.Start("testConnectionTimeout")
httpFetch("sync", {}, function(res)
timer.Remove("testConnectionTimeout")
callback(res)
end)
end

net.Receive("connectDiscordID", function( len, calling_ply )
Expand All @@ -149,6 +169,22 @@ net.Receive("request_discordPlayerTable", function( len, calling_ply )
net.Send(calling_ply)
end)

net.Receive("request_discordTestConnection", function( len, calling_ply )
if !calling_ply:IsSuperAdmin() then
return
end

testConnection(function (res)
local connectionsJSON = util.TableToJSON(res)
local compressedConnections = util.Compress(connectionsJSON)

net.Start("discordTestConnection")
net.WriteUInt(#compressedConnections, 32)
net.WriteData(compressedConnections, #compressedConnections)
net.Send(calling_ply)
end)
end)

hook.Add("PlayerSay", "discord_PlayerSay", function(target_ply, msg)
if (string.sub(msg,1,9) != '!discord ') then
return
Expand Down
59 changes: 51 additions & 8 deletions lua/ulx/xgui/settings/discord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ function discord.processModules()
if module.mtype == "discord" and ( not module.access or LocalPlayer():query( module.access ) ) then
local w,h = module.panel:GetSize()
if w == h and h == 0 then module.panel:SetSize( 275, 322 ) end

if module.panel.scroll then --For DListLayouts
module.panel.scroll.panel = module.panel
module.panel = module.panel.scroll
end --if
module.panel:SetParent( discord.panel )

local line = discord.catList:AddLine( module.name, i )
if ( module.panel == discord.curPanel ) then
discord.curPanel = nil
Expand All @@ -78,7 +78,7 @@ xlib.makelabel{
}

--Mute Options
local discord_settings_mute_options_Category = vgui.Create( "DCollapsibleCategory", discord_settings_panel )
local discord_settings_mute_options_Category = vgui.Create( "DCollapsibleCategory", discord_settings_panel )
discord_settings_mute_options_Category:SetSize( 393, 45 )
discord_settings_mute_options_Category:SetExpanded( true )
discord_settings_mute_options_Category:SetLabel( "Mute Options" )
Expand Down Expand Up @@ -114,9 +114,9 @@ mute_duration:SetDisabled( mute_round:GetChecked() )
discord_settings_mute_options_List.AddItem(mute_duration)

--Config
local discord_settings_config_Category = vgui.Create( "DCollapsibleCategory", discord_settings_panel )
local discord_settings_config_Category = vgui.Create( "DCollapsibleCategory", discord_settings_panel )
discord_settings_config_Category:SetSize( 393, 115 )
discord_settings_config_Category:SetExpanded( false )
discord_settings_config_Category:SetExpanded( true )
discord_settings_config_Category:SetLabel( "Config" )

local discord_settings_config_List = vgui.Create( "DPanelList", discord_settings_config_Category )
Expand Down Expand Up @@ -225,14 +225,14 @@ discord_settings_config_List.AddItem(xlib.makecheckbox{
})

--Bot Connection
local discord_botConnection_Category = vgui.Create( "DCollapsibleCategory", discord_settings_panel )
local discord_botConnection_Category = vgui.Create( "DCollapsibleCategory", discord_settings_panel )
discord_botConnection_Category:SetSize( 393, 25 )
discord_botConnection_Category:SetExpanded( false )
discord_botConnection_Category:SetLabel( "Bot Connection (Don't Open On Stream!)" )

local discord_botConnection_List = vgui.Create( "DPanelList", discord_botConnection_Category )
discord_botConnection_List:SetPos( 10, 25 )
discord_botConnection_List:SetSize( 393, 50 )
discord_botConnection_List:SetSize( 393, 90 )
discord_botConnection_List:EnableVerticalScrollbar( false )
discord_botConnection_List:SetSpacing( 5 )

Expand Down Expand Up @@ -263,6 +263,49 @@ discord_botConnection_List.AddItem(xlib.maketextbox{
repconvar="rep_discord_api_key",
parent=discord_botConnection_List
})
local discord_botConnection_testButton_result = xlib.makelabel{
x=285, y=50,
w=20, h=20,
label="",
parent=discord_botConnection_List
}
discord_botConnection_List.AddItem(discord_botConnection_testButton_result)
local discord_botConnection_testButton = xlib.makebutton{
x=303, y=50,
w=90,
label="Test Connection",
parent=discord_botConnection_List
}
discord_botConnection_testButton.DoClick = function()
discord_botConnection_testButton__ExecuteTest()
end
discord_botConnection_List.AddItem(discord_botConnection_testButton)

timer.Create("clearTestResponse", 3.0, 0, function()
discord_botConnection_testButton_result:SetText("")
end)

net.Receive("discordTestConnection", function()
local len = net.ReadUInt(32)
local compressedConnectionTestResponse = net.ReadData(len)
local connectionTestResponseJSON = util.Decompress(compressedConnectionTestResponse)
local connectionTestResponseTable = util.JSONToTable(connectionTestResponseJSON)

timer.Stop("clearTestResponse")
timer.Start("clearTestResponse")

if (connectionTestResponseTable['success']) then
discord_botConnection_testButton_result:SetTextColor( Color( 0, 200, 0) )
discord_botConnection_testButton_result:SetText("")
else
discord_botConnection_testButton_result:SetTextColor( Color( 255, 0, 0) )
discord_botConnection_testButton_result:SetText("")
end
end)
function discord_botConnection_testButton__ExecuteTest()
net.Start("request_discordTestConnection")
net.SendToServer()
end

xgui.hookEvent( "onProcessModules", nil, discord_settings_panel.processModules )
xgui.addSubModule( "Settings", discord_settings_panel, nil, "discord" )
Expand All @@ -274,7 +317,7 @@ local discord_playerConnections_panel = xlib.makelistlayout{ w=415, h=318, paren
local steamIDToDiscordIDConnectionTable = {}

--Player Connection
local discord_playerConnections_table_Category = vgui.Create( "DCollapsibleCategory", discord_playerConnections_panel )
local discord_playerConnections_table_Category = vgui.Create( "DCollapsibleCategory", discord_playerConnections_panel )
discord_playerConnections_table_Category:SetSize( 393, 45 )
discord_playerConnections_table_Category:SetExpanded( true )
discord_playerConnections_table_Category:SetLabel( "Player Connections" )
Expand Down

0 comments on commit 75ae71e

Please sign in to comment.