Fixed SplitArgs method capturing bot name

This commit is contained in:
2026-03-06 14:49:52 +04:00
parent 5a3640bc9f
commit a948e5d15f
+46 -12
View File
@@ -64,6 +64,40 @@ namespace Telegrator
return true; return true;
} }
/// <summary>
/// Checkes if sent <see cref="Message"/> contains command. Automatically cuts bot name from it
/// </summary>
/// <param name="message"></param>
/// <param name="command"></param>
/// <returns></returns>
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;
}
/// <summary> /// <summary>
/// Split message text into arguments, ignoring command instance. Splits by space character /// Split message text into arguments, ignoring command instance. Splits by space character
/// </summary> /// </summary>
@@ -74,16 +108,16 @@ namespace Telegrator
/// <exception cref="MissingMemberException"></exception> /// <exception cref="MissingMemberException"></exception>
public static string[] SplitArgs(this Message message) public static string[] SplitArgs(this Message message)
{ {
if (!message.IsCommand(out _)) if (message.Text is not { Length: > 0 } text)
throw new InvalidDataException("Message does not contain a command");
if (message is not { Text.Length: > 0 })
throw new ArgumentNullException(nameof(message), "Command text cannot be null or empty"); throw new ArgumentNullException(nameof(message), "Command text cannot be null or empty");
if (!message.Text.Contains(' ')) if (!text.Contains(' '))
throw new MissingMemberException("Command dont contains arguments"); 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);
} }
/// <summary> /// <summary>
@@ -94,17 +128,17 @@ namespace Telegrator
/// <returns></returns> /// <returns></returns>
public static bool TrySplitArgs(this Message message, out string[]? args) 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 }) if (message is not { Text.Length: > 0 })
return false; return false;
if (!message.Text.Contains(' ')) if (!message.Text.Contains(' '))
return false; 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; return true;
} }
} }