Skip to content

Commit

Permalink
Add bulkBan to GuildManager (#640)
Browse files Browse the repository at this point in the history
* add `bulkBan` to `GuildManager`

* add `.toList()` to `user_ids`

* make `bannedUsers`/`failedUsers` not-null, after spec update

* apply abito suggestion

Co-authored-by: Abitofevrything <[email protected]>

* export BulkBanResponse

---------

Co-authored-by: Abitofevrything <[email protected]>
  • Loading branch information
MCausc78 and abitofevrything authored Mar 29, 2024
1 parent 10181ac commit 6adc5ff
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/nyxx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
26 changes: 26 additions & 0 deletions lib/src/http/managers/guild_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ class GuildManager extends Manager<Guild> {
);
}

/// Parse a [BulkBanResponse] from [raw].
BulkBanResponse parseBulkBanResponse(Map<String, Object?> 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<String, Object?> raw) {
return WidgetSettings(
Expand Down Expand Up @@ -485,6 +493,24 @@ class GuildManager extends Manager<Guild> {
await client.httpHandler.executeSafe(request);
}

/// Ban up to 200 users from a guild, and optionally delete previous messages sent by the banned users.
Future<BulkBanResponse> bulkBan(Snowflake id, List<Snowflake> 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<String, Object?>);
}

/// Delete a ban in a guild.
Future<void> deleteBan(Snowflake id, Snowflake userId, {String? auditLogReason}) async {
final route = HttpRoute()
Expand Down
3 changes: 3 additions & 0 deletions lib/src/http/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
12 changes: 12 additions & 0 deletions lib/src/models/guild/ban.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<Snowflake> bannedUsers;

/// A list of user IDs, that were not banned.
final List<Snowflake> failedUsers;

/// @nodoc
BulkBanResponse({required this.bannedUsers, required this.failedUsers});
}
8 changes: 6 additions & 2 deletions lib/src/models/guild/guild.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,15 @@ class PartialGuild extends WritableSnowflakeEntity<Guild> {
/// List the bans in this guild.
Future<List<Ban>> 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<void> 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<BulkBanResponse> bulkBan(List<Snowflake> userIds, {Duration? deleteMessages, String? auditLogReason}) =>
manager.bulkBan(id, userIds, deleteMessages: deleteMessages, auditLogReason: auditLogReason);

/// Unban a user in this guild.
Future<void> deleteBan(Snowflake userId, {String? auditLogReason}) => manager.deleteBan(id, userId, auditLogReason: auditLogReason);

/// Update a guild's MFA level.
Expand Down

0 comments on commit 6adc5ff

Please sign in to comment.