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

Chat Command Handling

I am confused on what is exactly needed for chat command handling. My goal is to make a run-once API that will load all of my rollable tables. I just made a single one that would make the 10 GP gems from the DMG to try this out, but it's not parsing my !loot-table command to start itself (as evidenced by the lack of sendChat on the listener.

Am I missing another chunk that activates the chat listener?

var chatName="Loot-Table-API",
    tableName,
    tableFind,
	error=0;
    
	createTable = function(newName){
		var newTable;
		tableFind=findObjs({type: "rollabletable", name: newName});
		if(tableFind.length){
			sendChat("LootTable-API", "/w GM "+newName+" already exists as a rollable table.");
			return "";
		}
		else
		{
			newTable=createObj("rollabletable",{name: newName});
			return newTable;
		}
	},
	
	createItem = function(tableFind,itemName,itemWeight){
		createObj("tableitem",{
			_rollabletableid: tableFind.id,
			name: itemName,
			weight: itemWeight
		});
	},
	
	doItAll = function(){
		var gems10 = createTable("Gems10");
		if(gems10 != ""){
			createItem(gems10,"Azurite (plaque mottled deep blue)",1);
/* Things I'm taking out of the forum post because I don't want to violate copyright just now
/
/
/
*/
			createItem(gems10,"Turquoise (opaque light blue green)",1);
			sendChat(chatName, "/w GM "+gems10.name+" was created.");
		}
	}
	
	registerEventHandlers = function(){        
		on("chat:message", function (msg) {
			// Exit if not an api command 
			if (msg.type !== "api") {
                return;
            }


			var tokenized = msg.content.split(" "),
                command = tokenized[0];


			switch(command) {
				case "!loot-table":
                    sendChat(chatName,"LootTable API called")
					if(playerIsGM(msg.playerid)) {
						doItAll();
					}
				break;
			}
		});
	};
June 11 (9 years ago)

Edited June 11 (9 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
You need an on(chat:message, function(){whatever you want to happen on the chat command}) in order for the script to respond to something in chat. There's a bit more to it than that as well, but in general, that's what it looks like you're missing. I've gone with Aaron's method of making a handleInput function that gets called on a chat message and then filters through the chat to make sure things are only called when they should be. Take a look at the code from scripts like Aaron's GroupInitiative or tokenmod; or my PageNavigator.

Also, in case you don't want to have to reinvent the wheel, take a look at Aaron's TableExport script (which also imports tables) to see if it does what you need, or if it can give you ideas for how to do what you want to do if you need something different.
June 12 (9 years ago)
Lithl
Pro
Sheet Author
API Scripter
Your registerEventHandlers function is never called. Add this:
on('ready', function() { registerEventHandlers(); });
And you should be fine.
June 12 (9 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Woops, I'm blind, totally didn't see that you already had a registereventhandlers. Sorry about that.
I totally stole the framework from The Aaron's WeightedDice script, because I haven't really done any listening API yet.
June 13 (9 years ago)
The Aaron
Pro
API Scripter
It can take a bit to wrap your head around. =D
It works now. so all I have to do is populate thousands of tables in excel, then combine them like voltron into API format.
June 13 (9 years ago)
The Aaron
Pro
API Scripter
Indeed!  Ziechael has done something similar with my TableExport script.  He has something set up in spreadsheets to create the right commands, but you could likely use a similar technique to make the createItem() calls.
June 13 (9 years ago)
Ziechael
Forum Champion
Sheet Author
API Scripter
I do it with all my standard format import based scripts (power card maker, npc creator, recursive tables... I'm a formula junkie ;) )
My wife mocks me because all problems are solved with Excel. It's the duct tape of the computer age.
https://docs.google.com/spreadsheets/d/16b3clpy-zFUSK_SdrkvvwIDYi486at9dtoiXv6Qoa6I/edit?usp=sharing