/// <summary> /// Creates a new command args object from the regex match, the message arguments and the /// config for the current bot and server/guild. /// </summary> /// <param name="match">The original regex match for the command.</param> /// <param name="args">The arguments from the message creation event.</param> /// <param name="config">The config object for the server/guild.</param> public CommandArgs(Match match, MessageCreateEventArgs args, TConfig config, BotInstance instance) { Match = match; Args = args; Config = config; Instance = instance; }
/// <summary> /// Detaches the bot from its instance. /// </summary> public void Detach() { if (Instance != null) { Instance.Client.MessageCreated -= OnMessageCreated; Instance.Client.MessageReactionAdded -= OnReactionAdded; Instance.Client.MessageReactionRemoved -= OnReactionRemoved; Instance.onStartup -= OnStartup; Instance.onShutdown -= OnShutdown; Instance.onConnected -= OnConnected; Instance.onSave -= OnSave; Instance.onLoad -= OnLoad; Instance = null; } }
/// <summary> /// Attaches this bot to an instance so it can run. /// </summary> /// <param name="instance">The instance to attach the bot to.</param> public void AttachTo(BotInstance instance) { if (Instance != null) { Detach(); } Instance = instance; Instance.Client.MessageCreated += OnMessageCreated; Instance.Client.MessageReactionAdded += OnReactionAdded; Instance.Client.MessageReactionRemoved += OnReactionRemoved; Instance.onStartup += OnStartup; Instance.onShutdown += OnShutdown; Instance.onConnected += OnConnected; Instance.onSave += OnSave; Instance.onLoad += OnLoad; }
/// <summary> /// Attempts to invoke this command given the current user string. /// </summary> /// <param name="args">The message context used to invoke this command attempt.</param> /// <param name="config">The configuration object for the specifc bot and server.</params> /// <param name="instance">The bot instance running the current bot.</params> /// <returns>A task for handling the command.</returns> public async Task <CommandState> AttemptAsync(MessageCreateEventArgs args, TConfig config, BotInstance instance) { Match commandMatch = Regex.Match(args.Message.Content, $"^{config.Indicator}{CommandString}\\s*(?<parameters>[\\w\\W]*)$"); if (commandMatch.Success) { if (ParameterRegex is null) { await Callback(new CommandArgs <TConfig>(null, args, config, instance)); return(CommandState.Handled); } else { string parameters = commandMatch.Groups["parameters"].Value; Match parameterMatch = Regex.Match(parameters, ParameterRegex); if (parameterMatch.Success) { await Callback(new CommandArgs <TConfig>(parameterMatch, args, config, instance)); return(CommandState.Handled); } else { return(CommandState.ParameterError); } } } else { return(CommandState.Unhandled); } }