Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework Get-SteamPlayerBan #78

Merged
merged 13 commits into from
Apr 20, 2024
69 changes: 43 additions & 26 deletions SteamPS/Public/API/Get-SteamPlayerBan.ps1
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
function Get-SteamPlayerBan {
<#
.SYNOPSIS
Returns Community, VAC, and Economy ban statuses for given players.
.SYNOPSIS
Fetches ban information for Steam players.

.DESCRIPTION
Returns Community, VAC, and Economy ban statuses for given players.
This cmdlet fetches ban information for Steam players. The information includes whether the players are banned from the Steam Community, have VAC bans, the number of VAC bans, days since the last ban, number of game bans, and economy ban status.

.PARAMETER SteamID64
Comma-delimited list of 64 bit Steam IDs to return player ban information for.

.PARAMETER OutputFormat
Format of the output. Options are json (default), xml or vdf.
Specifies one or more 64-bit Steam IDs for which to fetch ban information. Enter the Steam IDs as a comma-separated list.

.EXAMPLE
Get-SteamPlayerBan -SteamID64 76561197960435530, 76561197960434622
Get-SteamPlayerBan -SteamID64 76561197960435530,76561197960434622

This example fetches ban information for the players with the specified Steam IDs.

.INPUTS
Array of int64.
int64[]: Specifies an array of 64-bit integers representing Steam IDs.

.OUTPUTS
Returns a string that is either formatted as json, xml or vdf.
Returns objects with the following properties:

players: List of player ban objects for each 64 bit ID requested
- SteamId (string) The player's 64 bit ID.
- CommunityBanned (bool) Indicates whether or not the player is banned from Steam Community.
- VACBanned (bool) Indicates whether or not the player has VAC bans on record.
- NumberOfVACBans (int) Number of VAC bans on record.
- DaysSinceLastBan (int) Number of days since the last ban.
- NumberOfGameBans (int) Number of bans in games, this includes CS:GO Overwatch bans.
- EconomyBan (string) The player's ban status in the economy. If the player has no bans on record the string will be "none", if the player is on probation it will say "probation", etc.
- SteamID64: The player's 64-bit ID.
- CommunityBanned: A boolean indicating whether the player is banned from the Steam Community.
- VACBanned: A boolean indicating whether the player has VAC bans on record.
- NumberOfVACBans: The number of VAC bans on record.
- DaysSinceLastBan: The number of days since the last ban.
- NumberOfGameBans: The number of bans in games, including CS:GO Overwatch bans.
- EconomyBan: The player's ban status in the economy. If the player has no bans on record, the string will be "none". If the player is on probation, it will say "probation", etc.

.NOTES
Author: Frederik Hjorslev Nylander
Expand All @@ -42,22 +40,41 @@
[Parameter(Mandatory = $true,
HelpMessage = '64 bit Steam ID to return player bans for.',
ValueFromPipelineByPropertyName = $true)]
[int64[]]$SteamID64,

[Parameter(Mandatory = $false,
HelpMessage = 'Format of the output. Options are json (default), xml or vdf.')]
[ValidateSet('json', 'xml', 'vdf')]
[string]$OutputFormat = 'json'
[int64[]]$SteamID64
)

begin {
Write-Verbose -Message "[BEGIN ] Starting: $($MyInvocation.MyCommand)"
}

process {
$Request = Invoke-WebRequest -Uri "https://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?format=$OutputFormat&key=$(Get-SteamAPIKey)&steamids=$($SteamID64 -join ',')" -UseBasicParsing
$Request = Invoke-RestMethod -Uri 'https://api.steampowered.com/ISteamUser/GetPlayerBans/v1/' -UseBasicParsing -Body @{
key = Get-SteamAPIKey
steamids = ($SteamID64 -join ',')
}

Write-Output -InputObject $Request.Content
if (-not [string]::IsNullOrEmpty($Request.players.SteamId)) {
foreach ($Item in $Request.players) {
[PSCustomObject]@{
SteamID64 = [int64]$Item.SteamId
CommunityBanned = $Item.CommunityBanned
VACBanned = $Item.VACBanned
NumberOfVACBans = $Item.NumberOfVACBans
DaysSinceLastBan = $Item.DaysSinceLastBan
NumberOfGameBans = $Item.NumberOfGameBans
EconomyBan = $Item.EconomyBan
}
}
} elseif ([string]::IsNullOrEmpty($Request.players)) {
$Exception = [Exception]::new("SteamID $SteamID64 couldn't be found.")
$ErrorRecord = [System.Management.Automation.ErrorRecord]::new(
$Exception,
'PlayerNotFound',
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$Request
)
$PSCmdlet.WriteError($ErrorRecord)
}
} # Process

end {
Expand Down
51 changes: 51 additions & 0 deletions Tests/Unit/Public/Get-SteamPlayerBan.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Describe 'Get-SteamPlayerBan Tests' {
BeforeAll {
. $SteamPSModulePath\Private\API\Get-SteamAPIKey.ps1
Mock -CommandName Get-SteamAPIKey -ModuleName SteamPS -MockWith {
return $true
}
}

Context 'With valid Steam ID' {
BeforeAll {
Mock -CommandName Invoke-RestMethod -ModuleName SteamPS -MockWith {
return '{
"players": [
{
"SteamId": "76561197983367235",
"CommunityBanned": false,
"VACBanned": false,
"NumberOfVACBans": 0,
"DaysSinceLastBan": 0,
"NumberOfGameBans": 0,
"EconomyBan": "none"
}
]
}' | ConvertFrom-Json
}
}

It 'Should return ban information' {
$result = Get-SteamPlayerBan -SteamID64 76561197983367235
$result | Should -Not -BeNullOrEmpty
$result.SteamID64 | Should -BeExactly 76561197983367235
$result.CommunityBanned | Should -Be $false
$result.VACBanned | Should -Be $false
$result.NumberOfVACBans | Should -Be 0
$result.DaysSinceLastBan | Should -Be 0
$result.NumberOfGameBans | Should -Be 0
$result.EconomyBan | Should -Be 'none'
}
}

Context 'With invalid Steam ID' {
BeforeAll {
Mock -CommandName Invoke-RestMethod -ModuleName SteamPS -MockWith {
return '{"players":[{}]}' | ConvertFrom-Json
}
}
It 'Should throw an error' {
{ Get-SteamPlayerBan -SteamID64 12345 -ErrorAction Stop } | Should -Throw
}
}
}