-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup and simpler command handlers (#241)
* 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
Showing
39 changed files
with
366 additions
and
1,245 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | ||
} | ||
} |
Oops, something went wrong.