alaingilbert / Turntable-API

Allows you to create bots for turntable.fm
http://alaingilbert.github.com/Turntable-API/
MIT License
318 stars 97 forks source link

How to make a proper Fan Command #248

Closed ghost closed 10 years ago

ghost commented 10 years ago

How can i make a proper Fan Command for my bot.

Fan Command: makes the bot automatically fan the person that did the command.

I tried coding a fan command with this code:

bot.on('speak', function (data) {
if (data.match(/fan/))
   bot.becomeFan(data.user[0].userid);
   bot.speak('I am now your fan, @ data.name.');
});

but it crashed the bot, and won't let the bot run.

alaingilbert commented 10 years ago

What error do you get ? (complete stack trace)

ghost commented 10 years ago

Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts! (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace at Bot.EventEmitter.addListener (events.js:168:15) at Object. (/var/lib/stickshift/51d1c5aa5973cae5750000b5/app-root/data/544956/node_modules/chat_bot.js:2214:5) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.runMain (module.js:492:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9) [ VeggieBot 3.6.0 is READY! on Tue Aug 06 2013 18:03:55 GMT-0400 (EDT) ] User not found, may be offline VeggieBot: VeggieBot, welcome to the room! We have free entertainment, lemonade, and crazy moderators.

/var/lib/stickshift/51d1c5aa5973cae5750000b5/app-root/data/544956/node_modules/chat_bot.js:2215 if (data.match(/fan/)) ^ TypeError: Object # has no method 'match' at Bot. (/var/lib/stickshift/51d1c5aa5973cae5750000b5/app-root/data/544956/node_modules/chat_bot.js:2215:10) at Bot.EventEmitter.emit (events.js:115:20) at Bot.treatCommand (/var/lib/stickshift/51d1c5aa5973cae5750000b5/app-root/data/544956/node_modules/ttapi/lib/bot.js:311:19) at Bot.treatPacket (/var/lib/stickshift/51d1c5aa5973cae5750000b5/app-root/data/544956/node_modules/ttapi/lib/bot.js:318:19) at Bot.onMessage (/var/lib/stickshift/51d1c5aa5973cae5750000b5/app-root/data/544956/node_modules/ttapi/lib/bot.js:333:19) at frameType (/var/lib/stickshift/51d1c5aa5973cae5750000b5/app-root/data/544956/node_modules/ttapi/lib/websocket.js:308:30) at process.startup.processNextTick.process._tickCallback (node.js:244:9)

alaingilbert commented 10 years ago

The problem you have is this:

Each time you register a listener (ie: bot.on('signal', callback);) the callback is going to be called every time the signal is triggered.

So, if you do something like this:

bot.on('signal1', function() {
  bot.on('signal2', function() {
    // ...
  });
});

Every time signal1 is triggered, you add a new listeners for signal2. (and this, create a memory leak)

This is the problem you have.

If you want to fix this, make sure you don't have nested listeners.

ghost commented 10 years ago

ok