Exemplo n.º 1
0
        private void HandleUpdate(object obj)
        {
            var update = (NewMessage)obj;

            IUser user;

            if (update.GetUser() == null)
            {
                user = Provider.CreateUser(update);
                Mia.Users.Add(user);
            }
            else
            {
                user = update.GetUser();
            }

            if (!WorkedUsers.ContainsKey(user))
            {
                WorkedUsers.Add(user, new object());
            }

            lock (WorkedUsers[user])
            {
                var prefix = GetPrefix();

                if (!update.Message.Text.StartsWith(prefix))
                {
                    return;
                }

                var command = Mia.Commands.Where(c => c.Aliases.Contains(update.Message.Text.Substring(prefix.Length))).FirstOrDefault();

                if (command == null)
                {
                    Provider.GetApi().Message.SendMessage(GetUnknownCommand(), user);
                }
                else
                {
                    var args = update.Message.Text.Split(' ').ToList();
                    args.RemoveAt(0);

                    var response = command.Run(args.ToArray(), user);

                    if (response is SyntaxErrorResponse)
                    {
                        response = GetSyntaxError();
                    }
                    else if (response is ErrorResponse)
                    {
                        response = GetError(response.Text);
                    }

                    Provider.GetApi().Message.SendMessage(response, user);
                }
            }
        }