Split config into builder and immutable classes, added some audioservice extension methods.
This commit is contained in:
@@ -9,14 +9,12 @@ namespace Discord.Commands
|
||||
client.Services.Add(new CommandService(config));
|
||||
return client;
|
||||
}
|
||||
public static DiscordClient UsingCommands(this DiscordClient client, Action<CommandServiceConfig> configFunc = null)
|
||||
public static DiscordClient UsingCommands(this DiscordClient client, Action<CommandServiceConfigBuilder> configFunc = null)
|
||||
{
|
||||
var config = new CommandServiceConfig();
|
||||
configFunc(config);
|
||||
client.Services.Add(new CommandService(config));
|
||||
var builder = new CommandServiceConfigBuilder();
|
||||
configFunc(builder);
|
||||
client.Services.Add(new CommandService(builder));
|
||||
return client;
|
||||
}
|
||||
public static CommandService Commands(this DiscordClient client, bool required = true)
|
||||
=> client.Services.Get<CommandService>(required);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,18 @@ namespace Discord.Commands
|
||||
private void OnCommandError(CommandErrorType errorType, CommandEventArgs args, Exception ex = null)
|
||||
=> CommandErrored(this, new CommandErrorEventArgs(errorType, args, ex));
|
||||
|
||||
public CommandService()
|
||||
: this(new CommandServiceConfigBuilder())
|
||||
{
|
||||
}
|
||||
public CommandService(CommandServiceConfigBuilder builder)
|
||||
: this(builder.Build())
|
||||
{
|
||||
if (builder.ExecuteHandler != null)
|
||||
CommandExecuted += builder.ExecuteHandler;
|
||||
if (builder.ErrorHandler != null)
|
||||
CommandErrored += builder.ErrorHandler;
|
||||
}
|
||||
public CommandService(CommandServiceConfig config)
|
||||
{
|
||||
Config = config;
|
||||
@@ -42,9 +54,8 @@ namespace Discord.Commands
|
||||
void IService.Install(DiscordClient client)
|
||||
{
|
||||
Client = client;
|
||||
Config.Lock();
|
||||
|
||||
if (Config.HelpMode != HelpMode.Disable)
|
||||
if (Config.HelpMode != HelpMode.Disabled)
|
||||
{
|
||||
CreateCommand("help")
|
||||
.Parameter("command", ParameterType.Multiple)
|
||||
|
||||
@@ -2,36 +2,45 @@
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public class CommandServiceConfig
|
||||
public class CommandServiceConfigBuilder
|
||||
{
|
||||
/// <summary> Gets or sets the prefix character used to trigger commands, if ActivationMode has the Char flag set. </summary>
|
||||
public char? PrefixChar { get { return _prefixChar; } set { SetValue(ref _prefixChar, value); } }
|
||||
private char? _prefixChar = null;
|
||||
|
||||
public char? PrefixChar { get; set; } = null;
|
||||
/// <summary> Gets or sets whether a message beginning with a mention to the logged-in user should be treated as a command. </summary>
|
||||
public bool AllowMentionPrefix { get { return _allowMentionPrefix; } set { SetValue(ref _allowMentionPrefix, value); } }
|
||||
private bool _allowMentionPrefix = true;
|
||||
|
||||
public bool AllowMentionPrefix { get; set; } = true;
|
||||
/// <summary>
|
||||
/// Gets or sets a custom function used to detect messages that should be treated as commands.
|
||||
/// This function should a positive one indicating the index of where the in the message's RawText the command begins,
|
||||
/// and a negative value if the message should be ignored.
|
||||
/// </summary>
|
||||
public Func<Message, int> CustomPrefixHandler { get { return _customPrefixHandler; } set { SetValue(ref _customPrefixHandler, value); } }
|
||||
private Func<Message, int> _customPrefixHandler = null;
|
||||
public Func<Message, int> CustomPrefixHandler { get; set; } = null;
|
||||
|
||||
/// <summary> Gets or sets whether a help function should be automatically generated. </summary>
|
||||
public HelpMode HelpMode { get { return _helpMode; } set { SetValue(ref _helpMode, value); } }
|
||||
private HelpMode _helpMode = HelpMode.Disable;
|
||||
public HelpMode HelpMode { get; set; } = HelpMode.Disabled;
|
||||
|
||||
//Lock
|
||||
protected bool _isLocked;
|
||||
internal void Lock() { _isLocked = true; }
|
||||
protected void SetValue<T>(ref T storage, T value)
|
||||
{
|
||||
if (_isLocked)
|
||||
throw new InvalidOperationException("Unable to modify a service's configuration after it has been created.");
|
||||
storage = value;
|
||||
}
|
||||
|
||||
/// <summary> Gets or sets a handler that is called on any successful command execution. </summary>
|
||||
public EventHandler<CommandEventArgs> ExecuteHandler { get; set; }
|
||||
/// <summary> Gets or sets a handler that is called on any error during command parsing or execution. </summary>
|
||||
public EventHandler<CommandErrorEventArgs> ErrorHandler { get; set; }
|
||||
|
||||
public CommandServiceConfig Build() => new CommandServiceConfig(this);
|
||||
}
|
||||
public class CommandServiceConfig
|
||||
{
|
||||
public char? PrefixChar { get; }
|
||||
public bool AllowMentionPrefix { get; }
|
||||
public Func<Message, int> CustomPrefixHandler { get; }
|
||||
|
||||
/// <summary> Gets or sets whether a help function should be automatically generated. </summary>
|
||||
public HelpMode HelpMode { get; set; } = HelpMode.Disabled;
|
||||
|
||||
internal CommandServiceConfig(CommandServiceConfigBuilder builder)
|
||||
{
|
||||
PrefixChar = builder.PrefixChar;
|
||||
AllowMentionPrefix = builder.AllowMentionPrefix;
|
||||
CustomPrefixHandler = builder.CustomPrefixHandler;
|
||||
HelpMode = builder.HelpMode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
public enum HelpMode
|
||||
{
|
||||
/// <summary> Disable the automatic help command. </summary>
|
||||
Disable,
|
||||
Disabled,
|
||||
/// <summary> Use the automatic help command and respond in the channel the command is used. </summary>
|
||||
Public,
|
||||
/// <summary> Use the automatic help command and respond in a private message. </summary>
|
||||
|
||||
Reference in New Issue
Block a user