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 .
×

Chat script help

Hi roll20 scripters, I'm looking for a script that would read in a sentence and then replace certain words in that sentence with specific words from a dictionary. I've no experience in javascript so some pointers would be helpful. As an example, I would want to type something like "!command some long sentence" and it would check a dictionary ["some" : "other", "sentence" : "words"] and replace the matching words with those from the dictionary and output "other long words" for this example. I've got something written in python that I have been copying and pasting the output into chat from but it would be nice to have it embedded in the system. I'm using it to roleplay a dwarf and use the khazalid language in place of some words. I'm not trying to convert all words in the sentence or make gibberish. Any assistance appreciated!
1588773964
The Aaron
Roll20 Production Team
API Scripter
Here's a script you can use.  Just call: !word-swap <message> You can edit the dict at the top to put in words. on('ready',()=>{ const dict = { "some": "other", "sentence": "words" }; const processInlinerolls = (msg) => { if(_.has(msg,'inlinerolls')){ return _.chain(msg.inlinerolls) .reduce(function(m,v,k){ let ti=_.reduce(v.results.rolls,function(m2,v2){ if(_.has(v2,'table')){ m2.push(_.reduce(v2.results,function(m3,v3){ m3.push(v3.tableItem.name); return m3; },[]).join(', ')); } return m2; },[]).join(', '); m['$[['+k+']]']= (ti.length && ti) || v.results.total || 0; return m; },{}) .reduce(function(m,v,k){ return m.replace(k,v); },msg.content) .value(); } else { return msg.content; } }; const doReplace = (txt) => Object.keys(dict).reduce((m,v)=>m.replace(new RegExp(`\\b${v}\\b`),dict[v]),txt); on('chat:message',(msg)=>{ if('api' === msg.type && /^!word-swap\b/i.test(msg.content)){ let player = (getObj('player',msg.playerid)||{get:()=>'API'}); let speakingas = (player.get('speakingas').length ? player.get('speakingas') : `player|${player.id}`); let content = processInlinerolls(msg); content = doReplace(content.replace(/^!word-swap\s*/,'')); sendChat(speakingas, `${msg.rolltemplate ? `&{template:${msg.rolltemplate}}`: ''}${content}`); } }); });
1588774063
The Aaron
Roll20 Production Team
API Scripter
It's not actually as complicated as it looks, I included handling inline rolls, roll templates, and speaking as.  The real work is just the doReplace function and the dict lookup.
Thank you very much, you're an absolute legend.
1588779598
The Aaron
Roll20 Production Team
API Scripter
No problem. =D