Skip to main content

steam_client/services/
trading.rs

1//! Trading functionality for Steam client.
2//!
3//! This module provides trade request functionality. Note that trading through
4//! the Steam UI has been deprecated in favor of trade offers, but this still
5//! works between bots.
6
7use steamid::SteamID;
8
9use crate::{error::SteamError, SteamClient};
10
11/// Trade restrictions that may apply.
12#[derive(Debug, Clone, Default)]
13pub struct TradeRestrictions {
14    /// Days of Steam Guard required.
15    pub steamguard_required_days: u32,
16    /// Days of new device cooldown.
17    pub new_device_cooldown_days: u32,
18    /// Default password reset probation days.
19    pub default_password_reset_probation_days: u32,
20    /// Current password reset probation days.
21    pub password_reset_probation_days: u32,
22    /// Default email change probation days.
23    pub default_email_change_probation_days: u32,
24    /// Current email change probation days.
25    pub email_change_probation_days: u32,
26}
27
28impl SteamClient {
29    /// Send a trade request to another user.
30    ///
31    /// **Note**: Trading has been removed from the Steam UI in favor of trade
32    /// offers. This still works between bots however.
33    ///
34    /// # Arguments
35    /// * `steam_id` - The SteamID of the user to trade with
36    pub async fn trade(&mut self, steam_id: SteamID) -> Result<(), SteamError> {
37        if !self.is_logged_in() {
38            return Err(SteamError::NotLoggedOn);
39        }
40
41        let msg = steam_protos::CMsgTradingInitiateTradeRequest { other_steamid: Some(steam_id.steam_id64()), ..Default::default() };
42
43        self.send_message(steam_enums::EMsg::EconTrading_InitiateTradeRequest, &msg).await
44    }
45
46    /// Cancel an outstanding trade request we sent to another user.
47    ///
48    /// # Arguments
49    /// * `steam_id` - The SteamID of the user to cancel the trade with
50    pub async fn cancel_trade_request(&mut self, steam_id: SteamID) -> Result<(), SteamError> {
51        if !self.is_logged_in() {
52            return Err(SteamError::NotLoggedOn);
53        }
54
55        let msg = steam_protos::CMsgTradingCancelTradeRequest { other_steamid: Some(steam_id.steam_id64()) };
56
57        self.send_message(steam_enums::EMsg::EconTrading_CancelTradeRequest, &msg).await
58    }
59
60    /// Respond to a trade request.
61    ///
62    /// # Arguments
63    /// * `trade_request_id` - The trade request ID from the TradeRequest event
64    /// * `accept` - Whether to accept or decline the trade
65    pub async fn respond_to_trade(&mut self, trade_request_id: u32, accept: bool) -> Result<(), SteamError> {
66        if !self.is_logged_in() {
67            return Err(SteamError::NotLoggedOn);
68        }
69
70        let response = if accept { steam_enums::EEconTradeResponse::Accepted } else { steam_enums::EEconTradeResponse::Declined };
71
72        let msg = steam_protos::CMsgTradingInitiateTradeResponse { trade_request_id: Some(trade_request_id), response: Some(response as u32), ..Default::default() };
73
74        self.send_message(steam_enums::EMsg::EconTrading_InitiateTradeResponse, &msg).await
75    }
76}