Swap Token Bars

A script posted by Aaron to the Roll20 forums, to swap the contents of one token bar with another.

List the bars you want to swap, like so:

!swap-bars 1 3Code language: JavaScript (javascript)

This will swap the values, max values, and the link to an attribute, if they exist.

Here’s the code:

on('ready',()=>{
  on('chat:message',msg=>{
    if('api'===msg.type && /^!swap-bars(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){
      let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');
      let [first,second] = msg.content.split(/\s+/).slice(1,3).map(n=>parseInt(n));

      if([1,2,3].includes(first) && [1,2,3].includes(second)){
        (msg.selected || [])
          .map(o=>getObj('graphic',o._id))
          .filter(g=>undefined !== g)
          .forEach(t=>
            t.set({
              [`bar${second}_value`]: t.get(`bar${first}_value`),
              [`bar${second}_max`]: t.get(`bar${first}_max`),
              [`bar${second}_link`]: t.get(`bar${first}_link`),
              [`bar${first}_value`]: t.get(`bar${second}_value`),
              [`bar${first}_max`]: t.get(`bar${second}_max`),
              [`bar${first}_link`]: t.get(`bar${second}_link`)
          }))
          ;
      } else {
        sendChat('',`/w "${who}" <div>Please use the format <code>!swap-bars NUM NUM</code> where <code>NUM</code> is <code>1</code>, <code>2</code>, or <code>3</code>.</div>`);
      }
    }
  });
});Code language: JavaScript (javascript)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.