Skip to content

Commit

Permalink
Cleaned up docs on SlashCommandHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
hburn7 committed Apr 15, 2023
1 parent 201f099 commit 613c860
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions BanchoSharp/Messaging/SlashCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,66 @@ namespace BanchoSharp.Messaging;

public class SlashCommandHandler : ISlashCommandHandler
{
private readonly string _prompt;
private readonly string[] _splits;
private readonly string _prompt;
private readonly string[] _splits;

/// <summary>
/// A <see cref="ISlashCommandHandler" /> is responsible for parsing
/// a user-provided slash command.
/// <example>
/// <code>
/// string prompt = "/join #osu with invalid args"
/// var handler = new SlashCommandHandler(prompt);
/// // handler.Command = "join"
/// // handler.RelevantParameters = { "#osu" }
/// // handler.Parameters = { "#osu", "with", "invalid", "args" }
/// </code>
/// </example>
/// </summary>
/// <param name="prompt">The input to process</param>
public SlashCommandHandler(string prompt)
{
_prompt = prompt;
_splits = _prompt.Split();
/// <summary>
/// A <see cref="ISlashCommandHandler"/> is responsible for parsing
/// a user-provided slash command.
/// <example>
/// <code>
/// string prompt = "/join #osu with invalid args"
/// var handler = new SlashCommandHandler(prompt);
/// // handler.Command = "join"
/// // handler.RelevantParameters = { "#osu" }
/// // handler.Parameters = { "#osu", "with", "invalid", "args" }
/// </code>
/// </example>
/// </summary>
/// <param name="prompt">The input to process</param>
public SlashCommandHandler(string prompt)
{
_prompt = prompt;
_splits = _prompt.Split();

Command = _prompt.Split('/')[1].Split()[0];
IsBanchoCommand = Command.ToUpper() is "JOIN" or "PART" or "ME" or "IGNORE" or "UNIGNORE" or "AWAY" or "QUERY";
Command = _prompt.Split('/')[1].Split()[0];
IsBanchoCommand = Command.ToUpper() is "JOIN" or "PART" or "ME" or "IGNORE" or "UNIGNORE" or "AWAY" or "QUERY";

if (_splits.Length > 1)
{
// Params provided
Parameters = GetParamsForCommand();
}
else
{
// No params provided
Parameters = null;
}
}
if (_splits.Length > 1)
{
// Params provided
Parameters = GetParamsForCommand();
}
else
{
// No params provided
Parameters = null;
}
}

public string? Command { get; }
public string[]? Parameters { get; }
public bool IsBanchoCommand { get; }
public string? Command { get; }
public string[]? Parameters { get; }
public bool IsBanchoCommand { get; }

private bool IsSlashCommand() => !string.IsNullOrWhiteSpace(_prompt) &&
_prompt.StartsWith("/") &&
_prompt.Length > 1;
private bool IsSlashCommand() => !string.IsNullOrWhiteSpace(_prompt) &&
_prompt.StartsWith("/") &&
_prompt.Length > 1;

private string[]? GetParamsForCommand() => Command.ToLower() switch
{
"join" => GetFirstArgOrDefault(),
"part" => GetFirstArgOrDefault(),
"me" => GetAllAsStringOrDefault(),
"ignore" => GetFirstArgOrDefault(),
"unignore" => GetFirstArgOrDefault(), // Not tested, I do not know if /unignore works. It's here just in case.
"away" => GetAllAsStringOrDefault(),
"query" => GetFirstArgOrDefault(),
_ => GetSpaceDelimitedArgsOrDefault()
};
private string[]? GetParamsForCommand() => Command.ToLower() switch
{
"join" => GetFirstArgOrDefault(),
"part" => GetFirstArgOrDefault(),
"me" => GetAllAsStringOrDefault(),
"ignore" => GetFirstArgOrDefault(),
"unignore" => GetFirstArgOrDefault(), // Not tested, I do not know if /unignore works. It's here just in case.
"away" => GetAllAsStringOrDefault(),
"query" => GetFirstArgOrDefault(),
_ => GetSpaceDelimitedArgsOrDefault()
};

private string[]? GetFirstArgOrDefault() => _splits.Length >= 2 ? new string[] { _splits[1] } : null;
private string[]? GetFirstArgOrDefault() => _splits.Length >= 2 ? new string[] { _splits[1] } : null;

// Combine into one string, return as a 1-parameter array containing the string.
private string[]? GetAllAsStringOrDefault() => _splits.Length >= 2 ? new string[] { string.Join(" ", _splits[1..]) } : null;
private string[]? GetSpaceDelimitedArgsOrDefault() => _splits.Any() ? _splits[1..] : null;
// Combine into one string, return as a 1-parameter array containing the string.
private string[]? GetAllAsStringOrDefault() => _splits.Length >= 2 ? new string[] { string.Join(" ", _splits[1..]) } : null;
private string[]? GetSpaceDelimitedArgsOrDefault() => _splits.Any() ? _splits[1..] : null;
}

0 comments on commit 613c860

Please sign in to comment.