C# SteamBot - 获取交易报价状态
C# SteamBot - Get trade offer state
我在使用此代码时遇到错误:
var offer = Bot.NewTradeOffer(dbSteamId);
offer.Items.AddMyItem(730, 2, 2611100148);
if (offer.Items.NewVersion)
{
string newOfferId;
if (offer.SendWithToken(out newOfferId, dbToken, dbMessage))
{
Log.Success("Trade offer sent : Offer ID " + newOfferId);
Bot.TryGetTradeOffer(newOfferId, out offer);
}
}
它因错误而崩溃:
ERROR: System.NullReferenceException: Object reference not set to an instance of an object.
at SteamTrade.TradeOffer.TradeOfferManager.IsOfferValid(Offer offer) in C:\Users\Robertas\Desktop\SteamBot-master\SteamTrade\TradeOffer\TradeOfferManager.cs:line 130
at SteamTrade.TradeOffer.TradeOfferManager.GetOffer(String offerId, TradeOffer& tradeOffer) in C:\Users\Robertas\Desktop\SteamBot-master\SteamTrade\TradeOffer\TradeOfferManager.cs:line 162
at SteamBot.Bot.TryGetTradeOffer(String offerId, TradeOffer& tradeOffer) in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\Bot.cs:line 331
at SteamBot.TradeOfferUserHandler.SendTradeOffer() in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\TradeOfferUserHandler.cs:line 81
at SteamBot.TradeOfferUserHandler.OnLoginCompleted() in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\TradeOfferUserHandler.cs:line 64
at SteamBot.Bot.<HandleSteamMessage>b__5(LoginKeyCallback callback) in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\Bot.cs:line 476
at SteamKit2.CallbackMsgExtensions.Handle[T](ICallbackMsg msg, Action`1 handler)
at SteamBot.Bot.HandleSteamMessage(ICallbackMsg msg) in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\Bot.cs:line 458
at SteamBot.Bot.BackgroundWorkerOnDoWork(Object sender, DoWorkEventArgs doWorkEventArgs) in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\Bot.cs:line 941
有什么想法吗?也许你知道检查交易报价状态的不同方法?请分享您的想法。谢谢!
已编辑:
public bool GetOffer(string offerId, out TradeOffer tradeOffer)
{
tradeOffer = null;
var resp = webApi.GetTradeOffer(offerId);
if (resp != null)
{
if (IsOfferValid(resp.Offer))
{
tradeOffer = new TradeOffer(session, resp.Offer);
return true;
}
else
{
//todo: log steam api is giving us invalid offers.
Console.WriteLine("Offer returned from steam api is not valid : " + resp.Offer.TradeOfferId);
}
}
return false;
}
resp.Offer 为空。怎么可能为空?我可以提到当机器人发送到我的帐户时一切正常,但是当它发送到其他帐户时,它崩溃了。
已编辑#2:
好的,我发现了错误。得到回应是问题。当我调用这个(http://api.steampowered.com/IEconService/GetTradeOffer/v1/?key=48B9FCE3FE8A74ADDA29174BFD47441B&tradeofferid=530787948&language=en_us)时:
{
"response": {
}
}
{
"response": {
"offer": {
"tradeofferid": "530796219",
"accountid_other": 213128749,
"message": "Security Code: HqaMyUDN",
"expiration_time": 1435675048,
"trade_offer_state": 2,
"items_to_receive": [
{
"appid": "730",
"contextid": "2",
"assetid": "2275856684",
"classid": "310776767",
"instanceid": "0",
"amount": "1",
"missing": false
}
]
,
"is_our_offer": false,
"time_created": 1434465448,
"time_updated": 1434465448,
"from_real_time_trade": false
},...
知道为什么一个响应是空的,而另一个是正常的吗?
看来您正在使用 SteamBot library. Since its source code is readily available, take a look at the source code for the GetTradeOffer method, found here: TradeOfferWebApi source
很明显,从此方法接收到非空响应并不能保证它包含有效的报价对象。您的支票
resp.Offer,因此,应该首先验证它不为空:
if (resp != null)
{
if (resp.Offer != null && IsOfferValid(resp.Offer))
至于确定为什么它不为空:将源代码抓取到 SteamBot,运行 您的应用程序 + 调试器中的 SteamBot 项目,然后进入方法.根据 GetTradeOffer 方法的源代码,它很可能会引发异常。
考虑到代码中 'todo' 条评论的数量,该库似乎仍处于开发中,因此 运行 SteamBot 源代码可能有助于调试目的(您可以轻松修改源代码以实际将异常记录到对您有用的目的地)。
编辑(响应 OP 的编辑 #2):从文档到 Steam's IEconService API,一个帐户只能检索关于该帐户的优惠信息。可能的解释是,要么您传递了无效的优惠 ID(您可能已经排除了),要么您的应用所在的帐户 运行ning 无权查看此优惠。
我在使用此代码时遇到错误:
var offer = Bot.NewTradeOffer(dbSteamId);
offer.Items.AddMyItem(730, 2, 2611100148);
if (offer.Items.NewVersion)
{
string newOfferId;
if (offer.SendWithToken(out newOfferId, dbToken, dbMessage))
{
Log.Success("Trade offer sent : Offer ID " + newOfferId);
Bot.TryGetTradeOffer(newOfferId, out offer);
}
}
它因错误而崩溃:
ERROR: System.NullReferenceException: Object reference not set to an instance of an object.
at SteamTrade.TradeOffer.TradeOfferManager.IsOfferValid(Offer offer) in C:\Users\Robertas\Desktop\SteamBot-master\SteamTrade\TradeOffer\TradeOfferManager.cs:line 130
at SteamTrade.TradeOffer.TradeOfferManager.GetOffer(String offerId, TradeOffer& tradeOffer) in C:\Users\Robertas\Desktop\SteamBot-master\SteamTrade\TradeOffer\TradeOfferManager.cs:line 162
at SteamBot.Bot.TryGetTradeOffer(String offerId, TradeOffer& tradeOffer) in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\Bot.cs:line 331
at SteamBot.TradeOfferUserHandler.SendTradeOffer() in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\TradeOfferUserHandler.cs:line 81
at SteamBot.TradeOfferUserHandler.OnLoginCompleted() in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\TradeOfferUserHandler.cs:line 64
at SteamBot.Bot.<HandleSteamMessage>b__5(LoginKeyCallback callback) in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\Bot.cs:line 476
at SteamKit2.CallbackMsgExtensions.Handle[T](ICallbackMsg msg, Action`1 handler)
at SteamBot.Bot.HandleSteamMessage(ICallbackMsg msg) in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\Bot.cs:line 458
at SteamBot.Bot.BackgroundWorkerOnDoWork(Object sender, DoWorkEventArgs doWorkEventArgs) in C:\Users\Robertas\Desktop\SteamBot-master\SteamBot\Bot.cs:line 941
有什么想法吗?也许你知道检查交易报价状态的不同方法?请分享您的想法。谢谢!
已编辑:
public bool GetOffer(string offerId, out TradeOffer tradeOffer)
{
tradeOffer = null;
var resp = webApi.GetTradeOffer(offerId);
if (resp != null)
{
if (IsOfferValid(resp.Offer))
{
tradeOffer = new TradeOffer(session, resp.Offer);
return true;
}
else
{
//todo: log steam api is giving us invalid offers.
Console.WriteLine("Offer returned from steam api is not valid : " + resp.Offer.TradeOfferId);
}
}
return false;
}
resp.Offer 为空。怎么可能为空?我可以提到当机器人发送到我的帐户时一切正常,但是当它发送到其他帐户时,它崩溃了。
已编辑#2:
好的,我发现了错误。得到回应是问题。当我调用这个(http://api.steampowered.com/IEconService/GetTradeOffer/v1/?key=48B9FCE3FE8A74ADDA29174BFD47441B&tradeofferid=530787948&language=en_us)时:
{
"response": {
}
}
{
"response": {
"offer": {
"tradeofferid": "530796219",
"accountid_other": 213128749,
"message": "Security Code: HqaMyUDN",
"expiration_time": 1435675048,
"trade_offer_state": 2,
"items_to_receive": [
{
"appid": "730",
"contextid": "2",
"assetid": "2275856684",
"classid": "310776767",
"instanceid": "0",
"amount": "1",
"missing": false
}
]
,
"is_our_offer": false,
"time_created": 1434465448,
"time_updated": 1434465448,
"from_real_time_trade": false
},...
知道为什么一个响应是空的,而另一个是正常的吗?
看来您正在使用 SteamBot library. Since its source code is readily available, take a look at the source code for the GetTradeOffer method, found here: TradeOfferWebApi source
很明显,从此方法接收到非空响应并不能保证它包含有效的报价对象。您的支票 resp.Offer,因此,应该首先验证它不为空:
if (resp != null)
{
if (resp.Offer != null && IsOfferValid(resp.Offer))
至于确定为什么它不为空:将源代码抓取到 SteamBot,运行 您的应用程序 + 调试器中的 SteamBot 项目,然后进入方法.根据 GetTradeOffer 方法的源代码,它很可能会引发异常。
考虑到代码中 'todo' 条评论的数量,该库似乎仍处于开发中,因此 运行 SteamBot 源代码可能有助于调试目的(您可以轻松修改源代码以实际将异常记录到对您有用的目的地)。
编辑(响应 OP 的编辑 #2):从文档到 Steam's IEconService API,一个帐户只能检索关于该帐户的优惠信息。可能的解释是,要么您传递了无效的优惠 ID(您可能已经排除了),要么您的应用所在的帐户 运行ning 无权查看此优惠。