Skip to content

Commit

Permalink
Implement gdm control endpoints (#600)
Browse files Browse the repository at this point in the history
* implement oauth2 gdm control

* add `threadName`/`appliedTags` params to `WebhookManager.execute`

* add DmRecipientBuilder
  • Loading branch information
MCausc78 authored Dec 11, 2023
1 parent b082750 commit 6ed9657
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
14 changes: 14 additions & 0 deletions lib/src/builders/channel/group_dm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ class GroupDmUpdateBuilder extends UpdateBuilder<GroupDmChannel> {
if (icon != null) 'icon': base64Encode(icon!),
};
}

class DmRecipientBuilder extends CreateBuilder<DmRecipientBuilder> {
String accessToken;

String nick;

DmRecipientBuilder({required this.accessToken, required this.nick});

@override
Map<String, Object?> build() => {
'access_token': accessToken,
'nick': nick,
};
}
21 changes: 20 additions & 1 deletion lib/src/http/managers/channel_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:http/http.dart' show MultipartFile;
import 'package:nyxx/src/builders/builder.dart';
import 'package:nyxx/src/builders/channel/group_dm.dart';
import 'package:nyxx/src/builders/channel/stage_instance.dart';
import 'package:nyxx/src/builders/channel/thread.dart';
import 'package:nyxx/src/builders/invite.dart';
Expand Down Expand Up @@ -607,7 +608,7 @@ class ChannelManager extends ReadOnlyManager<Channel> {

request = MultipartRequest(
route,
method: 'PATCH',
method: 'POST',
jsonPayload: jsonEncode(payload),
files: files,
);
Expand Down Expand Up @@ -825,4 +826,22 @@ class ChannelManager extends ReadOnlyManager<Channel> {

stageInstanceCache.remove(channelId);
}

Future<void> addRecipient(Snowflake channelId, Snowflake userId, DmRecipientBuilder builder) async {
final route = HttpRoute()
..channels(id: channelId.toString())
..recipients(id: userId.toString());
final request = BasicRequest(route, method: 'PUT', body: jsonEncode(builder.build()));

await client.httpHandler.executeSafe(request);
}

Future<void> removeRecipient(Snowflake channelId, Snowflake userId) async {
final route = HttpRoute()
..channels(id: channelId.toString())
..recipients(id: userId.toString());
final request = BasicRequest(route, method: 'DELETE');

await client.httpHandler.executeSafe(request);
}
}
15 changes: 12 additions & 3 deletions lib/src/http/managers/webhook_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ class WebhookManager extends Manager<Webhook> {
}

/// Execute a webhook.
Future<Message?> execute(Snowflake id, MessageBuilder builder, {required String token, bool? wait, Snowflake? threadId}) async {
Future<Message?> execute(Snowflake id, MessageBuilder builder,
{required String token, bool? wait, Snowflake? threadId, String? threadName, List<Snowflake>? appliedTags}) async {
final route = HttpRoute()
..webhooks(id: id.toString())
..add(HttpRoutePart(token));
Expand All @@ -160,7 +161,11 @@ class WebhookManager extends Manager<Webhook> {
final HttpRequest request;
if (!identical(builder.attachments, sentinelList) && builder.attachments?.isNotEmpty == true) {
final attachments = builder.attachments!;
final payload = builder.build();
final payload = {
...builder.build(),
if (threadName != null) 'thread_name': threadName,
if (appliedTags != null) 'applied_tags': appliedTags.map((e) => e.toString()),
};

final files = <MultipartFile>[];
for (int i = 0; i < attachments.length; i++) {
Expand All @@ -185,7 +190,11 @@ class WebhookManager extends Manager<Webhook> {
request = BasicRequest(
route,
method: 'POST',
body: jsonEncode(builder.build()),
body: jsonEncode({
...builder.build(),
if (threadName != null) 'thread_name': threadName,
if (appliedTags != null) 'applied_tags': appliedTags.map((e) => e.toString()),
}),
queryParameters: queryParameters,
authenticated: false,
);
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 @@ -311,4 +311,7 @@ extension RouteHelpers on HttpRoute {

/// Adds the [`avatar-decorations`](https://discord.com/developers/docs/reference#image-formatting-cdn-endpoints) part to this [HttpRoute].
void avatarDecorations({String? id}) => add(HttpRoutePart('avatar-decorations', [if (id != null) HttpRouteParam(id)]));

/// 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)]));
}

0 comments on commit 6ed9657

Please sign in to comment.