# Managing player groups

Player groups are a way to organize players into groups, and then assign permissions to those groups. This way, you can easily manage permissions for multiple players at once.

# Attaching player to a group Async

To attach a player to a group, you can use the useAccount, useCharacter and useVirtual functions. All of them have a groups property that you can use to attach a player to a group.

import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

async function makeAdmin(player: alt.Player) {
  const character = Rebar.document.character.useCharacter(player);
  await character.groups.add('admin');
}
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

async function makeAdmin(player: alt.Player) {
  const account = Rebar.document.account.useAccount('account-id');
  await account.groups.add('admin');
}
import {useRebar} from '@Server/index.js';

const Rebar = useRebar();
const {CollectionNames} = Rebar.database;

async function makeAdmin(_id: string) {
    const document = await Rebar.document.virtual.useVirtual(_id, CollectionNames.Accounts);
    if (document) {
        await virtual.groups.add('admin');
    }
}

# Removing player from a group Async

To remove a player from a group, you can use the useAccount, useCharacter and useVirtual functions. All of them have a groups property that you can use to remove a player from a group.

import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

async function removeAdmin(player: alt.Player) {
  const character = Rebar.document.character.useCharacter(player);
  await character.groups.remove('admin');
}
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

async function removeAdmin(player: alt.Player) {
  const account = Rebar.document.account.useAccount('account-id');
  await account.groups.remove('admin');
}
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();
const { CollectionNames } = Rebar.database;

async function removeAdmin(_id: string) {
  const document = await Rebar.document.useVirtual(_id, CollectionNames.Accounts);
  if (document) {
      await virtual.groups.remove('admin');
  }
}

# Clearing all groups Async

To clear all groups from a player, you can use the useAccount, useCharacter and useVirtual functions. All of them have a groups property that you can use to clear all groups from a player.

import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

async function clearGroups(player: alt.Player) {
  const character = Rebar.document.character.useCharacter(player);
  await character.groups.clear();
}
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

async function clearGroups(player: alt.Player) {
  const account = Rebar.document.account.useAccount('account-id');
  await account.groups.clear();
}
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

async function clearGroups(_id: string) {
  const document = await Rebar.document.useVirtual(_id, CollectionNames.Accounts);
  if (document) {
      await virtual.groups.clear();
  }
}

# Getting a list of groups

To get a list of groups that a player is in, you can use the useAccount, useCharacter and useVirtual functions. All of them have a groups property that you can use to get a list of groups.

import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

function getGroups(player: alt.Player) {
  const character = Rebar.document.character.useCharacter(player);
  return character.groups.get();
}
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

function getGroups(player: alt.Player) {
  const account = Rebar.document.account.useAccount('account-id');
  return account.groups.get();
}
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();
const { CollectionNames } = Rebar.database;

async function getGroups(_id: string) {
  const document = await Rebar.document.useVirtual(_id, CollectionNames.Accounts);
  if (document) {
      return virtual.groups.get();
  }
  return undefined;
}

# Check group membership

To check if a player is a member of a group, you can use the useAccount, useCharacter and useVirtual functions. All of them have a groups property that you can use to check if a player is a member of a group.

import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

function isMemberOfAdminGroup(player: alt.Player) {
  const character = Rebar.document.character.useCharacter(player);
  return character.groups.memberOf('admin');
}
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();

function isMemberOfAdminGroup(player: alt.Player) {
  const account = Rebar.document.account.useAccount('account-id');
  return account.groups.memberOf('admin');
}
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();
const { CollectionNames } = Rebar.database;

async function isMemberOfAdminGroup(_id: string) {
  const document = await Rebar.document.useVirtual(_id, CollectionNames.Accounts);
  if (document) {
      return virtual.groups.memberOf('admin');
  }
  return false;
}