Skip to content

Instantly share code, notes, and snippets.

@nolivo
Created July 7, 2020 15:57
Show Gist options
  • Save nolivo/a8806398d4fb71bc5d5eaa2df2a96a34 to your computer and use it in GitHub Desktop.
Save nolivo/a8806398d4fb71bc5d5eaa2df2a96a34 to your computer and use it in GitHub Desktop.
on("chat:message",function(msg){
if(msg.type=="api" && msg.content=="!partyMover"){
buildPartyMoverMacro();
}
});
on("change:page:name",function(){
buildPartyMoverMacro();
sendChat("API","!mc refresh");
})
function buildPartyMoverMacro(){
var pageList = findObjs({type:"page"});
var pageNames = _.reduce(pageList,function(pageNames,pageName){return pageNames + pageName.get("name")+"|"},"");
log(pageNames);
pageNames = pageNames.split("|").sort().join("|");
log(pageNames);
var partyMoverMacro = findObjs({type:"macro",name:"partyMover"})[0];
var macroAction = `!mc moveall --target ?{pick|${pageNames}}`;
if (partyMoverMacro === undefined){
createObj("macro",{
name:"partyMover",
action:macroAction,
playerid:findGMID()
});
}
else{
partyMoverMacro.set("action",macroAction);
}
sendChat("API","/w gm PartyMoverMacro was updated");
}
function findGMID(){
var playerList = findObjs({type:"player"});
var gmInfo = _.filter(playerList,function(player){return playerIsGM(player.id)});
return gmInfo[0].get("_id");
}
@rpgRackNar
Copy link

 I have updated the code to more modern javascript. The _ library is no longer needed. Also empty entries are filtered out now. I hope you have fun with it.

on("chat:message", (msg) => {
  if (msg.type == "api" && msg.content == "!partyMover") {
    buildPartyMoverMacro();
  }
});

on("change:page:name", () => {
  buildPartyMoverMacro();
  sendChat("API", "!mc refresh");
});

function buildPartyMoverMacro() {
  const pageList = findObjs({ type: "page" });
  const pageNames = pageList
    .map((page) => page.get("name"))
    .filter((pageName) => !!pageName) // remove empty entries
    .sort()
    .join("|");

  log(pageNames);

  const partyMoverMacro = findObjs({ type: "macro", name: "partyMover" })[0];
  const macroAction = `!mc moveall --target ?{pick|${pageNames}}`;

  if (partyMoverMacro === undefined) {
    createObj("macro", {
      name: "partyMover",
      action: macroAction,
      playerid: findGMID(),
    });
  } else {
    partyMoverMacro.set("action", macroAction);
  }

  sendChat("API", "/w gm PartyMoverMacro was updated");
}

function findGMID() {
  const playerList = findObjs({ type: "player" });
  const gmInfo = playerList.find((player) => playerIsGM(player.id)); // Here it would actually need an error handling. But I think there will always be a GM
  return gmInfo.get("_id");
}

@justbill2020
Copy link

justbill2020 commented Apr 14, 2021

@nolivo and @rpgRackNar I had the thought... if we take this and make the macro run the api script each time we won't really ever need to run the sript itself. thoughts???

on("chat:message", (msg) => {
  if (msg.type == "api" && msg.content == "!partyMover") {
    buildPartyMoverMacro();
  }
});

on("change:page:name", () => {
  buildPartyMoverMacro();
  sendChat("API", "!mc refresh");
});

function buildPartyMoverMacro() {
  const pageList = findObjs({ type: "page" });
  const pageNames = pageList
    .map((page) => page.get("name"))
    .filter((pageName) => !!pageName) // remove empty entries
    .sort()
    .join("|");

  log(pageNames);

  const partyMoverMacro = findObjs({ type: "macro", name: "partyMover" })[0];
  const macroAction = `!partyMover\n!mc moveall --target ?{pick|${pageNames}}`; // this calls this script first uses \n to insert a new line

  if (partyMoverMacro === undefined) {
    createObj("macro", {
      name: "partyMover",
      action: macroAction,
      playerid: findGMID(),
    });
  } else {
    partyMoverMacro.set("action", macroAction);
  }

  sendChat("API", "/w gm PartyMoverMacro was updated");
}

function findGMID() {
  const playerList = findObjs({ type: "player" });
  const gmInfo = playerList.find((player) => playerIsGM(player.id)); // Here it would actually need an error handling. But I think there will always be a GM
  return gmInfo.get("_id");
}

@nolivo
Copy link
Author

nolivo commented Apr 14, 2021

That might work. Go for it.

@nolivo and @rpgRackNar I had the thought... if we take this and make the macro run the api script each time we won't really ever need to run the sript itself. thoughts???

on("chat:message", (msg) => {
  if (msg.type == "api" && msg.content == "!partyMover") {
    buildPartyMoverMacro();
  }
});

on("change:page:name", () => {
  buildPartyMoverMacro();
  sendChat("API", "!mc refresh");
});

function buildPartyMoverMacro() {
  const pageList = findObjs({ type: "page" });
  const pageNames = pageList
    .map((page) => page.get("name"))
    .filter((pageName) => !!pageName) // remove empty entries
    .sort()
    .join("|");

  log(pageNames);

  const partyMoverMacro = findObjs({ type: "macro", name: "partyMover" })[0];
  const macroAction = `!partyMover\n!mc moveall --target ?{pick|${pageNames}}`; // this calls this script first uses \n to insert a new line

  if (partyMoverMacro === undefined) {
    createObj("macro", {
      name: "partyMover",
      action: macroAction,
      playerid: findGMID(),
    });
  } else {
    partyMoverMacro.set("action", macroAction);
  }

  sendChat("API", "/w gm PartyMoverMacro was updated");
}

function findGMID() {
  const playerList = findObjs({ type: "player" });
  const gmInfo = playerList.find((player) => playerIsGM(player.id)); // Here it would actually need an error handling. But I think there will always be a GM
  return gmInfo.get("_id");
}

@CarstenGoeltenboth
Copy link

CarstenGoeltenboth commented May 29, 2021

Added 2 small changes to exclude archived pages.

on("chat:message", (msg) => {
  if (msg.type == "api" && msg.content == "!partyMover") {
    buildPartyMoverMacro();
  }
});

on("change:page:name", () => {
  buildPartyMoverMacro();
  sendChat("API", "!mc refresh");
});

on("change:page:archived", () => {
  buildPartyMoverMacro();
  sendChat("API", "!mc refresh");
});

function buildPartyMoverMacro() {
  const pageList = findObjs({ type: "page", archived: false});
  const pageNames = pageList
    .map((page) => page.get("name"))
    .filter((pageName) => !!pageName) // remove empty entries
    .sort()
    .join("|");

  log(pageNames);

  const partyMoverMacro = findObjs({ type: "macro", name: "partyMover" })[0];
  const macroAction = `!mc moveall --target ?{pick|${pageNames}}`;

  if (partyMoverMacro === undefined) {
    createObj("macro", {
      name: "partyMover",
      action: macroAction,
      playerid: findGMID(),
    });
  } else {
    partyMoverMacro.set("action", macroAction);
  }

  sendChat("API", "/w gm PartyMoverMacro was updated");
}

function findGMID() {
  const playerList = findObjs({ type: "player" });
  const gmInfo = playerList.find((player) => playerIsGM(player.id)); // Here it would actually need an error handling. But I think there will always be a GM
  return gmInfo.get("_id");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment