using axXez.Utils;
using System;
using System.Linq;
using System.Reflection;
namespace axXez.TS3.Bot
{
/// A registered chat command, bound to a module method and ready to execute.
public class Command
{
private readonly CommandAttribute _attr;
private readonly Func _callback;
/// The command keyword without the leading !, e.g. "bot status".
public string Keyword => _attr.Keyword;
/// Short description of what the command does, shown in !help output.
public string Description => _attr.Description;
/// Minimum role required to invoke this command.
public CommandRole Role { get; set; }
/// Chat scopes this command responds to.
public CommandScope Scope { get; set; }
/// Parameter signature string, e.g. <name> [seconds]. Null if the command takes no parameters.
public string Parameters { get; private set; }
/// Full usage string, e.g. !keyword <name> [seconds].
public string Usage { get; private set; }
/// BBCode-formatted help line shown in !help output, e.g. [B]!keyword[/B] — description.
public string Help { get; private set; }
/// The module instance that owns this command.
public Module Module { get; private set; }
internal MethodInfo Method { get; private set; }
internal Command(CommandAttribute attr, MethodInfo method, Module module, Func callback, CommandLineParser parser = null)
{
_attr = attr;
Method = method;
Module = module;
_callback = callback;
Role = attr.Role;
Scope = attr.Scope;
if (!string.IsNullOrWhiteSpace(attr.Parameters))
Parameters = attr.Parameters;
else if (parser != null && parser.Parameters.Count > 0)
Parameters = string.Join(" ", parser.Parameters.Select(p => p.IsRequired ? $"<{p.Name}>" : $"[{p.Name}]"));
Usage = string.IsNullOrWhiteSpace(Parameters) ? $"!{Keyword}" : $"!{Keyword} {Parameters}";
Help = string.IsNullOrWhiteSpace(Description) ? $"[B]{Usage}[/B]" : $"[B]{Usage}[/B] — {Description}";
}
internal CommandResponse Execute(CommandContext context)
=> _callback(context);
///
public override string ToString() => Keyword;
///
public override int GetHashCode() => Keyword.GetHashCode();
///
public override bool Equals(object obj)
=> typeof(Command).IsAssignableFrom(obj.GetType()) && (obj as Command).Keyword.Equals(Keyword) && (obj as Command).Module.Equals(Module);
}
}