Skip to content

Commit

Permalink
Add support for chatrooms and invites
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Dec 21, 2015
1 parent 5038527 commit 15f9eb4
Showing 1 changed file with 64 additions and 23 deletions.
87 changes: 64 additions & 23 deletions ArchiSteamFarm/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ internal Bot(string botName) {
CallbackManager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);

SteamFriends = SteamClient.GetHandler<SteamFriends>();
CallbackManager.Subscribe<SteamFriends.ChatInviteCallback>(OnChatInvite);
CallbackManager.Subscribe<SteamFriends.ChatMsgCallback>(OnChatMsg);
CallbackManager.Subscribe<SteamFriends.FriendsListCallback>(OnFriendsList);
CallbackManager.Subscribe<SteamFriends.FriendMsgCallback>(OnFriendMsg);
CallbackManager.Subscribe<SteamFriends.FriendMsgHistoryCallback>(OnFriendMsgHistory);
Expand Down Expand Up @@ -399,15 +401,19 @@ private void HandleCallbacks() {
}
}

private void SendMessageToUser(ulong steamID, string message) {
private void SendMessage(ulong steamID, string message) {
if (steamID == 0 || string.IsNullOrEmpty(message)) {
return;
}

SteamFriends.SendChatMessage(steamID, EChatEntryType.ChatMsg, message);
// TODO: I really need something better
if (steamID < 110300000000000000) {

This comment has been minimized.

Copy link
@Ne3tCode

Ne3tCode Dec 21, 2015

U mad. Take ur c00kies :3

This comment has been minimized.

Copy link
@JustArchi

JustArchi Dec 22, 2015

Author Member

I know about that one, but I don't want to cast whole ulong to SteamID just for that one property.

This comment has been minimized.

Copy link
@Ne3tCode

Ne3tCode Dec 22, 2015

But first argument of the function is SteamID. Is there no implicit cast?

This comment has been minimized.

Copy link
@JustArchi

JustArchi Dec 22, 2015

Author Member

No, first argument of the function is ulong.

SteamFriends.SendChatMessage(steamID, EChatEntryType.ChatMsg, message);
} else {
SteamFriends.SendChatRoomMessage(steamID, EChatEntryType.ChatMsg, message);
}
}


private void ResponseStatus(ulong steamID, string botName = null) {
if (steamID == 0) {
return;
Expand All @@ -419,15 +425,15 @@ private void ResponseStatus(ulong steamID, string botName = null) {
bot = this;
} else {
if (!Bots.TryGetValue(botName, out bot)) {
SendMessageToUser(steamID, "Couldn't find any bot named " + botName + "!");
SendMessage(steamID, "Couldn't find any bot named " + botName + "!");
return;
}
}

if (bot.CardsFarmer.CurrentGamesFarming.Count > 0) {
SendMessageToUser(steamID, "Bot " + bot.BotName + " is currently farming appIDs: " + string.Join(", ", bot.CardsFarmer.CurrentGamesFarming) + " and has a total of " + bot.CardsFarmer.GamesToFarm.Count + " games left to farm");
SendMessage(steamID, "Bot " + bot.BotName + " is currently farming appIDs: " + string.Join(", ", bot.CardsFarmer.CurrentGamesFarming) + " and has a total of " + bot.CardsFarmer.GamesToFarm.Count + " games left to farm");
}
SendMessageToUser(steamID, "Currently " + Bots.Count + " bots are running");
SendMessage(steamID, "Currently " + Bots.Count + " bots are running");
}

private void Response2FA(ulong steamID, string botName = null) {
Expand All @@ -441,18 +447,18 @@ private void Response2FA(ulong steamID, string botName = null) {
bot = this;
} else {
if (!Bots.TryGetValue(botName, out bot)) {
SendMessageToUser(steamID, "Couldn't find any bot named " + botName + "!");
SendMessage(steamID, "Couldn't find any bot named " + botName + "!");
return;
}
}

if (bot.SteamGuardAccount == null) {
SendMessageToUser(steamID, "That bot doesn't have ASF 2FA enabled!");
SendMessage(steamID, "That bot doesn't have ASF 2FA enabled!");
return;
}

long timeLeft = 30 - TimeAligner.GetSteamTime() % 30;
SendMessageToUser(steamID, "2FA Token: " + bot.SteamGuardAccount.GenerateSteamGuardCode() + " (expires in " + timeLeft + " seconds)");
SendMessage(steamID, "2FA Token: " + bot.SteamGuardAccount.GenerateSteamGuardCode() + " (expires in " + timeLeft + " seconds)");
}

private void Response2FAOff(ulong steamID, string botName = null) {
Expand All @@ -466,20 +472,20 @@ private void Response2FAOff(ulong steamID, string botName = null) {
bot = this;
} else {
if (!Bots.TryGetValue(botName, out bot)) {
SendMessageToUser(steamID, "Couldn't find any bot named " + botName + "!");
SendMessage(steamID, "Couldn't find any bot named " + botName + "!");
return;
}
}

if (bot.SteamGuardAccount == null) {
SendMessageToUser(steamID, "That bot doesn't have ASF 2FA enabled!");
SendMessage(steamID, "That bot doesn't have ASF 2FA enabled!");
return;
}

if (bot.DelinkMobileAuthenticator()) {
SendMessageToUser(steamID, "Done! Bot is no longer using ASF 2FA");
SendMessage(steamID, "Done! Bot is no longer using ASF 2FA");
} else {
SendMessageToUser(steamID, "Something went wrong!");
SendMessage(steamID, "Something went wrong!");
}
}

Expand All @@ -489,15 +495,15 @@ private void ResponseStart(ulong steamID, string botName) {
}

if (Bots.ContainsKey(botName)) {
SendMessageToUser(steamID, "That bot instance is already running!");
SendMessage(steamID, "That bot instance is already running!");
return;
}

new Bot(botName);
if (Bots.ContainsKey(botName)) {
SendMessageToUser(steamID, "Done!");
SendMessage(steamID, "Done!");
} else {
SendMessageToUser(steamID, "That bot instance failed to start, make sure that XML config exists and bot is active!");
SendMessage(steamID, "That bot instance failed to start, make sure that XML config exists and bot is active!");
}
}

Expand All @@ -507,14 +513,14 @@ private async Task ResponseStop(ulong steamID, string botName) {
}

if (!Bots.ContainsKey(botName)) {
SendMessageToUser(steamID, "That bot instance is already inactive!");
SendMessage(steamID, "That bot instance is already inactive!");
return;
}

if (await Shutdown(botName).ConfigureAwait(false)) {
SendMessageToUser(steamID, "Done!");
SendMessage(steamID, "Done!");
} else {
SendMessageToUser(steamID, "That bot instance failed to shutdown!");
SendMessage(steamID, "That bot instance failed to shutdown!");
}
}

Expand All @@ -540,9 +546,9 @@ private async Task HandleMessage(ulong steamID, string message) {
await ShutdownAllBots().ConfigureAwait(false);
break;
case "!farm":
SendMessageToUser(steamID, "Please wait...");
SendMessage(steamID, "Please wait...");
await CardsFarmer.StartFarming().ConfigureAwait(false);
SendMessageToUser(steamID, "Done!");
SendMessage(steamID, "Done!");
break;
case "!restart":
await Program.Restart().ConfigureAwait(false);
Expand Down Expand Up @@ -652,6 +658,36 @@ private async void OnDisconnected(SteamClient.DisconnectedCallback callback) {
SteamClient.Connect();
}

private void OnChatInvite(SteamFriends.ChatInviteCallback callback) {
if (callback == null) {
return;
}

ulong steamID = callback.PatronID;
if (steamID != SteamMasterID) {
return;
}

SteamFriends.JoinChat(callback.ChatRoomID);
}

private async void OnChatMsg(SteamFriends.ChatMsgCallback callback) {
if (callback == null) {
return;
}

if (callback.ChatMsgType != EChatEntryType.ChatMsg) {
return;
}

ulong steamID = callback.ChatterID;
if (steamID != SteamMasterID) {
return;
}

await HandleMessage(callback.ChatRoomID, callback.Message).ConfigureAwait(false);
}

private void OnFriendsList(SteamFriends.FriendsListCallback callback) {
if (callback == null) {
return;
Expand Down Expand Up @@ -685,7 +721,12 @@ private async void OnFriendMsg(SteamFriends.FriendMsgCallback callback) {
return;
}

await HandleMessage(callback.Sender, callback.Message).ConfigureAwait(false);
ulong steamID = callback.Sender;
if (steamID != SteamMasterID) {
return;
}

await HandleMessage(steamID, callback.Message).ConfigureAwait(false);
}

private async void OnFriendMsgHistory(SteamFriends.FriendMsgHistoryCallback callback) {
Expand Down Expand Up @@ -908,7 +949,7 @@ private async void OnPurchaseResponse(ArchiHandler.PurchaseResponseCallback call

var purchaseResult = callback.PurchaseResult;
var items = callback.Items;
SendMessageToUser(SteamMasterID, "Status: " + purchaseResult + " | Items: " + string.Join("", items));
SendMessage(SteamMasterID, "Status: " + purchaseResult + " | Items: " + string.Join("", items));

if (purchaseResult == ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK) {
await CardsFarmer.StartFarming().ConfigureAwait(false);
Expand Down

0 comments on commit 15f9eb4

Please sign in to comment.