Exemple #1
0
        /// <summary>
        /// Handles an incoming user message.
        /// </summary>
        /// <param name="userMessage">The message.</param>
        public async Task HandleUserMessage(AbstractSimpleUserMessage userMessage)
        {
            string text     = userMessage.GetSimpleText();
            int    cmdIndex = 0;

            if (text.StartsWith("//") || text.StartsWith("!!"))
            {
                cmdIndex = 2;
            }
            else if (text.StartsWith("/") || text.StartsWith("!"))
            {
                cmdIndex = 1;
            }
            else if (!userMessage.HasMentionPrefix(out cmdIndex))
            {
                return;
            }
            text = text.Substring(cmdIndex).TrimStart();
            string[] cmdArgs = text.Split(' ', 2);
            string   command = cmdArgs[0].ToLower();

            if (Commands.TryGetValue(command, out Func <BotCommand, Task> func))
            {
                string   arguments = cmdArgs.Length > 1 ? cmdArgs[1].Trim() : string.Empty;
                string[] splitArgs = arguments.Split(' ').Where((x) => !string.IsNullOrWhiteSpace(x)).ToArray();
                await func(new BotCommand(this, userMessage, command, splitArgs));
            }
        }
Exemple #2
0
 /// <summary>
 /// Constructs a new bot command to run.
 /// </summary>
 /// <param name="bot">The bot instance.</param>
 /// <param name="message">The user message that initiated this command.</param>
 /// <param name="alias">The alias used in the request.</param>
 /// <param name="arguments">All arguments.</param>
 public BotCommand(Bot bot, AbstractSimpleUserMessage message, string alias, string[] arguments)
 {
     Bot       = bot;
     Message   = message;
     Alias     = alias;
     Arguments = arguments;
 }