diff --git a/Telegrator/TypesExtensions.cs b/Telegrator/TypesExtensions.cs index 8584f70..8ee6452 100644 --- a/Telegrator/TypesExtensions.cs +++ b/Telegrator/TypesExtensions.cs @@ -64,6 +64,40 @@ namespace Telegrator return true; } + /// + /// Checkes if sent contains command. Automatically cuts bot name from it + /// + /// + /// + /// + public static bool IsCommand(this Message message, out string? command, out string? args) + { + command = null; + if (message is not { Entities.Length: > 0, Text.Length: > 0 }) + return false; + + MessageEntity commandEntity = message.Entities[0]; + if (commandEntity.Type != MessageEntityType.BotCommand) + return false; + + if (commandEntity.Offset != 0) + return false; + + command = message.Text.Substring(1, commandEntity.Length - 1); + if (message.Text.Length > command.Length) + { + args = message.Text.Substring(command.Length); + } + + if (command.Contains('@')) + { + string[] split = command.Split('@'); + command = split[0]; + } + + return true; + } + /// /// Split message text into arguments, ignoring command instance. Splits by space character /// @@ -74,16 +108,16 @@ namespace Telegrator /// public static string[] SplitArgs(this Message message) { - if (!message.IsCommand(out _)) - throw new InvalidDataException("Message does not contain a command"); - - if (message is not { Text.Length: > 0 }) + if (message.Text is not { Length: > 0 } text) throw new ArgumentNullException(nameof(message), "Command text cannot be null or empty"); - if (!message.Text.Contains(' ')) - throw new MissingMemberException("Command dont contains arguments"); + if (!text.Contains(' ')) + throw new MissingMemberException("Command doesn't contains arguments"); - return message.Text.Split([' '], StringSplitOptions.RemoveEmptyEntries).Skip(1).ToArray(); + if (!message.IsCommand(out _, out string? argsStr)) + throw new InvalidDataException("Message does not contain a command"); + + return argsStr.Split([' '], StringSplitOptions.RemoveEmptyEntries); } /// @@ -94,17 +128,17 @@ namespace Telegrator /// public static bool TrySplitArgs(this Message message, out string[]? args) { - args = null; - if (!message.IsCommand(out _)) - return false; - if (message is not { Text.Length: > 0 }) return false; if (!message.Text.Contains(' ')) return false; - args = message.Text.Split([' '], StringSplitOptions.RemoveEmptyEntries).Skip(1).ToArray(); + args = null; + if (!message.IsCommand(out _, out string> argsStr)) + return false; + + args = argsStr.Split([' '], StringSplitOptions.RemoveEmptyEntries); return true; } }