go-steam

repository·master·Indexed 19 days ago

https://github.com/philipp15b/go-steam

A Go implementation of the Steam protocol based on SteamKit2, enabling automation of Steam actions such as trading, chatting, and inventory management without the official client. It includes specialized sub-packages for bot utilities (gsbot), trading, trade offers, economy/inventory, and Team Fortress 2 operations. The library provides comprehensive tools for session authentication, SteamGuard 2FA, and connection management via the Steam network.

Tokens
12.2K
Snippets
52
Records
75
Agent score
65%

What's inside go-steam

  1. Overview of go-steam features

    master

    The go-steam library implements the Steam protocol to allow automation without running a Steam client. It is based on the .NET library SteamKit2.

    Key features include:

    • Trading: Trade offers, inventories, and notifications.
    • Social: Friend and group management, and chatting with friends.
    • Persona: Managing persona states (e.g., online, offline, looking to trade).
    • Security: SteamGuard with two-factor authentication.
    • TF2: Team Fortress 2 item crafting, moving, naming, and deleting.

    For official Steam Web API type wrappers, use the go-steamapi package.

  2. Best practices for working with go-steam

    master

    When developing with go-steam, keep the following in mind:

    • Verify with Steam Client: If an operation fails, check if it works under the same conditions using the official Steam client. This helps determine if the issue is a go-steam implementation detail or a Steam-side restriction (e.g., the 7-day trade restriction for newly authorized devices).
    • Expect Breaking Changes: Because Steam does not maintain a public API for many of these features, implementations (especially in trade and tradeoffer) may break unexpectedly.
    • Debugging: Steam's internals are complex. When reporting issues, provide as much precise information as possible to assist in debugging.
  3. Install dependencies for the generator

    master

    To use the generator to create Go code from SteamKit protocol descriptors, you must satisfy the following dependencies:

    1. SteamKit submodule: Ensure the submodule is initialized and updated.
    2. protoc: The protocol buffer compiler must be installed on your system.
    3. protoc-gen-go: The Go plugin for the protocol buffer compiler.
    4. ** ext{.NET Core SDK}**: Version 3.1 or later must be installed.
    git submodule update --init --recursive
    
    # Verify protoc installation
    protoc --version
    
    # Install protoc-gen-go
    go get google.golang.org/protobuf/cmd/protoc-gen-go
    
    # Verify protoc-gen-go installation
    protoc-gen-go --version
  4. Updating go-steam to a new SteamKit version

    master

    The Go source code is generated using tools located in the generator directory. To update to a new SteamKit version:

    1. Use the generator to create new Go source files (refer to generator/README.md for specific instructions).
    2. Update the go-steam codebase as necessary to accommodate the new generated files.
  5. Execute the generator

    master

    Run the generator using go run generator.go with specific commands to manage the build lifecycle. The clean proto steamlang sequence performs the following in order:

    1. clean: Removes existing build files.
    2. proto: Builds the protocol buffer files.
    3. steamlang: Builds the steamlang files.
    go run generator.go clean proto steamlang
  6. Handle Steam authentication events

    master

    The Auth object processes incoming Steam packets and emits events. To manage the authentication lifecycle, you should listen for the following events emitted by the Auth instance:

    • LoggedOnEvent: Emitted when a login is successful. Contains WebApiUserNonce, AccountFlags, and heartbeat intervals.
    • LogOnFailedEvent: Emitted when a login attempt fails, containing the Result error code.
    • LoginKeyEvent: Emitted when a new LoginKey is provided by Steam. Use the LoginKey field for future logins.
    • MachineAuthUpdateEvent: Emitted when Steam provides a new machine authentication hash. Store this hash to simplify future logins.
    • LoggedOffEvent: Emitted when the session is terminated.
    • AccountInfoEvent: Emitted with details about the account, such as PersonaName and Country.
  7. Use retry methods for network resilience

    master

    The tradeoffer package provides WithRetry variants for almost all major operations (e.g., AcceptWithRetry, CreateWithRetry, GetOfferWithRetry).

    These methods are useful for handling transient network errors. They take two additional parameters:

    • retryCount: The number of times to attempt the operation if it fails.
    • retryDelay: A time.Duration to wait between attempts.

    Note: The retry logic will not retry if the error returned is a *SteamError (a logical error returned by Steam, such as an invalid token or malformed request), as retrying a logical error will not change the outcome.

    // Retry creating an offer up to 3 times with a 2-second delay between attempts
    offerId, err := client.CreateWithRetry(
        partnerID, 
        nil, 
        myItems, 
        theirItems, 
        nil, 
        "Retry message", 
        3, 
        2 * time.Second,
    )
  8. Access social data caches

    master

    The Social struct maintains internal caches of your social network, which are updated automatically via incoming Steam packets. You can access these via the following exported fields:

    • Friends *socialcache.FriendsList: A cache of your individual friends and their current persona states.
    • Groups *socialcache.GroupsList: A cache of your Steam groups/clans.
    • Chats *socialcache.ChatsList: A cache of your active chat rooms and their members.
  9. Handle chat room and invite events

    master

    The API provides several event types for managing chat interactions:

    • ChatEnterEvent: Fired when joining a chat. Contains ChatRoomId, ChatRoomType, and EnterResponse.
    • ChatInviteEvent: Fired when receiving a chat invite. Includes InvitedId, ChatRoomId, and ChatRoomName.
    • ChatMemberInfoEvent: Fired when information about a chat member is received. Uses StateChangeDetails to describe changes like users joining or leaving via steamlang.EChatMemberStateChange.
    • ChatActionResultEvent: Fired when a chat action (like sending a message or performing a room action) completes, providing the Action and the Result.
  10. Handle friend and profile lifecycle events

    master

    The following events handle specific social actions:

    • FriendAddedEvent: Fired when a friend is successfully added to your list. Contains the Result, SteamId, and PersonaName.
    • IgnoreFriendEvent: Fired when an attempt to ignore a friend is processed. Contains the Result.
    • ProfileInfoEvent: Fired in response to requesting profile information. Provides details like RealName, CityName, CountryName, and a Summary (bio).
  11. Use the Trading API to manage Steam trades

    master

    The Trading struct provides access to the Steam client's trading capabilities. Note that while this API bootstraps the trade process, the actual trade execution is handled by the Steam website, not the client itself.

    Key Workflows:

    • Initiating a trade: Use RequestTrade to propose a trade to a friend. You will receive a TradeResultEvent once the request is processed (either failed or accepted by the friend).
    • Responding to a proposal: When a friend proposes a trade to you, the client emits a TradeProposedEvent. You can respond to this using RespondRequest to either accept or decline.
    • Canceling a request: Use CancelRequest to retract a trade proposal you previously sent.
    // Example workflow concept
    // 1. Request a trade
    trading.RequestTrade(friendSteamID)
    
    // 2. Respond to an incoming proposal (triggered by TradeProposedEvent)
    trading.RespondRequest(requestId, true) // true to accept
    
    // 3. Cancel a pending request
    trading.CancelRequest(friendSteamID)