I am making a trade offer bot in C# using SteamKit2, and most of the time it is successfully connecting to steam. But some of the time it just freezes when I have it output "Connecting to Steam..." right before client.connect();
is called. It happens often enough that it needs to be fixed, but I don't know what the problem is. Here is my code (a lot was taken from a SteamKit2 tutorial):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SteamKit2;
namespace ATO
{
class OfferSender
{
string username;
string password;
SteamClient client;
CallbackManager manager;
SteamUser user;
bool isRunning = false;
public OfferSender()
{
}
public void login()
{
Console.Write("Please enter your username: ");
username = Console.ReadLine();
Console.Write("Please enter your password: ");
password = Console.ReadLine();
client = new SteamClient();
manager = new CallbackManager(client);
user = client.GetHandler<SteamUser>();
new Callback<SteamClient.ConnectedCallback>(OnConnected, manager);
new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
isRunning = true;
Console.WriteLine("\nConnecting to Steam...\n");
client.Connect();
while(isRunning)
{
manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
}
Console.ReadKey();
}
public void OnConnected(SteamClient.ConnectedCallback callback)
{
if (callback.Result != EResult.OK)
{
Console.WriteLine("Error connecting to Steam: {0}", callback.Result);
isRunning = false;
return;
}
Console.WriteLine("Connected to steam.\nLogging in {0}...\n", username);
user.LogOn(new SteamUser.LogOnDetails {
Username = username,
Password = password
});
}
public void OnLoggedOn(SteamUser.LoggedOnCallback callback)
{
if (callback.Result == EResult.AccountLogonDenied)
{
Console.WriteLine("This account is SteamGuard protected.");
return;
}
if(callback.Result != EResult.OK)
{
Console.WriteLine("Unable to log in to steam {0}\n", callback.Result);
isRunning = false;
return;
}
Console.WriteLine("successfully logged in!");
Environment.Exit(0);
}
}
}
How do I fix this?
You need to handle some more callbacks.
Here is how do I log into steam with my bot.
SteamBot.cs
Program.cs