Skip to content

Javascript snippets

dc edited this page Jan 26, 2017 · 4 revisions

Some useful code snippets for calling javascript functions

accessing uservars from JS

extracting a specific user variable and returning a string note that rs.getUservars will return ALL variables for ALL users so you need to pass it a specific userId to filter with:

> object whoami javascript
	
  // gets ALL uservars
  // let uservars = rs.getUservars(); 
  //let uid = rs.currentUser();
  
  let uservars = rs.getUservars(rs.currentUser())

  console.log("getgender");
  console.log("uservars", uservars)
  
  var sex = uservars.sex;
  
  switch (sex) {
  	case 1:
    	return "male"
      break;
      
  	case 2:
    	return "female"
      break;
      
    default:
    	return "unknown:" + gender;
  }
  
< object
 



> topic dev-intro inherits shared

  + start
  - gender is <call>whoami</call>
  
< topic

note this is basically the same as inline RiveScript

  + mysex
  * <get sex> == 1 => you're male!
  * <get sex> == 2 => you're female!
  - I don't know if you're a boy or a girl or other!

see here for more docs: https://github.com/aichaos/rivescript-js/blob/master/docs/rivescript.md#string-currentuser-

discussion https://github.com/aichaos/rivescript-js/issues/205#issuecomment-275042516