> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-angular-v5-docs-update.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Ban/Kick Member From A Group

> Ban/Kick Member From A Group — CometChat documentation.

There are certain actions that can be performed on the group members:

1. Kick a member from the group
2. Ban a member from the group
3. Unban a member from the group
4. Update the scope of the member of the group

All of the above actions can only be performed by the **Admin** or the **Moderator** of the group.

## Kick a Group Member

The Admin or Moderator of a group can kick a member out of the group using the `kickGroupMember()` method.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    private String UID = "UID";
    private String GUID = "GUID";

    CometChat.kickGroupMember(UID, GUID, new CometChat.CallbackListener<String>() {
      @Override
      public void onSuccess(String successMessage) {
        Log.d(TAG, "Group member kicked successfully");
      }

      @Override
      public void onError(CometChatException e) {
        Log.d(TAG, "Group member kicking failed with exception: " + e.getMessage());
      }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val GUID:String="GUID"
    val UID:String="UID"

    CometChat.kickGroupMember(UID,GUID,object :CometChat.CallbackListener<String>(){
      override fun onSuccess(p0: String?) {
        Log.d(TAG, "Group member kicked successfully")
      }

      override fun onError(p0: CometChatException?) {
        Log.d(TAG, "Group member kicking failed with exception: " + p0?.message)
      }
    })
    ```
  </Tab>
</Tabs>

The `kickGroupMember()` takes following parameters:

| Parameter | Description                                           |
| --------- | ----------------------------------------------------- |
| `UID`     | The UID of the user to be kicked                      |
| `GUID`    | The GUID of the group from which user is to be kicked |

The kicked user will be no longer part of the group and can not perform any actions in the group, but the kicked user can rejoin the group.

## Ban a Group Member

The Admin or Moderator of the group can ban a member from the group using the `banGroupMember()` method.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    private String UID = "UID";
    private String GUID = "GUID";

    CometChat.banGroupMember(UID, GUID, new CometChat.CallbackListener<String>() {
      @Override
      public void onSuccess(String successMessage) {
        Log.d(TAG, "Group member banned successfully");
      }

      @Override
      public void onError(CometChatException e) {
        Log.d(TAG, "Group member banning failed with exception: " + e.getMessage());
      }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val GUID:String="GUID"
    val UID:String="UID"

    CometChat.banGroupMember(UID,GUID,object:CometChat.CallbackListener<String>(){
      override fun onSuccess(p0: String?) {
        Log.d(TAG, "Group member banned successfully")
      }

      override fun onError(p0: CometChatException?) {
        Log.d(TAG, "Group member banning failed with exception: " + p0?.message)
      }
    })
    ```
  </Tab>
</Tabs>

The `banGroupMember()` method takes the following parameters:

| Parameter | Description                                             |
| --------- | ------------------------------------------------------- |
| `UID`     | The `UID` of the user to be banned                      |
| `GUID`    | The `GUID` of the group from which user is to be banned |

The banned user will be no longer part of the group and can not perform any actions in the group. A banned user cannot rejoin the group same group without being unbanned.

## Unban a Banned Group Member from a Group

Only Admin or Moderators of the group can unban a previously banned member from the group using the `unbanGroupMember()` method.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    private String UID = "UID";
    private String GUID = "GUID";

    CometChat.unbanGroupMember(UID, GUID, new CometChat.CallbackListener<String>() {
      @Override
      public void onSuccess(String successMessage) {
        Log.d(TAG, "Group Member unbanned successfully");
      }

      @Override
      public void onError(CometChatException e) {
        Log.d(TAG, "Group Member unbanning failed with exception: " + e.getMessage());
      }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val GUID:String="GUID"
    val UID:String="UID"

    CometChat.unbanGroupMember(UID,GUID,object :CometChat.CallbackListener<String>(){
      override fun onSuccess(p0: String?) {
        Log.d(TAG, "Group Member unbanned successfully")
      }

      override fun onError(p0: CometChatException?) {
        Log.d(TAG, "Group Member unbanning failed with exception: " + p0?.message)
      }
    })
    ```
  </Tab>
</Tabs>

The `unbanGroupMember()` method takes the following parameters

| Parameter | Description                                          |
| --------- | ---------------------------------------------------- |
| `UID`     | The UID of the user to be unbanned                   |
| `GUID`    | The UID of the group from which user is to be banned |

The unbanned user can now rejoin the group.

## Get List of Banned Members for a Group

In order to fetch the list of banned groups members for a group, you can use the `BannedGroupMembersRequest` class. To use this class i.e to create an object of the BannedGroupMembersRequest class, you need to use the `BannedGroupMembersRequestBuilder` class. The `BannedGroupMembersRequestBuilder` class allows you to set the parameters based on which the banned group members are to be fetched.

The `BannedGroupMembersRequestBuilder` class allows you to set the below parameters:

The `GUID` of the group for which the banned members are to be fetched must be specified in the constructor of the `GroupMembersRequestBuilder` class.

### Set Limit

This method sets the limit i.e. the number of banned members that should be fetched in a single iteration.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    BannedGroupMembersRequest bannedGroupMembersRequest = new BannedGroupMembersRequest.BannedGroupMembersRequestBuilder(GUID)
      .setLimit(limit)
      .build();
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val bannedGroupMembersRequest = BannedGroupMembersRequestBuilder(GUID)
      .setLimit(limit)
      .build()
    ```
  </Tab>
</Tabs>

### Set Search Keyword

This method allows you to set the search string based on which the banned group members are to be fetched.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    BannedGroupMembersRequest bannedGroupMembersRequest = new BannedGroupMembersRequest.BannedGroupMembersRequestBuilder(GUID)
      .setSearchKeyword("abc")
      .build();
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val bannedGroupMembersRequest = BannedGroupMembersRequestBuilder(GUID)
      .setSearchKeyword("CometChat")
      .build()
    ```
  </Tab>
</Tabs>

Finally, once all the parameters are set to the builder class, you need to call the `build()` method to get the object of the `BannedGroupMembersRequest` class.

Once you have the object of the `BannedGroupMembersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `GroupMember` objects containing n number of banned members where n is the limit set in the builder class.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    private BannedGroupMembersRequest bannedGroupMembersRequest = null;
    private String GUID = "GUID";
    private int limit = 30;

    bannedGroupMembersRequest = new BannedGroupMembersRequest.BannedGroupMembersRequestBuilder(GUID).setLimit(limit).build();

    bannedGroupMembersRequest.fetchNext(new CometChat.CallbackListener<List<GroupMember>>(){
      @Override
      public void onSuccess(List <GroupMember> list) {
        Log.d(TAG, "Banned Group Member list fetched successfully: " + list.size());
      }

      @Override
      public void onError(CometChatException e) {
        Log.d(TAG, "Banned Group Member list fetching failed with exception: " + e.getMessage());
      }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    var bannedGroupMembersRequest:BannedGroupMembersRequest?=null
    val GUID:String="GUID"
    val limit:Int=30

    bannedGroupMembersRequest=BannedGroupMembersRequest.BannedGroupMembersRequestBuilder(GUID).setLimit(limit).build()

    bannedGroupMembersRequest?.fetchNext(object :CometChat.CallbackListener<List<GroupMember>>(){
      override fun onSuccess(p0: List<GroupMember>?) {
        Log.d(TAG, "Banned Group Member list fetched successfully: " + p0?.size)
      }
      override fun onError(p0: CometChatException?) {
        Log.d(TAG, "Banned Group Member list fetching failed with exception: " + p0?.message)
      }
    })
    ```
  </Tab>
</Tabs>

## Real-Time Group Member Kicked/Banned Events

*In other words, as a member of a group, how do I know when someone is banned/kicked when my app is running?*

In order to get real-time events for the kick/ban/unban group members you need to override the following methods of the `GroupListener` class.

1. `onGroupMemberKicked()` - Triggered when any group member has been kicked.
2. `onGroupMemberBanned()` - Triggered when any group member has been banned.
3. `onGroupMemberUnbanned()` - Triggered when any group member has been unbanned.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    CometChat.addGroupListener(GroupChatActivity.class.getSimpleName(), new CometChat.GroupListener() {
      @Override
      public void onGroupMemberKicked(Action action, User kickedUser, User kickedBy, Group kickedFrom) {

      }

      @Override
      public void onGroupMemberBanned(Action action, User bannedUser, User bannedBy, Group bannedFrom) {

      }

      @Override
      public void onGroupMemberUnbanned(Action action, User unbannedUser, User unbannedBy, Group unbannedFrom) {

      }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    CometChat.addGroupListener(MainActivity::class.java.simpleName, object : GroupListener() {
      override fun onGroupMemberKicked(action: Action?, kickedUser: User?, kickedBy: User?, kickedFrom: Group?) {}

      override fun onGroupMemberBanned(action: Action?, bannedUser: User?, bannedBy: User?, bannedFrom: Group?) {}
      
      override fun onGroupMemberUnbanned(action: Action?, unbannedUser: User?, unbannedBy: User?, unbannedFrom: Group?) {}
    })
    ```
  </Tab>
</Tabs>

## Missed Group Member Kicked/Banned Events

*In other words, as a member of a group, how do I know when someone is banned/kicked when my app is not running?*

When you retrieve the list of previous messages if a member has been kicked/banned/unbanned from any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class.

For group member kicked event, the details can be obtained using the below fields of the `Action` class-

1. `action` - `kicked`
2. `actionBy` - User object containing the details of the user who has kicked the member
3. `actionOn` - User object containing the details of the member that has been kicked
4. `actionFor` - Group object containing the details of the Group from which the member was kicked

For group member banned event, the details can be obtained using the below fields of the `Action` class-

1. `action` - `banned`
2. `actionBy` - User object containing the details of the user who has banned the member
3. `actionOn` - User object containing the details of the member that has been banned
4. `actionFor` - Group object containing the details of the Group from which the member was banned

For group member unbanned event, the details can be obtained using the below fields of the `Action` class-

1. `action` - `unbanned`
2. `actionBy` - User object containing the details of the user who has unbanned the member
3. `actionOn` - User object containing the details of the member that has been unbanned
4. `actionFor` - Group object containing the details of the Group from which the member was unbanned
