-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 2 commits
a1ae78f
a23085a
4502cd0
7b7205e
aa9d0a4
6620397
e5daf9f
90272c5
52c0be6
6f69ada
063693b
7570918
b36bb10
2be92fc
eced08f
adcd8f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) { | ||
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) { | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. And you missed |
||
ArchiLogger.LogNullError(nameof(response)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not blindly copy paste, |
||
} | ||
|
||
await SendMessage(notification.steamid_friend, response).ConfigureAwait(false); | ||
} | ||
|
||
private async void OnLicenseList(SteamApps.LicenseListCallback callback) { | ||
|
@@ -5220,4 +5200,4 @@ private enum ERedeemFlags : byte { | |
SkipKeepMissingGames = 128 | ||
} | ||
} | ||
} | ||
} |
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>>>() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
}; | ||
|
||
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; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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).