banAutoChat method
Future<Map>
banAutoChat({})
Implementation
Future<Map> banAutoChat({
required Object chat_id,
required Object target_chat_id,
required List<Map> target_ban_chats,
bool? isUseCache,
Duration? durationCacheExpire,
bool is_ban_members = false,
List<int>? whiteListUserIds,
required int clientId,
FutureOr<void> Function(List<int> gban_user_ids)? onData,
dynamic Function(Map data, int client_id)? onProcces,
}) async {
whiteListUserIds ??= [];
var get_chat_user = await request(
"getChat",
parameters: {
"chat_id": target_chat_id,
},
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
await request(
"sendMessage",
parameters: {
"chat_id": chat_id,
"text": "Process",
},
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
List<int> gban_user_ids = [];
if (get_chat_user["result"] is Map) {
if (get_chat_user["result"]["type"] is String) {
String get_chat_user_type = get_chat_user["result"]["type"] as String;
if (get_chat_user_type == "private") {
gban_user_ids.add(get_chat_user["result"]["id"]);
} else {
if (is_ban_members) {
await request(
"sendMessage",
parameters: {
"chat_id": chat_id,
"text": "Fetch all member\nProcess ini lebih lama",
},
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
var get_ban_member = await getAllMembers(
chat_id: get_chat_user["result"]["id"],
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
if (get_ban_member["user_ids"] is List) {
List ban_member_user_ids = get_ban_member["user_ids"];
for (var i = 0; i < ban_member_user_ids.length; i++) {
var loop_data = ban_member_user_ids[i];
if (!gban_user_ids.contains(loop_data)) {
gban_user_ids.add(loop_data);
}
}
}
} else {
var getChatAdministrators = await request(
"getChatAdministrators",
parameters: {
"chat_id": get_chat_user["result"]["id"],
},
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
List<Map> administrators =
(getChatAdministrators['administrators'] as List).cast<Map>();
late bool isNotOwned = true;
for (var admin_index = 0;
admin_index < administrators.length;
admin_index++) {
try {
Map admin = administrators[admin_index];
if (whiteListUserIds.contains(admin["user_id"])) {
isNotOwned = false;
}
if (isNotOwned) {
if (!gban_user_ids.contains(admin["user_id"])) {
gban_user_ids.add(admin["user_id"]);
}
}
} catch (e) {}
}
}
}
}
}
if (onData != null) {
await onData(gban_user_ids);
}
List failed = [];
for (var chatIndex = 0; chatIndex < target_ban_chats.length; chatIndex++) {
try {
var chat = target_ban_chats[chatIndex];
if (chat["username"] is String) {
var chatUsername = "@${chat["username"]}";
var msg_text =
"start gban: ${chatUsername} ${chatIndex} ${target_ban_chats.length}";
await Future.delayed(Duration(seconds: 2));
await request(
"sendMessage",
parameters: {
"chat_id": chat_id,
"text": msg_text,
},
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
await Future.delayed(Duration(milliseconds: 2));
try {
await request(
"getChat",
parameters: {
"chat_id": chatUsername,
},
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
} catch (e) {
continue;
}
} else {
var chatUsername = "@${chat["id"]}";
var msg_text =
"start gban: ${chatUsername} ${chatIndex} ${target_ban_chats.length}";
await Future.delayed(Duration(seconds: 2));
await request(
"sendMessage",
parameters: {
"chat_id": chat_id,
"text": msg_text,
},
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
await Future.delayed(Duration(milliseconds: 2));
try {
await request(
"getChat",
parameters: {
"chat_id": chatUsername,
},
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
);
} catch (e) {
continue;
}
}
for (var index = 0; index < gban_user_ids.length; index++) {
int gban_user_id = gban_user_ids[index];
await Future.delayed(Duration(milliseconds: 500));
try {
await invoke(
"banChatMember",
parameters: {
"chat_id": chat["id"],
"member_id": {
"@type": "messageSenderUser",
"user_id": gban_user_id,
},
},
clientId: clientId,
isUseCache: isUseCache,
durationCacheExpire: durationCacheExpire,
isVoid: true,
);
} catch (e) {
if (e is Map) {
if (e["parameters"] is Map &&
e["parameters"]["retry_after"] is int) {
failed.add(gban_user_id);
} else {
if (e["message"] != "PARTICIPANT_ID_INVALID") {}
}
} else {}
}
}
} catch (e) {
print(e);
}
}
return {
"@type": "bannedChats",
"data_succes": [],
"data_failed": failed,
};
}