diff --git a/lib/nyxx.dart b/lib/nyxx.dart index 4119dc7f8..dae1d438b 100644 --- a/lib/nyxx.dart +++ b/lib/nyxx.dart @@ -164,7 +164,7 @@ export 'src/models/message/component.dart' export 'src/models/invite/invite.dart' show Invite, TargetType; export 'src/models/invite/invite_metadata.dart' show InviteWithMetadata; export 'src/models/webhook.dart' show PartialWebhook, Webhook, WebhookType, WebhookAuthor; -export 'src/models/guild/ban.dart' show Ban; +export 'src/models/guild/ban.dart' show Ban, BulkBanResponse; export 'src/models/guild/guild_preview.dart' show GuildPreview; export 'src/models/guild/guild_widget.dart' show GuildWidget, WidgetSettings, WidgetImageStyle; export 'src/models/guild/guild.dart' diff --git a/lib/src/http/managers/guild_manager.dart b/lib/src/http/managers/guild_manager.dart index 26480caed..20a75f981 100644 --- a/lib/src/http/managers/guild_manager.dart +++ b/lib/src/http/managers/guild_manager.dart @@ -210,6 +210,14 @@ class GuildManager extends Manager { ); } + /// Parse a [BulkBanResponse] from [raw]. + BulkBanResponse parseBulkBanResponse(Map raw) { + return BulkBanResponse( + bannedUsers: parseMany(raw['banned_users'] as List, Snowflake.parse), + failedUsers: parseMany(raw['failed_users'] as List, Snowflake.parse), + ); + } + /// Parse a [WidgetSettings] from [raw]. WidgetSettings parseWidgetSettings(Map raw) { return WidgetSettings( @@ -485,6 +493,24 @@ class GuildManager extends Manager { await client.httpHandler.executeSafe(request); } + /// Ban up to 200 users from a guild, and optionally delete previous messages sent by the banned users. + Future bulkBan(Snowflake id, List userIds, {Duration? deleteMessages, String? auditLogReason}) async { + final route = HttpRoute() + ..guilds(id: id.toString()) + ..bulkBan(); + final request = BasicRequest( + route, + method: 'POST', + auditLogReason: auditLogReason, + body: jsonEncode({ + 'user_ids': userIds.map((s) => s.toString()).toList(), + if (deleteMessages != null) 'delete_message_seconds': deleteMessages.inSeconds, + }), + ); + final response = await client.httpHandler.executeSafe(request); + return parseBulkBanResponse(response.jsonBody as Map); + } + /// Delete a ban in a guild. Future deleteBan(Snowflake id, Snowflake userId, {String? auditLogReason}) async { final route = HttpRoute() diff --git a/lib/src/http/route.dart b/lib/src/http/route.dart index 751f79af8..e5bc5ef78 100644 --- a/lib/src/http/route.dart +++ b/lib/src/http/route.dart @@ -314,4 +314,7 @@ extension RouteHelpers on HttpRoute { /// Adds the [`recipients`](https://discord.com/developers/docs/resources/channel#group-dm-add-recipient) part to this [HttpRoute]. void recipients({String? id}) => add(HttpRoutePart('recipients', [if (id != null) HttpRouteParam(id)])); + + /// Adds the [`bulk-ban`](https://discord.com/developers/docs/resources/guild#bulk-guild-ban) part to this [HttpRoute]. + void bulkBan() => add(HttpRoutePart('bulk-ban')); } diff --git a/lib/src/models/guild/ban.dart b/lib/src/models/guild/ban.dart index c2d1e1886..442abae57 100644 --- a/lib/src/models/guild/ban.dart +++ b/lib/src/models/guild/ban.dart @@ -1,3 +1,4 @@ +import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/user/user.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; @@ -15,3 +16,14 @@ class Ban with ToStringHelper { /// @nodoc Ban({required this.reason, required this.user}); } + +class BulkBanResponse with ToStringHelper { + /// A list of user IDs, that were succesfully banned. + final List bannedUsers; + + /// A list of user IDs, that were not banned. + final List failedUsers; + + /// @nodoc + BulkBanResponse({required this.bannedUsers, required this.failedUsers}); +} diff --git a/lib/src/models/guild/guild.dart b/lib/src/models/guild/guild.dart index 3a4c18fc3..37dea7ef8 100644 --- a/lib/src/models/guild/guild.dart +++ b/lib/src/models/guild/guild.dart @@ -111,11 +111,15 @@ class PartialGuild extends WritableSnowflakeEntity { /// List the bans in this guild. Future> listBans({int? limit, Snowflake? after, Snowflake? before}) => manager.listBans(id, limit: limit, after: after, before: before); - /// Ban a member in this guild. + /// Ban a user in this guild. Future createBan(Snowflake userId, {Duration? deleteMessages, String? auditLogReason}) => manager.createBan(id, userId, auditLogReason: auditLogReason, deleteMessages: deleteMessages); - /// Unban a member in this guild. + /// Ban up to 200 users from a guild, and optionally delete previous messages sent by the banned users. + Future bulkBan(List userIds, {Duration? deleteMessages, String? auditLogReason}) => + manager.bulkBan(id, userIds, deleteMessages: deleteMessages, auditLogReason: auditLogReason); + + /// Unban a user in this guild. Future deleteBan(Snowflake userId, {String? auditLogReason}) => manager.deleteBan(id, userId, auditLogReason: auditLogReason); /// Update a guild's MFA level.