예제 #1
0
        public async Task Price([Remainder, Summary("Item name")] string item)
        {
            try
            {
                item = item.ToLower();
                if (!bItemsCached)
                {
                    using (StreamReader sr = new StreamReader(@"res\osrsitems.txt"))
                    {
                        string itemsJson = await sr.ReadToEndAsync();

                        string        format = itemsJson.Trim(new char[] { '{', '}' });
                        List <string> items  = format.Split(new string[] { "}," }, StringSplitOptions.None).ToList <string>();
                        for (int i = 0; i < items.Count; i++)
                        {
                            items[i] = items[i].Trim().Replace("\"", String.Empty).Replace("{", String.Empty);
                            string[] itemArray = items[i].Split('\n');
                            string   id        = itemArray[0].Trim('\"').Split(':').First();
                            string   name      = itemArray[1].Split(':').Last().Replace(",", String.Empty).Trim();
                            bool     trade     = Boolean.Parse(itemArray[3].Split(':').Last().Replace(",", String.Empty).Trim());

                            RunescapeItem rsItem = new RunescapeItem(id, name, trade);
                            try
                            {
                                if (trade)
                                {
                                    dictItems.Add(rsItem.Name.ToLower(), rsItem);
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    bItemsCached = true;
                }

                RunescapeItem OsrsItem = dictItems[item];
                if (!OsrsItem.Trade)
                {
                    await Context.Channel.SendMessageAsync("Item is not tradeable!");

                    return;
                }
                string itemId         = OsrsItem.Id;
                string responseString = await client.GetStringAsync($"http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item={itemId}");

                XmlDocument docItem = JsonConvert.DeserializeXmlNode(responseString);
                XmlNode     ndItem  = docItem.DocumentElement;
                string      price   = ndItem.SelectSingleNode("current/price").InnerText;
                string      icon    = ndItem.SelectSingleNode("icon_large").InnerText;
                string      trend   = ndItem.SelectSingleNode("day30/change").InnerText;
                var         embed   = new Discord.EmbedBuilder();
                embed.WithImageUrl(icon);
                await Context.Channel.SendMessageAsync($"{item}: {price} gp", false, embed.Build());

                await Context.Channel.SendMessageAsync($"Trend: {trend}");
            }
            catch (Exception ex)
            {
                await Context.Channel.SendMessageAsync($"Error looking item in GE. Item is either untradeable or spelled wrong.");
            }
        }