How To Tell Who Is At Least A Moderator In A Space

Here’s an example of how to use spaceSetsSpaceMembers to parse out who is at least a moderator in a space:

  const modOrGreaterUids: string[] = [];
  game.subscribeToEvent("spaceSetsSpaceMembers", ({ spaceSetsSpaceMembers }) => {
    for (const [uid, member] of Object.entries(spaceSetsSpaceMembers.members)) {
      for (const [roleName, roleValue] of Object.entries(member.roles)) {
        if (
          roleName === "DEFAULT_MOD" ||
          (roleName === "OWNER" && roleValue && !modOrGreaterUids.includes(uid))
        ) {
          console.log("Found mod ", uid);
          modOrGreaterUids.push(uid);
        }
      }
    }
  });

In subsequent events/handlers, use the data (varies by event) or context.playerId (available if present) on the event to check if it’s in that list, and you should be able to tell if a user is at least a moderator.

The builder role is called DEFAULT_BUILDER.

1 Like

Just wanted to add this here, for those who do not find spaceSetsSpaceMembers works for their space.

Another way of getting a list of mods, owners, and builders uids:

const owners = new Set;
const mods = new Set;
const builders = new Set;
game.subscribeToEvent('spaceOverwrites',(data) => {
    var members = JSON.parse(data.spaceOverwrites.spaceData).members;
    for(const uuid in members){
        if(members[uuid].roles.OWNER){owners.add(uuid);}
        if(members[uuid].roles.DEFAULT_MOD){mods.add(uuid);}
        if(members[uuid].roles.DEFAULT_BUILDER){builders.add(uuid);}
    }
})

Trick here is to set this up BEFORE calling game.connect. Then, you can use .has() on mods, owners, or builders to check if a user id matches the required permissions.

2 Likes

Thanks for this! In looking through the server side code I can see why this behavior occurs - I’ve filed a ticket to get this looked at. It shouldn’t be this difficult to tell who is in what role, so hopefully we can simplify this code for you soon :slight_smile: