{"id":2275,"date":"2020-08-15T16:16:55","date_gmt":"2020-08-16T00:16:55","guid":{"rendered":"https:\/\/www.sirknightj.com\/blog\/?p=2275"},"modified":"2020-08-15T16:29:44","modified_gmt":"2020-08-16T00:29:44","slug":"discord-js-hypixel-bazaar-price-bot","status":"publish","type":"post","link":"https:\/\/www.sirknightj.com\/blog\/2020\/08\/15\/discord-js-hypixel-bazaar-price-bot\/","title":{"rendered":"Discord.js Bot Hypixel Bazaar Price Checker &#8211; Behind the Scenes"},"content":{"rendered":"<p>Over the past few weeks, I&#8217;ve been working on coding a Discord Bot with both fun commands, and moderation commands. As I&#8217;m playing around with the Discord.js library, I learned a ton of neat things about working with it.<\/p>\n<p><a href=\"https:\/\/github.com\/sirknightj\/utilities-discord-bot\">Here<\/a> it is, in case you want to see it.<\/p>\n<p>I wanted to get my bot to get Hypixel Skyblock Bazaar data. However, accessing the Hypixel API can be quite obscure and confusing if you&#8217;ve never done it before. So, to save you the trouble, I&#8217;ll share with you how my bot pulls bazaar data from the Hypixel API.<\/p>\n<p><em>Note: We&#8217;ll be using JavaScript for this, as we are using Discord.js.<\/em><\/p>\n<h5>Installing Dependencies<\/h5>\n<p>I&#8217;m going to assume you&#8217;re using using <span style=\"background-color: #ebebeb;\">npm<\/span>, and that you&#8217;ve already set up your project file using npm.<\/p>\n<p>To fetch things from a website, we&#8217;ll be using the <a href=\"https:\/\/www.npmjs.com\/package\/node-fetch#installation\">node-fetch<\/a> package.<\/p>\n<p>Go ahead and install node-fetch. Navigate to your project folder and<br \/>\n<span style=\"background-color: #ebebeb; font-family: 'courier new', courier, monospace;\">npm install node-fetch<\/span>. You&#8217;ll know that it installed correctly when it appears as a dependency in your <span style=\"background-color: #ebebeb;\">package.json<\/span>\u00a0file.<\/p>\n<p>Next, you&#8217;ll want to make sure that you actually use node-fetch in your code:<br \/>\n<span style=\"background-color: #ebebeb; font-family: 'courier new', courier, monospace;\">const fetch = require(&#8216;node-fetch&#8217;);<\/span><\/p>\n<p><!--more--><\/p>\n<h5>Getting the Response<\/h5>\n<p>Now that you have that set up, make sure that you have an API key from Hypixel. You can obtain one by logging into the server and typing in <span style=\"background-color: #ebebeb;\">\/api new<\/span>.<\/p>\n<p>In your code, you&#8217;ll want to set up something similar to the following:<\/p>\n<pre>let api_key = \"YOUR_API_KEY\";\r\nlet product_id = \"PRODUCT_NAME_HERE\";\r\n\r\nfetch(`https:\/\/api.hypixel.net\/skyblock\/bazaar\/product?key=${api_key}&amp;productId=${product_id}`)\r\n    .then(response =&gt; response.json())\r\n    .then(data =&gt; {\r\n        \/\/ Do something with the data here.\r\n        console.log(data);\r\n    });<\/pre>\n<p>Your API key should be kept private. Make sure that wherever you decide to store your API key, that it doesn&#8217;t accidentally get uploaded onto github if you decide to publicly share your code.<\/p>\n<p>An example of what you could do with the data is to compute the &#8220;flipping profit&#8221; or how much you&#8217;d make by setting up a buy order, then setting up the item as a sell order.<\/p>\n<p>You can access the buy order prices with <span style=\"background-color: #ebebeb; font-family: 'courier new', courier, monospace;\">data.product_info.quck_status.buyPrice<\/span>.<br \/>\nYou can access the sell order prices with <span style=\"background-color: #ebebeb; font-family: 'courier new', courier, monospace;\">data.product_info.quck_status.sellPrice<\/span>.<br \/>\nNow, just subtract the sell price from the buy price and you&#8217;ve got profit. You&#8217;ll have to take the 1% selling tax into account, but hey, you created a basic bot to tell you the profits of the items.<\/p>\n<p>You can see my usage of this <a href=\"https:\/\/github.com\/sirknightj\/utilities-discord-bot\/blob\/master\/commands\/bazaar.js\">here<\/a>.<\/p>\n<h3>Unofficial Documentation<\/h3>\n<p>data is basically the parsed json object returned whenever you send the request. <strong>The official documentation for it can be found <a href=\"https:\/\/github.com\/HypixelDev\/PublicAPI\/blob\/master\/Documentation\/methods\/skyblock\/bazaar.md\">here<\/a><\/strong>. Note all of this data is read-only, so you can&#8217;t change any of the data.<\/p>\n<p>I really liked the Discord.js-style of documentation, so I&#8217;m going to attempt to mimic it here, to make it easier to read.<\/p>\n<h3>Data<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Properties<\/strong><\/td>\n<td><strong>Methods<\/strong><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#success\">success<\/a><br \/>\n<a href=\"#product_info\">product_info<\/a><\/td>\n<td><em>None<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Properties<\/h4>\n<h5 id=\"success\">.success<\/h5>\n<p>True if the fetch request was successful.<br \/>\n<strong>Type<\/strong>: ?boolean<\/p>\n<h5 id=\"product_info\">.product_info<\/h5>\n<p>True if the fetch request was successful.<br \/>\n<strong>Type<\/strong>: <a href=\"#product_info\">product_info<\/a><\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"product_info\">product_info<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Properties<\/strong><\/td>\n<td><strong>Methods<\/strong><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#buy_summary\">buy_summary<\/a><br \/>\n<a href=\"#sell_summary\">sell_summary<\/a><br \/>\n<a href=\"#quick_summary\">quick_status<\/a><\/td>\n<td><em>None<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Properties<\/h4>\n<h5 id=\"buy_summary\">.buy_summary<\/h5>\n<p>The current top 30 buy orders.<br \/>\n<strong>Type: <\/strong><a href=\"#BazaarTransaction\">BazaarTransaction<\/a>[]\n<h5 id=\"sell_summary\">.sell_summary<\/h5>\n<p>The current top 30 sell orders.<br \/>\n<strong>Type: <\/strong><a href=\"#BazaarTransaction\">BazaarTransaction<\/a>[]\n<h5 id=\"quick_summary\">.quick_status<\/h5>\n<p>The stats that most users care about.<br \/>\n<strong>Type: <\/strong><a href=\"#quick_status\">quick_status<\/a><\/p>\n<h3 id=\"BazaarTransaction\">BazaarTransaction<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Properties<\/strong><\/td>\n<td><strong>Methods<\/strong><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#amount\">amount<\/a><br \/>\n<a href=\"#priceperunit\">pricePerUnit<\/a><br \/>\n<a href=\"#orders\">orders<\/a><\/td>\n<td><em>None<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5 id=\"amount\">.amount<\/h5>\n<p>The number of items in the transaction order.<br \/>\n<strong>Type:\u00a0<\/strong>?number<\/p>\n<h5 id=\"priceperunit\">.pricePerUnit<\/h5>\n<p>The price per item in the transaction order.<br \/>\n<strong>Type:\u00a0<\/strong>?number<\/p>\n<h5 id=\"orders\">.orders<\/h5>\n<p>The number of transaction orders offered at this price.<br \/>\n<strong>Type:\u00a0<\/strong>?number<\/p>\n<h3 id=\"quick_status\">quick_status<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Properties<\/strong><\/td>\n<td><strong>Methods<\/strong><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#productId\">productId<\/a><br \/>\n<a href=\"#sellPrice\">sellPrice<\/a><br \/>\n<a href=\"#sellVolume\">sellVolume<\/a><br \/>\n<a href=\"#sellMovingWeek\">sellMovingWeek<\/a><br \/>\n<a href=\"#sellOrders\">sellOrders<\/a><br \/>\n<a href=\"#buyPrice\">buyPrice<\/a><br \/>\n<a href=\"#buyVolume\">buyVolume<\/a><br \/>\n<a href=\"#buyMovingWeek\">buyMovingWeek<\/a><br \/>\n<a href=\"#buyOrders\">buyOrders<\/a><\/td>\n<td><em>None<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5 id=\"productId\">.productId<\/h5>\n<p>The name of this product.<br \/>\n<strong>Type: <\/strong>string<br \/>\n<strong>Examples:<\/strong> &#8220;ENCHANTED_SAND&#8221; or &#8220;RAW_FISH&#8221;<\/p>\n<h5 id=\"sellPrice\">.sellPrice<\/h5>\n<p>The weighted average of the top 2% of sell orders, by volume.<br \/>\n<strong>Type: <\/strong>?number<br \/>\n<strong>Usage:\u00a0<\/strong><em>This is the instant-buy price you see in the Hypixel Bazaar.<\/em><\/p>\n<h5 id=\"sellVolume\">.sellVolume<\/h5>\n<p>The current number of items offered in all sell orders combined.<br \/>\n<strong>Type: <\/strong>?number<\/p>\n<h5 id=\"sellMovingWeek\">.sellMovingWeek<\/h5>\n<p>The number of transactions from the last 7 days.<br \/>\n<strong>Type: <\/strong>?number<\/p>\n<h5>.sellOrders<\/h5>\n<p>The count of active sell orders.<br \/>\n<strong>Type: <\/strong>?number<\/p>\n<h5 id=\"buyPrice\">.buyPrice<\/h5>\n<p>The weighted average of the top 2% of buy orders, by volume.<br \/>\n<strong>Type: <\/strong>?number<br \/>\n<strong>Usage:\u00a0<\/strong><em>This is the instant-sell price you see in the Hypixel Bazaar.<\/em><\/p>\n<h5 id=\"buyVolume\">.buyVolume<\/h5>\n<p>The current number of items offered in all buy orders combined.<br \/>\n<strong>Type: <\/strong>?number<\/p>\n<h5 id=\"buyMovingWeek\">.buyMovingWeek<\/h5>\n<p>The number of transactions from the last 7 days.<br \/>\n<strong>Type: <\/strong>?number<\/p>\n<h5 id=\"buyOrders\">.buyOrders<\/h5>\n<p>The count of active sell orders.<br \/>\n<strong>Type: <\/strong>?number<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the past few weeks, I&#8217;ve been working on coding a Discord Bot with both fun commands, and moderation commands. As I&#8217;m playing around with the Discord.js library, I learned a ton of neat things about working with it. Here it is, in case you want to see it. I wanted to get my bot &#8230; <a title=\"Discord.js Bot Hypixel Bazaar Price Checker &#8211; Behind the Scenes\" class=\"read-more\" href=\"https:\/\/www.sirknightj.com\/blog\/2020\/08\/15\/discord-js-hypixel-bazaar-price-bot\/\" aria-label=\"More on Discord.js Bot Hypixel Bazaar Price Checker &#8211; Behind the Scenes\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[47,5],"tags":[],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts\/2275"}],"collection":[{"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/comments?post=2275"}],"version-history":[{"count":14,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts\/2275\/revisions"}],"predecessor-version":[{"id":2289,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts\/2275\/revisions\/2289"}],"wp:attachment":[{"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/media?parent=2275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/categories?post=2275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/tags?post=2275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}