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

Cleanup and simpler command handlers #241

Merged
merged 9 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 0 additions & 37 deletions TwitchLib.Client.Enums/ChatColorPresets.cs

This file was deleted.

19 changes: 0 additions & 19 deletions TwitchLib.Client.Enums/CommercialLength.cs

This file was deleted.

19 changes: 0 additions & 19 deletions TwitchLib.Client.Enums/StringEnum.cs

This file was deleted.

67 changes: 0 additions & 67 deletions TwitchLib.Client.Models/Builders/ChatCommandBuilder.cs

This file was deleted.

62 changes: 0 additions & 62 deletions TwitchLib.Client.Models/Builders/WhisperCommandBuilder.cs

This file was deleted.

52 changes: 0 additions & 52 deletions TwitchLib.Client.Models/ChatCommand.cs

This file was deleted.

101 changes: 101 additions & 0 deletions TwitchLib.Client.Models/CommandInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
namespace TwitchLib.Client.Models;

/// <summary>Object representing a command received via Twitch chat.</summary>
public class CommandInfo
{
/// <summary>Property representing the command identifier (ie command prefix).</summary>
public char Identifier { get; }

/// <summary>Property representing the actual command (without the command prefix).</summary>
public string Name { get; }

/// <summary>Property representing all arguments received in a string form.</summary>
public string ArgumentsAsString { get; }

/// <summary>Property representing all arguments received in a List form.</summary>
public List<string> ArgumentsAsList { get; }

/// <summary>
/// Initializes a new instance of the <see cref="CommandInfo"/> class.
/// </summary>
public CommandInfo(char identifier, string name) : this(identifier, name, string.Empty, new())
{ }

/// <summary>
/// Initializes a new instance of the <see cref="CommandInfo"/> class.
/// </summary>
public CommandInfo(char identifier, string name, string argumentsAsString, List<string> argumentsAsList)
{
Identifier = identifier;
Name = name;
ArgumentsAsString = argumentsAsString;
ArgumentsAsList = argumentsAsList;
}

public static bool TryParse(ReadOnlySpan<char> s, out CommandInfo result)
{
result = default;
s = s.Trim();
if (s.IsEmpty)
return false;
var commandIdentifier = s[0];
s = s.Slice(1);
if (s.IsEmpty || s[0] == ' ') // if string contains only the identifier or the first char after identifier is space, then it is invalid input
return false;
var indexOfSpace = s.IndexOf(' ');
if (indexOfSpace == -1)
{
var name = s.ToString();
result = new(commandIdentifier, name);
}
else
{
var name = s.Slice(0, indexOfSpace).ToString();
s = s.Slice(indexOfSpace + 1).TrimStart();
var argumentsAsString = s.ToString();
result = new(commandIdentifier, name, argumentsAsString, ParseArgumentsToList(s));
}
return true;

static List<string> ParseArgumentsToList(ReadOnlySpan<char> s)
{
int index;
var arguments = new List<string>();
while (!s.IsEmpty)
{
bool isQuote = s[0] == '"';
if (s[0] == '"')
{
s = s.Slice(1);
index = s.IndexOf('"');
}
else
{
index = s.IndexOfAny('"', ' ');
}
if (index == -1)
{
arguments.Add(s.ToString());
s = default;
}
else
{
arguments.Add(s.Slice(0, index).ToString());
if (!isQuote && s[index] == '"') // s"txt" we dont want remove quote after s
index--;
s = s.Slice(index + 1);
}
s = s.TrimStart();
}
return arguments;
}
}

/// <inheritdoc/>
public override string ToString()
{
return ArgumentsAsString.Length == 0
? $"{Identifier}{Name}"
: $"{Identifier}{Name} {ArgumentsAsString}";
}
}
Loading
Loading