seishun / node-steam-trade

Node.js wrapper around Steam trading
162 stars 36 forks source link

Bot gets really spammy after a trade #49

Closed kjsmita6 closed 9 years ago

kjsmita6 commented 9 years ago

I am having an issue with one of my bots where the bot spams messages after a trade. I used this code to message myself what items someone donates:

if (status == 'complete') { logger.info('Trade was completed'); for(var i = 0; i < endItems.length; i++) { logger.debug(steamID + ' (' + bot.users[steamID].playerName + ') donated ' + endItems); bot.sendMessage('76561198091343023', steamID + ' (' + bot.users[steamID].playerName + ') donated ' + endItems); //76561198108993241 } endItems = []; inTrade = false; }

I get the following message right after:

Cupcake [BOT]: 76561198091343023 (Banshee.§Ð9) donated The Widowmaker,The Widowmaker,Baby Face's Blaster,Baby Face's Blaster Cupcake [BOT]: 76561198091343023 (Banshee.§Ð9) donated The Widowmaker,The Widowmaker,Baby Face's Blaster,Baby Face's Blaster Cupcake [BOT]: 76561198091343023 (Banshee.§Ð9) donated The Widowmaker,The Widowmaker,Baby Face's Blaster,Baby Face's Blaster Cupcake [BOT]: 76561198091343023 (Banshee.§Ð9) donated The Widowmaker,The Widowmaker,Baby Face's Blaster,Baby Face's Blaster

(this was after donating 2 items).

This seems to fix itself when I restart the bot though. I tried adding steamTrade.setMaxListeners to 0, but that didn't help.

seishun commented 9 years ago

Can you reduce your bot code to a minimal test case that would pinpoint a specific issue in the library?

kjsmita6 commented 9 years ago

I did this. It seems like it only spams when an item is added or removed. I have this:

bot.on('sessionStart', function(steamID) { steamTrade.open(steamID); steamTrade.loadInventory(440, 2, function(inv) { inventory = inv; steamTrade.on('offerChanged', function(boolean, item) { if(boolean = true) { console.log('They added ' + item.name); rup(); } else { console.log('They removed ' + item.name); } }); }); steamTrade.on('end', function(status) { if (status == 'complete') { console.log('Trade was completed'); } }); });

kjsmita6 commented 9 years ago

Also "rup()" is a function that i made, it equals this:

function rup() { steamTrade.on('ready', function() { logger.debug('They readied up, doing the same'); steamTrade.ready(function() { setTimeout(function() { steamTrade.confirm(); }, 1500); }); }); };

fiskWasTaken commented 9 years ago

From what it looks like that rup() function is creating a new listener for the ready event each time rup() is called. You don't want to do that, it's probably the root of your problem. It probably is, since we can see that 4 items are being donated, and the message is repeated 4 times...

Remember that everything here is event based. Get rid of that rup() function entirely and put the listener for 'ready' in your script's global scope. The code inside that will run whenever the other user readies up, as described in the documentation.

seishun commented 9 years ago

You're adding an extra listener for 'offerChanged' whenever the inventory is loaded, and an extra listener for 'ready' whenever 'offerChanged' is emitted, which causes your bot to confirm multiple times, and thus receive the "trade finished" response multiple times.

kjsmita6 commented 9 years ago

This still hasn't fixed it. My code now is:

steamTrade.on('ready', function() { console.log('They readied up, doing the same'); steamTrade.ready(function() { setTimeout(function() { steamTrade.confirm(); }, 1500); }); });

bot.on('sessionStart', function(steamID) { steamTrade.open(steamID); steamTrade.loadInventory(440, 2, function(inv) { inventory = inv; }); steamTrade.on('offerChanged', function(boolean, item) { if(boolean == true) { console.log('They added ' + item.name); } else if(boolean == false) { console.log('They removed ' + item.name); } }); steamTrade.on('end', function(status) { if (status == 'complete') { console.log('Trade was completed'); } }); });

And it doesn't just spam me with the trade completed, it spams with "They added" and "They remove" and "They said this:".

fiskWasTaken commented 9 years ago

That's because you still haven't fixed your issue with your code. Refer to previous comments.

This issue tracker is not for code help. If you don't understand how event-driven programming works then you should be reading up on it first.