package roblox import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "net/url" "strconv" ) // Item struct containing data on retrieved items type Item struct { ProductID int ItemID int AssetID int UserID int UserName string Type string Name string CopyLocked bool Owned bool } // HasItem will return if you own the item func (s *Session) HasItem(id int) (bool, error) { resp, err := s.client.Get(fmt.Sprintf("http://api.roblox.com/Ownership/HasAsset?userId=%d&assetId=%d", s.ID, id)) if err != nil { return false, err } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { return false, err } return string(data) == "true", nil } // AddItem add item by id to inventory func (s *Session) AddItem(id int, seller int) error { v := url.Values{} v.Set("rqtype", "purchase") v.Set("productID", strconv.Itoa(id)) v.Set("expectedCurrency", "1") v.Set("expectedPrice", "0") v.Set("expectedSellerID", strconv.Itoa(seller)) v.Set("userAssetID", "") resp, err := s.client.Post("https://www.roblox.com/api/item.ashx?"+v.Encode(), "application/json", bytes.NewBufferString("")) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode == 403 { req, err := http.NewRequest("POST", "https://www.roblox.com/api/item.ashx?"+v.Encode(), bytes.NewBufferString("")) req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0]) req.Header.Set("Content-Type", "application/json") resp, err := s.client.Do(req) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != 200 { return fmt.Errorf("Failed to add item. Status %d", resp.StatusCode) } } return nil } // RemoveItem will remove item from inventory func (s *Session) RemoveItem(id int) error { v := url.Values{} v.Set("assetId", strconv.Itoa(id)) resp, err := s.client.Post("https://www.roblox.com/asset/delete-from-inventory", "application/x-www-form-urlencoded", bytes.NewBufferString(v.Encode())) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode == 403 { req, err := http.NewRequest("POST", "https://www.roblox.com/asset/delete-from-inventory", bytes.NewBufferString(v.Encode())) req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0]) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") resp, err := s.client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() if resp.StatusCode != 200 { return fmt.Errorf("Failed to remove item. Status %d", resp.StatusCode) } } return nil } // GetModels will list models in inventory func (s *Session) GetModels(user int) ([]Item, error) { var Data []Item resp, err := s.client.Get("https://www.roblox.com/users/inventory/list-json?assetTypeId=10&userId=" + strconv.Itoa(user)) if err != nil { return Data, fmt.Errorf("Could not get list: %s", err) } if resp.StatusCode == 200 { var dat map[string]interface{} defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) if err := json.Unmarshal(body, &dat); err != nil { panic(err) } set := dat["Data"].(map[string]interface{}) ilist := set["Items"].([]interface{}) for _, obj := range ilist { itm := obj.(map[string]interface{}) iInfo := itm["Item"].(map[string]interface{}) iCreator := itm["Creator"].(map[string]interface{}) Data = append(Data, Item{0, int(iInfo["AssetId"].(float64)), 0, int(iCreator["Id"].(float64)), iCreator["Name"].(string), "Model", iInfo["Name"].(string), false, true}) } return Data, nil } return Data, fmt.Errorf("Could not get models. Status: %d", resp.StatusCode) }