Skip to content

Instantly share code, notes, and snippets.

@BadgerCode
Last active August 6, 2024 20:35
Show Gist options
  • Save BadgerCode/a16b475a21a2233d699d092fde0497de to your computer and use it in GitHub Desktop.
Save BadgerCode/a16b475a21a2233d699d092fde0497de to your computer and use it in GitHub Desktop.
Checks if a user is using a family-shared copy of Garry's Mod from an account which is banned and then kicks them
-- This goes in:
-- garrysmod/garrysmod/lua/autorun/account-sharing.lua
-- Or, if you want to keep your custom code separate from the default GMod server files:
-- garrysmod/garrysmod/addons/accountsharing/lua/autorun/account-sharing.lua
-- You will need to make some of these folders
-- Original post http://facepunch.com/showthread.php?t=1341204&p=43469693&viewfull=1#post43469693
-- Credit to McSimp
local APIKey = "APIKEYGOESHERE" -- See http://steamcommunity.com/dev/apikey
local function HandleSharedPlayer(ply, lenderSteamID)
local chatMessage = string.format("FamilySharing: %s (%s) has been lent Garry's Mod by %s", ply:Nick(), ply:SteamID(), lenderSteamID)
print(chatMessage)
if not (ULib and ULib.bans) then return end
local playerIsBanned = ULib.bans[lenderSteamID]
if playerIsBanned then
local banMessage = string.format("%s (%s) has been lent Garry's Mod by a banned account- %s", ply:Nick(), ply:SteamID(), lenderSteamID)
for i, ply in ipairs(player.GetHumans()) do
ply:ChatPrint(banMessage)
end
ply:Kick("The account that lent you Garry's Mod is banned on this server")
end
end
local function CheckFamilySharing(ply)
http.Fetch(
string.format("http://api.steampowered.com/IPlayerService/IsPlayingSharedGame/v0001/?key=%s&format=json&steamid=%s&appid_playing=4000",
APIKey,
ply:SteamID64()
),
function(rawJSONBody, len, headers, status)
body = util.JSONToTable(rawJSONBody)
if status ~= 200 or not body or not body.response then
error(string.format("FamilySharing: Invalid Steam API response (%s) for %s | %s: \nJSON: \"%s\"\n", status, ply:Nick(), ply:SteamID(), rawJSONBody))
end
local lender = body.response.lender_steamid or nil
if lender and lender ~= '' and lender ~= "0" then
HandleSharedPlayer(ply, util.SteamIDFrom64(lender))
end
end,
function(code)
error(string.format("FamilySharing: Failed API call for %s | %s (Error: %s)\n", ply:Nick(), ply:SteamID(), code))
end
)
end
hook.Remove("PlayerAuthed", "CheckFamilySharing")
hook.Add("PlayerAuthed", "CheckFamilySharing", CheckFamilySharing)
@isaac309
Copy link

isaac309 commented Apr 25, 2022

APIKEY works but get this error in the console?

[sahaddon] addons/sahaddon/lua/sv_bans.lua:103: FamilySharing: Invalid Steam API response for |

  1. error - [C]:-1
  2. onsuccess - addons/sahaddon/lua/sv_bans.lua:103
    3. unknown - lua/includes/modules/http.lua:69

SVBANS: safecrackSVBANS: lockpickSVBANS: caSVBANS: hacking[Player1|2|STEAM_0:1:91111823] Lua Error:

[ERROR] gamemodes/darkrp/gamemode/cl_options.lua:137: bad argument #1 to 'pairs' (table expected, got nil)

  1. pairs - [C]:-1
  2. LoadOptions - gamemodes/darkrp/gamemode/cl_options.lua:137
    3. CreateOptionsMenuPanel - gamemodes/darkrp/gamemode/cl_mainmenu.lua:1208
    4. ShowMainMenu - gamemodes/darkrp/gamemode/cl_mainmenu.lua:2729
    5. unknown - gamemodes/darkrp/gamemode/cl_mainmenu.lua:2822
    6. unknown - lua/includes/modules/concommand.lua:54

@BadgerCode
Copy link
Author

Hey @isaac309
On line 10, you need to replace APIKEYGOESHERE with your Steam API key

You can get a Steam API key from here- http://steamcommunity.com/dev/apikey

@isaac309
Copy link

Yes I added the apikey to local and still a [sahaddon] addons/sahaddon/lua/sv_bans.lua:103: FamilySharing: Invalid Steam API response for |

error - [C]:-1
onsuccess - addons/sahaddon/lua/sv_bans.lua:103
3. unknown - lua/includes/modules/http.lua:69
SVBANS: safecrackSVBANS: lockpickSVBANS: caSVBANS: hacking[Player1|2|STEAM_0:1:91111823] Lua Error:

[ERROR] gamemodes/darkrp/gamemode/cl_options.lua:137: bad argument #1 to 'pairs' (table expected, got nil)

pairs - [C]:-1
LoadOptions - gamemodes/darkrp/gamemode/cl_options.lua:137
3. CreateOptionsMenuPanel - gamemodes/darkrp/gamemode/cl_mainmenu.lua:1208
4. ShowMainMenu - gamemodes/darkrp/gamemode/cl_mainmenu.lua:2729
5. unknown - gamemodes/darkrp/gamemode/cl_mainmenu.lua:2822
6. unknown - lua/includes/modules/concommand.lua:54

@BadgerCode
Copy link
Author

I've updated the code to have some better logging and error handling.
Could you update your copy of the code @isaac309 ?

@isaac309
Copy link

I've updated the code to have some better logging and error handling. Could you update your copy of the code @isaac309 ?

Yes! The new code was a success erased the error. Thought it maybe could've been the firewall off the localhost, but it cleared right up! Thank you!

@BadgerCode
Copy link
Author

BadgerCode commented Aug 6, 2024

This no longer works as Valve have removed the API endpoint
https://developer.valvesoftware.com/w/index.php?title=Steam_Web_API&type=revision&diff=238804&oldid=230181

IsPlayingSharedGame

This could be re-written to use the newer, in-game method to retrieve this information
https://wiki.facepunch.com/gmod/Player:OwnerSteamID64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment