Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Script help - error when listener activated

Hi, I'm a coder but never worked with JS and getting this script working is driving me mad. It's simply meant to detect the !wandering command, then parse the level number from page name ("Level 1: Dungeon Level" -> "1") and send to chat "!rtm WM" + LevelNb. Rollable Table Macros will then handle rolling on the correct wandering monster table for that page. However, every time I send the !wandering command, the API errors out. Any advice would be great, thanks. var WanderingMonster = WanderingMonster || ( function() {     'use strict';          var commandListener = function() {         // Listens for API command         on( 'chat:message', function( msg ) {             if( msg.type === 'api' && !msg.rolltemplate ) {                 if( command === 'wandering' ) {                     var currentPageID = Campaign().get('playerpageid') ,                         currentPage = getObj( 'page' , currentPageID ) ,                         pageName = currentPage.get('name') ,                         pageNumber = pageName.substring( 7 , pageName.search(':') - 1);                     sendChat( 'WM', '!rtm WM' + pageNumber );                 }             }         });     };     return {         CommandListener: commandListener     }; }()); on( 'ready', function(){    'use strict';        WanderingMonster.CommandListener(); });
1550968502

Edited 1550968994
GiGs
Pro
Sheet Author
API Scripter
You don't have a 'command' defined here: if( command === 'wandering' ) { There are a number of ways to detect the command, a simple one is this: on("chat:message", function(msg) {         if (msg.type == "api" && msg.content.indexOf("!wandering") !== -1) { This detects if the message starts with !wandering, and ignores anything else. You don't need the msg.rolltemplate (I've never used that, is it actually valid?)
OK, changing that worked, thanks a lot! No idea on the msg.rolltemplate - I copied the functionality from rtm, where it worked.
1550968984
GiGs
Pro
Sheet Author
API Scripter
You're welcome :)
1551146543
The Aaron
Roll20 Production Team
API Scripter
GiGs said: You don't have a 'command' defined here: if( command === 'wandering' ) { There are a number of ways to detect the command, a simple one is this: on("chat:message", function(msg) {         if (msg.type == "api" && msg.content.indexOf("!wandering") !== -1) { This detects if the message starts with !wandering, and ignores anything else. You don't need the msg.rolltemplate (I've never used that, is it actually valid?) Technically, that detects if the msg.content contains “!wandering”. People seem to favor that method, but it would also match “!delay !wandering” or pretty much any string containing the command. Better to check === 0 to be certain it is at the start of the string, or use /^!wandering\b/i.test(msg.content) which matches the beginning of the string (and the word boundary at the end), is more efficient (fails in two character comparisons rather than (n-string length) ones), and gives you case insensitive comparisons.