Ban/Kick Members from a Group

Learn how to ban and kick members from a group.

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

Please note: All the above-mentioned actions can only be performed by the Admin or the Moderator of the group.

Kick a Group Member

The Admin or Moderator of the group can kick a member out of the group using the kickUser() method provided by the CometChat class. This method can be used as shown below:

let uid  = "UID"; // Uid of the user to be kicked
let guid  = "GUID"; // guid of the group the user is to be kicked from

CometChat.kickGroupMember(UID: uid, GUID: guid, onSuccess: { (response) in

  print("\(uid) is kicked from the group \(guid) successfully.")

}) { (error) in

  print("Group member kicking failed with error:  " + error!.errorDescription);
}
NSString *UID = @"UID"; // Uid of the user to be kicked
NSString *GUID = @"GUID"; // guid of the group the user is to be kicked from

[CometChat kickGroupMemberWithUID:UID GUID:GUID onSuccess:^(NSString * response) {
   NSLog(@"%@ kicked from the group %@ successfully", UID, GUID);
    
} onError:^(CometChatException * error) {
    
    NSLog(@"Group member kicking failed with error: %@",[error errorDescription]);
    
}];

The kickGroupMember() takes the 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.

let uid  = "UID"; 
let guid  = "GUID"  ;

CometChat.banGroupMember(UID: uid, GUID: guid, onSuccess: { (response) in

  print("\(uid) is banned from the group \(guid) successfully.")

}) { (error) in

  print("Group member baning failed with error:  " + error!.errorDescription);
}
NSString *UID = @"UID";
NSString *GUID = @"GUID";

[CometChat banGroupMemberWithUID:UID GUID:GUID onSuccess:^(NSString * response) {
   
   NSLog(@"%@ banned from the group %@ successfully", UID, GUID);
    
} onError:^(CometChatException * error) {
    
    NSLog(@"Group member baning failed with error: %@",[error errorDescription]);
    
}];

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 the 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.

let uid  = "SUPERHERO1"; // Uid of the user to be reinstated
let guid  = "SUPERGROUP1"   ; // guid of the group the user is to be reinstated into

CometChat.unbanGroupMember(UID: uid, GUID: guid, onSuccess: { (response) in

  print("\(uid) is unbanned from the group \(guid) successfully.")

}) { (error) in

  print("Group member unbaning failed with error:  " + error!.errorDescription);
}
NSString *UID = @"UID";
NSString *GUID = @"GUID";

[CometChat unbanGroupMemberWithUID:UID GUID:GUID onSuccess:^(NSString * response) {
    NSLog(@"%@ unbanned from the group %@ successfully", UID, GUID);
    
} onError:^(CometChatException * error) {
    
    NSLog(@"Group member unbaning failed with error: %@",[error errorDescription]);
    
}];

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 BannedGroupMembersRequestBuilder class.

  1. set(limit: Int) - This method sets the limit i.e. the number of banned members that should be fetched in a single iteration.
let bannedGroupMembersRequest = BannedGroupMembersRequest.BannedGroupMembersRequestBuilder(guid: guid).set(limit: limit).build();

  1. set(searchKeyword : String) - This method allows you to set the search string based on which the banned group members are to be fetched.
let bannedGroupMembersRequest = BannedGroupMembersRequest.BannedGroupMembersRequestBuilder(guid: guid)
.set(limit: limit)
.set(searchKeyword: "abc")
.build();

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.

let limit = 10;
let guid = "SUPERGROUP1"

let bannedGroupMembersRequest = BannedGroupMembersRequest.BannedGroupMembersRequestBuilder(guid: guid).set(limit: limit).build();

bannedGroupMembersRequest.fetchNext(onSuccess: { (groupMembers) in

  for bannedMember in groupMembers {

     print("Group Member fetched successfully. " + bannedMember.stringValue())
  }

}) { (error) in

  print("Banned Group Member list fetching failed with error: " + error!.errorDescription);
}
NSString *GUID = @"GUID";
NSInteger limit = 30;

BannedGroupMembersRequest *bannedGroupMemberRequest = [[[[BannedGroupMembersRequestBuilder alloc]initWithGuid:GUID] setLimitWithLimit:limit] build];

[bannedGroupMemberRequest fetchNextOnSuccess:^(NSArray<GroupMember *> * bannedMembers) {
   
    for (GroupMember *member in bannedMembers) {
        
        NSLog(@"Group Member fetched successfully. %@",[member stringValue]);
    }
    
} onError:^(CometChatException * error) {
    
    NSLog(@"Banned Group Member list fetching failed with error: %@",[error ErrorDescription]);
}];

Real-Time Group Member Kicked/Banned Events

In order to receive user Events for kick/ban/unban you must add protocol conformance CometChatGroupDelegate as shown for following methods:

  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.
    methods of the CometChatGroupDelegate.
extension AppDelegate: CometChatGroupDelegate {

  func onGroupMemberKicked(action: ActionMessage, kickedUser: User, kickedBy: User, kickedFrom: Group) {

    print("\(kickedUser.name) kicked from the group \(kickedFrom.name) by \(kickedBy.name).")
  }

  func onGroupMemberBanned(action: ActionMessage, bannedUser: User, bannedBy: User, bannedFrom: Group) {

    print("\(bannedUser.name) banned from the group \(bannedFrom.name) by \(bannedBy.name).")
  }

  func onGroupMemberUnbanned(action: ActionMessage, unbannedUser: User, unbannedBy: User, unbannedFrom: Group) {

    print("\(unbannedUser.name) unbanned from the group \(unbannedFrom.name) by \(unbannedBy.name).")
  }
}
@interface ViewController ()<CometChatGroupDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CometChat setGroupdelegate:self];
    
}
- (void)onGroupMemberBannedWithAction:(Action *)action bannedUser:(User * _Nonnull)bannedUser bannedBy:(User * _Nonnull)bannedBy bannedFrom:(Group * _Nonnull)bannedFrom {
    // user was unbanned
}

- (void)onGroupMemberKickedWithAction:(Action *)action kickedUser:(User * _Nonnull)kickedUser kickedBy:(User * _Nonnull)kickedBy kickedFrom:(Group * _Nonnull)kickedFrom {
    // user was kicked
}

- (void)onGroupMemberUnbannedWithAction:(Action *)action unbannedUser:(User * _Nonnull)unbannedUser unbannedBy:(User * _Nonnull)unbannedBy unbannedFrom:(Group * _Nonnull)unbannedFrom {
    // user was unbanned
}

@end

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

Did this page help you?