Jump to content
McKay Development

Creating Specific offer from chat command


Recommended Posts

I'd like to create something like !buy and !sell command for my bot but I got error when testing it and I don't know why is that.

 

My code:

function peoplechatsell(name){
	Methods.readDataBase('./prices.json')
	.then((Database) => {
		console.log('hi '+name);
		if (Database[name]) {		
			var item = Database[name];
			var buy_metal = item.buy.metal;
			var buy_key = item.buy.keys;
			var myref = Pures_InStock.metal;
			var mykey = Pures_InStock.keys;
			console.log(myref)
			if(myref >= buy_metal && mykey >= buy_key){
				console.log('Enough Pure to Buy... Creating Offer');
				manager.getInventoryContents(config.bossSteamID, 440, 2, true, function(err, inventory) {
					if(err) {
					  console.log('Error getting their inventory: ' + err);
					}else{
						var offer = manager.createOffer(config.bossSteamID);
						var ref = inventory.filter(function (item) {
							return item.market_hash_name.match('Refined Metal');
						});
						var scrap = inventory.filter(function (item) {
							return item.market_hash_name.match('Scrap Metal');
						});
						var needref = Math.floor(buy_metal);
						var a = buy_metal*10 - needref*10;
						var needscrap = Math.floor(a);
						
						console.log(needref);
						console.log(needscrap);

						offer.addMyItems(ref.slice(0, needref));
						offer.addMyItems(scrap.slice(0, needscrap));

						manager.getUserInventoryContents(config.bossSteamID, 440, 2, true, function(err, inventory) {
							if(err) {
							  console.log('Error getting their inventory: ' + err);
							}else{
								var Item = inventory.filter(function (item) {
									return item.market_hash_name.match(name);
								});		
		
								offer.addTheirItems(Item.slice(0, 1));
		
								offer.send(function (err, status){
									if (err) {
										console.log(err);
									} else if (status == 'pending'){
										community.acceptConfirmationForObject(config.identity_secret, offer.id, function(err) {
											if (err) {
												console.log(err);
											} else {
												console.log("Offer confirmed");
											}
										});
									} else {
										console.log('Trade offer #'+offer.id+' sent successfully');
									}
								});								
							}
						})
						
					}
				})
			} else {
				console.log('nono');
				
			}
		} else {
			return;
		}
	})
}

const parseAdminMessage = (message) => {
	if (message == '!update') {
		updatePrices();
	} else if (message == '!code') {
		var code = SteamTotp.generateAuthCode(config.shared_secret)
		client.chatMessage(config.bossSteamID, 'Here is the code: ' + code);
	} else if (message == '!bump') {
		bumpListings();
	} else if (message.indexOf("!sell") === 0) {
		var name = (message.replace(/^!sell ?/, ""));
		peoplechatsell(name);
	}
}

 

 

Error code:

hi Battle-Worn Robot Taunt Processor
2
Enough Pure to Buy... Creating Offer
C:\Users\User\Desktop\custom-tf2\node_modules\steamcommunity\components\users.js:453
                                                callback(err);
                                                ^

TypeError: callback is not a function
    at C:\Users\User\Desktop\custom-tf2\node_modules\steamcommunity\components\users.js:453:7
    at SteamCommunity._checkHttpError (C:\Users\User\Desktop\custom-tf2\node_modules\steamcommunity\components\http.js:110:3)
    at Request._callback (C:\Users\User\Desktop\custom-tf2\node_modules\steamcommunity\components\http.js:50:61)
    at Request.self.callback (C:\Users\User\Desktop\custom-tf2\node_modules\request\request.js:185:22)
    at Request.emit (events.js:198:13)
    at Request.<anonymous> (C:\Users\User\Desktop\custom-tf2\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:198:13)
    at Gunzip.<anonymous> (C:\Users\User\Desktop\custom-tf2\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:286:20)
    at Gunzip.emit (events.js:203:15)
Press any key to continue . . .

 

Link to comment
Share on other sites

I see. It fixes it now but I want to know if there's any better method to calculate the metal (like check if there's any reclaimed metal and use them if possible instead of all scrap) needed and how to code when there's a need for exchange.

My price list is in json format like this:

{
	"Battle-Worn Robot Taunt Processor":{
		"sell":{
			"keys":0,
			"metal":0.88
		},
		"buy":{
			"keys":0,
			"metal":0.77
		},
		"max_stock":1,
		"status":"both"
	}
}

 

Edited by TextDynasty
Link to comment
Share on other sites

I'd do something like calculate the total amount of scrap required (so e.g. 1.33 is 12 scrap). Then you could do something like this to calculate how much of each metal type you require:

let refRequired = Math.floor(scrapRequired / 9);
scrapRequired %= 9;
let recRequired = Math.floor(scrapRequired / 3);
scrapRequired %= 3;
// scrapRequired is now how much you need after ref and rec

 

Link to comment
Share on other sites

6 hours ago, Dr. McKay said:

I'd do something like calculate the total amount of scrap required (so e.g. 1.33 is 12 scrap). Then you could do something like this to calculate how much of each metal type you require:


let refRequired = Math.floor(scrapRequired / 9);
scrapRequired %= 9;
let recRequired = Math.floor(scrapRequired / 3);
scrapRequired %= 3;
// scrapRequired is now how much you need after ref and rec

 

Yes I did something like this but if there is, lets say, not enough scrap metal, the bot cannot proceed the trade because it cannot recognize it can get exchanges from the user. Here is my script so far

 

function peoplechatsell(steamID, name, amount){
	Methods.readDataBase('./prices.json')
	.then((Database) => {
		if (Database[name] && Database[name].status != 'none' && Database[name] != 'sell') {		
			var item = Database[name];
			var stock = item.max_stock;
			var buy_metal = item.buy.metal * amount;
			var buy_key = item.buy.keys * amount;
			var myref = Pures_InStock.metal;
			var mykey = Pures_InStock.keys;
			console.log(myref);
			if(myref >= buy_metal && mykey >= buy_key){
				manager.getInventoryContents(440, 2, true, function(err, inventory) {
					if(err) {
					  console.log('Error getting their inventory: ' + err);
					}else{
						var offer = manager.createOffer(steamID);
						var key = inventory.filter(function (item) {
							return item.market_hash_name.match('Mann Co. Supply Crate Key');
						});
						var ref = inventory.filter(function (item) {
							return item.market_hash_name.match('Refined Metal');
						});
						var rec = inventory.filter(function (item) {
							return item.market_hash_name.match('Reclaimed Metal');
						});
						var scrap = inventory.filter(function (item) {
							return item.market_hash_name.match('Scrap Metal');
						});
						var iteminstock = inventory.filter(function (item) {
							return item.market_hash_name.match(name);
						});

						var needkey = buy_key;
						var needref = Math.floor(buy_metal);
						var a = (buy_metal*10 - needref*10)/3;
						var needrec = Math.floor(a);
						var b = buy_metal*10 - needref*10 - needrec*3;
						var needscrap = Math.floor(b);

						console.log('Need Key: '+needkey);
						console.log('Need Ref: '+needref);
						console.log('Need Rec: '+needrec);
						console.log('Need Scrap: '+needscrap);

						console.log(ref.length);
						console.log(rec.length);
						console.log(scrap.length);

						var scrapRequired = Math.floor(buy_metal)*9 +(buy_metal * 10)-(Math.floor(buy_metal)*10);
						let refRequired = Math.floor(scrapRequired / 9);
						scrapRequired %= 9;
						let recRequired = Math.floor(scrapRequired / 3);
						scrapRequired %= 3;

						console.log('Need Scrap: '+scrapRequired);
						console.log('Need Rec: '+recRequired);
						console.log('Need Ref: '+refRequired);

						



						//Check if item is overstock
						if(amount + iteminstock.length > stock){
							console.log('Item overstock');
							client.chatMessage(steamID, 'Very sorry, the item ('+name+') you are trying to sell is overstock. The item stock limit is '+stock+'.');
							return;
						};

						//Check if there is enough metal to create offer
						if(key.length >= needkey && ref.length >= needref && rec.length >= needrec && scrap.length >= needscrap){
							offer.addMyItems(key.slice(0, needkey));
							offer.addMyItems(ref.slice(0, needref));
							offer.addMyItems(rec.slice(0, needrec));
							offer.addMyItems(scrap.slice(0, needscrap));
						} else {
							console.log('Not enough currencies. Cancelling');
							client.chatMessage(steamID, 'Sorry, !sell command only works when bot have enough pure.');
							return;
						}
						
						//load user bp
						manager.getUserInventoryContents(steamID, 440, 2, true, function(err, inventory) {
							if(err) {
							  console.log('Error getting their inventory: ' + err);
							  client.chatMessage(steamID, 'Sorry, I cannot get your inventory right now.');
							}else{
								var Item = inventory.filter(function (item) {
									return item.market_hash_name.match(name);
								});
		
								if(Item.length >= amount){
									offer.addTheirItems(Item.slice(0, amount));		
									offer.send(function (err, status){
										if (err) {
											console.log(err);
										} else if (status == 'pending'){
											community.acceptConfirmationForObject(config.identity_secret, offer.id, function(err) {
												if (err) {
													console.log(err);
												} else {
													console.log("Offer confirmed");
													client.chatMessage(steamID, 'Trade offer sent.');
												}
											});
										} else {
											console.log('Trade offer #'+offer.id+' sent successfully');
											client.chatMessage(steamID, 'Thanks for picking '+config.game+' Service.');
										}
									});
								}else{
									console.log('User does not have the item.');
									client.chatMessage(steamID, 'Please make sure you have the item in your backpack.');
									return;
								}
							}
						})
						
					}
				})
			} else {
				console.log('Not enough currencies to buy.');
				client.chatMessage(steamID, 'Sorry, !sell command only works when bot have enough pure.');
				return;				
			}
		} else {
			console.log('Item not appears on the listings.')
			client.chatMessage(steamID, 'Sorry, but the item you are selling is not on my buying list.');
			return;
		}
	})
}

 

Link to comment
Share on other sites

8 hours ago, TextDynasty said:

The bot now counts each type of metal it requires but it cannot switch to others when one is out of stock. What should I do?

Working out change is similar to the popular coin change problem, except you don't have infinite amount of each coin. 

 

Here's how I would do it:

Convert the price to scrap. Then using the coin change problem work out the least amount of scrap needed. In this case your coins would be 1, 3, 9, and key price in scrap being scrap, rec, ref and key price in scrap respectively. 

 

Look up the coin change problem and develop a function to work out the change. It's hard but also good to develop your coding skills. I wouldn't recommend to steal someone else's code since you don't know what it's doing and at the end of the day if your bot gets scammed it is your fault for not knowing how your code works. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...