Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Feature command rewrite #862

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 15 additions & 35 deletions ArchiSteamFarm/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1404,38 +1404,6 @@ private void HandleCallbacks() {
}
}

private async Task HandleMessage(ulong chatGroupID, ulong chatID, ulong steamID, string message) {
if ((chatGroupID == 0) || (chatID == 0) || (steamID == 0) || string.IsNullOrEmpty(message)) {
ArchiLogger.LogNullError(nameof(chatGroupID) + " || " + nameof(chatID) + " || " + nameof(steamID) + " || " + nameof(message));
return;
}

string response = await Response(steamID, message).ConfigureAwait(false);

// We respond with null when user is not authorized (and similar)
if (string.IsNullOrEmpty(response)) {
return;
}

await SendMessage(chatGroupID, chatID, response).ConfigureAwait(false);
}

private async Task HandleMessage(ulong steamID, string message) {
if ((steamID == 0) || string.IsNullOrEmpty(message)) {
ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(message));
return;
}

string response = await Response(steamID, message).ConfigureAwait(false);

// We respond with null when user is not authorized (and similar)
if (string.IsNullOrEmpty(response)) {
return;
}

await SendMessage(steamID, response).ConfigureAwait(false);
}

private async Task HeartBeat() {
if (!KeepRunning || !IsConnectedAndLoggedOn || (HeartBeatFailures == byte.MaxValue)) {
return;
Expand Down Expand Up @@ -1948,7 +1916,14 @@ private async Task OnIncomingChatMessage(CChatRoom_IncomingChatMessage_Notificat
}

ArchiLogger.LogChatMessage(false, message, notification.chat_group_id, notification.chat_id, notification.steamid_sender);
await HandleMessage(notification.chat_group_id, notification.chat_id, notification.steamid_sender, message).ConfigureAwait(false);

string response = await Commands.Parse(this, notification.steamid_sender, message).ConfigureAwait(false);
if(response == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space + use null or empty for strings (nearly always, unless it matters for you whether you have null or empty).

ArchiLogger.LogNullError(nameof(response));
return;
}

await SendMessage(notification.chat_group_id, notification.chat_id, response).ConfigureAwait(false);
}

private async Task OnIncomingMessage(CFriendMessages_IncomingMessage_Notification notification) {
Expand Down Expand Up @@ -1982,7 +1957,12 @@ private async Task OnIncomingMessage(CFriendMessages_IncomingMessage_Notificatio
return;
}

await HandleMessage(notification.steamid_friend, message).ConfigureAwait(false);
string response = await Commands.Parse(this, notification.steamid_friend, message).ConfigureAwait(false);
if(response == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. And you missed return too, so it'll crash instead 😉.

ArchiLogger.LogNullError(nameof(response));
Copy link
Member

@JustArchi JustArchi Jul 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not blindly copy paste, LogNullError() should be used ONLY for UNEXPECTED errors, null response is entirely expected when the user doesn't have permission to interact with the bot and therefore generates no response.

}

await SendMessage(notification.steamid_friend, response).ConfigureAwait(false);
}

private async void OnLicenseList(SteamApps.LicenseListCallback callback) {
Expand Down Expand Up @@ -5220,4 +5200,4 @@ private enum ERedeemFlags : byte {
SkipKeepMissingGames = 128
}
}
}
}
51 changes: 51 additions & 0 deletions ArchiSteamFarm/Commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ArchiSteamFarm {
internal sealed class Commands
{
private static readonly Dictionary<(string Command, byte ArgumentCount), Func<Bot, ulong, string[], Task<string>>> CommandDictionary = new Dictionary<(string Command, byte arguments), Func<Bot, ulong, string[], Task<string>>>() {
Copy link
Member

@JustArchi JustArchi Jul 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong on so many levels. Why do you even put args number there, didn't I just tell you to make static version for each command and in that static version do any kind of arguments parsing? Why do you put lambdas here instead of JUST a pointer to a function that you will await/execute in Parse() function? You didn't just kill old working functionality, but you did it for absolutely no reason - this is unacceptable.


};

internal static async Task<string> Parse(Bot bot, ulong steamID, string message) {
if(bot == null || steamID == 0 || string.IsNullOrEmpty(message)) {
ASF.ArchiLogger.LogNullError(nameof(bot) + " || " + nameof(steamID) + " || " + nameof(message));
return null;
}

if (!string.IsNullOrEmpty(Program.GlobalConfig.CommandPrefix)) {
if(!message.StartsWith(Program.GlobalConfig.CommandPrefix, StringComparison.Ordinal)) {
return null;
}

message = message.Substring(Program.GlobalConfig.CommandPrefix.Length);
}

string[] messageParts = message.Split((char[]) null, StringSplitOptions.RemoveEmptyEntries);

if(messageParts.Length == 0) {
ASF.ArchiLogger.LogNullError(nameof(messageParts));
return null;
}

string command = messageParts[0];
string[] arguments = new string[messageParts.Length - 1];
Array.Copy(messageParts, 1, arguments, 0, arguments.Length);

if (CommandDictionary.TryGetValue((command.ToUpperInvariant(), (byte) arguments.Length), out Func<Bot, ulong, string[], Task<string>> func)) {
string response = await func(bot, steamID, arguments).ConfigureAwait(false);
if(response == null) {
ASF.ArchiLogger.LogNullError(nameof(response));
return null;
}

return response;
}

ASF.ArchiLogger.LogNullError(nameof(func));
return null;
}
}
}
4 changes: 2 additions & 2 deletions ArchiSteamFarm/IPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ private static async Task<bool> HandleApiCommandPost(HttpListenerRequest request
argument = Program.GlobalConfig.CommandPrefix + argument;
}

string content = await targetBot.Response(Program.GlobalConfig.SteamOwnerID, argument).ConfigureAwait(false);
string content = await Commands.Parse(targetBot, Program.GlobalConfig.SteamOwnerID, argument).ConfigureAwait(false);

await ResponseJsonObject(request, response, new GenericResponse<string>(true, "OK", content)).ConfigureAwait(false);
return true;
Expand Down Expand Up @@ -1283,4 +1283,4 @@ internal TypeProperties(string baseType = null, HashSet<string> customAttributes
}
}
}
}
}