Skip to content

Commit

Permalink
Cleanup and simpler command handlers (#241)
Browse files Browse the repository at this point in the history
* replace all duplicate Notice events with one universal event

* delete sync methods wrapper around async

* Typo

* remove unused Enum

* <inheritdoc/>

* rewrite: command workflow

* old/new command handlers

* merge dev and fix error
  • Loading branch information
AoshiW authored Aug 5, 2023
1 parent 989bc8d commit c3b730b
Show file tree
Hide file tree
Showing 39 changed files with 366 additions and 1,245 deletions.
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

0 comments on commit c3b730b

Please sign in to comment.