Skip to content

Commit

Permalink
Clean up interaction response code; move isEphemeral from MessageBuil…
Browse files Browse the repository at this point in the history
…der to InteractionResponseBuilder (#519)
  • Loading branch information
abitofevrything authored Aug 22, 2023
1 parent bce7eaa commit 1a46a93
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 29 deletions.
49 changes: 47 additions & 2 deletions lib/src/builders/interaction_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@ class InteractionResponseBuilder extends CreateBuilder<InteractionResponseBuilde

factory InteractionResponseBuilder.pong() => InteractionResponseBuilder(type: InteractionCallbackType.pong, data: null);

factory InteractionResponseBuilder.channelMessage(MessageBuilder message) => InteractionResponseBuilder(
factory InteractionResponseBuilder.channelMessage(MessageBuilder message, {bool? isEphemeral}) => InteractionResponseBuilder(
type: InteractionCallbackType.channelMessageWithSource,
data: message,
data: _EphemeralMessageBuilder(
content: message.content,
nonce: message.nonce,
tts: message.tts,
embeds: message.embeds,
allowedMentions: message.allowedMentions,
replyId: message.replyId,
requireReplyToExist: message.requireReplyToExist,
components: message.components,
stickerIds: message.stickerIds,
attachments: message.attachments,
suppressEmbeds: message.suppressEmbeds,
suppressNotifications: message.suppressNotifications,
isEphemeral: isEphemeral,
),
);

factory InteractionResponseBuilder.deferredChannelMessage({bool? isEphemeral}) => InteractionResponseBuilder(
Expand Down Expand Up @@ -56,6 +70,37 @@ class InteractionResponseBuilder extends CreateBuilder<InteractionResponseBuilde
}
}

class _EphemeralMessageBuilder extends MessageBuilder {
final bool? isEphemeral;

_EphemeralMessageBuilder({
required super.content,
required super.nonce,
required super.tts,
required super.embeds,
required super.allowedMentions,
required super.replyId,
required super.requireReplyToExist,
required super.components,
required super.stickerIds,
required super.attachments,
required super.suppressEmbeds,
required super.suppressNotifications,
required this.isEphemeral,
});

@override
Map<String, Object?> build() {
final built = super.build();

if (isEphemeral != null) {
built['flags'] = (built['flags'] as int? ?? 0) | (isEphemeral == true ? MessageFlags.ephemeral.value : 0);
}

return built;
}
}

enum InteractionCallbackType {
pong._(1),
channelMessageWithSource._(4),
Expand Down
18 changes: 4 additions & 14 deletions lib/src/builders/message/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,31 @@ import 'package:nyxx/src/models/message/embed.dart';
import 'package:nyxx/src/models/message/message.dart';
import 'package:nyxx/src/models/snowflake.dart';

class MessageBuilder extends CreateBuilder<Message> implements MessageUpdateBuilder {
@override
class MessageBuilder extends CreateBuilder<Message> {
final String? content;

final dynamic /* int | String */ nonce;

final bool? tts;

@override
final List<Embed>? embeds;

@override
final AllowedMentions? allowedMentions;

final Snowflake? replyId;

final bool? requireReplyToExist;

@override
final List<ActionRowBuilder>? components;

final List<Snowflake>? stickerIds;

@override
final List<AttachmentBuilder>? attachments;

@override
final bool? suppressEmbeds;

final bool? suppressNotifications;

final bool? isEphemeral;

MessageBuilder({
this.content,
this.nonce,
Expand All @@ -53,7 +45,6 @@ class MessageBuilder extends CreateBuilder<Message> implements MessageUpdateBuil
this.attachments,
this.suppressEmbeds,
this.suppressNotifications,
this.isEphemeral,
});

@override
Expand Down Expand Up @@ -135,10 +126,9 @@ class MessageBuilder extends CreateBuilder<Message> implements MessageUpdateBuil
if (components != null) 'components': components!.map((e) => e.build()).toList(),
if (stickerIds != null) 'sticker_ids': stickerIds!.map((e) => e.toString()).toList(),
if (attachments != null) 'attachments': attachments!.map((e) => e.build()).toList(),
if (suppressEmbeds != null || suppressNotifications != null || isEphemeral != null)
'flags': (suppressEmbeds == true ? MessageFlags.suppressEmbeds.value : 0) |
(suppressNotifications == true ? MessageFlags.suppressNotifications.value : 0) |
(isEphemeral == true ? MessageFlags.ephemeral.value : 0),
if (suppressEmbeds != null || suppressNotifications != null)
'flags':
(suppressEmbeds == true ? MessageFlags.suppressEmbeds.value : 0) | (suppressNotifications == true ? MessageFlags.suppressNotifications.value : 0),
};
}
}
Expand Down
33 changes: 20 additions & 13 deletions lib/src/models/interaction.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:nyxx/src/builders/application_command.dart';
import 'package:nyxx/src/builders/builder.dart';
import 'package:nyxx/src/builders/interaction_response.dart';
import 'package:nyxx/src/builders/message/message.dart';
import 'package:nyxx/src/http/managers/interaction_manager.dart';
Expand Down Expand Up @@ -111,19 +112,19 @@ mixin MessageResponse<T> on Interaction<T> {
}

/// Send a response to this interaction.
Future<void> respond(MessageBuilder builder) async {
Future<void> respond(MessageBuilder builder, {bool? isEphemeral}) async {
assert(!_didRespond, 'Can only respond to an interaction once');

if (!_didAcknowledge) {
_didAcknowledge = true;
_didRespond = true;
_wasEphemeral = builder.isEphemeral;
_wasEphemeral = isEphemeral;

await manager.createResponse(id, token, InteractionResponseBuilder.channelMessage(builder));
await manager.createResponse(id, token, InteractionResponseBuilder.channelMessage(builder, isEphemeral: isEphemeral));
} else {
assert(builder.isEphemeral == _wasEphemeral || builder.isEphemeral == null, 'Cannot change the value of isEphemeral between acknowledge and respond');
assert(isEphemeral == _wasEphemeral || isEphemeral == null, 'Cannot change the value of isEphemeral between acknowledge and respond');

await manager.updateOriginalResponse(token, builder);
await manager.createFollowup(token, builder);
}
}

Expand Down Expand Up @@ -261,27 +262,33 @@ class MessageComponentInteraction extends Interaction<MessageComponentInteractio
}

@override
Future<void> respond(MessageBuilder builder, {bool? updateMessage}) async {
Future<void> respond(Builder<Message> builder, {bool? updateMessage, bool? isEphemeral}) async {
assert(updateMessage == null || type == InteractionType.messageComponent, 'Cannot set updateMessage for non-component interactions');
assert(updateMessage != true || builder.isEphemeral != true, 'Cannot set isEphemeral to true if updateMessage is set to true');
assert(updateMessage != true || isEphemeral != true, 'Cannot set isEphemeral to true if updateMessage is set to true');
assert(builder is MessageUpdateBuilder == updateMessage, 'builder must be a MessageUpdateBuilder if updateMessage is true');
assert(builder is MessageBuilder != updateMessage, 'builder must be a MessageBuilder if updateMessage is null or false');

if (!_didRespond) {
assert(updateMessage != true || builder.isEphemeral != true, 'Cannot set isEphemeral to true if updateMessage is set to true');
assert(updateMessage != true || isEphemeral != true, 'Cannot set isEphemeral to true if updateMessage is set to true');

_didRespond = true;
_didUpdateMessage = updateMessage;
_wasEphemeral = builder.isEphemeral;
_wasEphemeral = isEphemeral;

if (updateMessage == true) {
await manager.createResponse(id, token, InteractionResponseBuilder.updateMessage(builder));
await manager.createResponse(id, token, InteractionResponseBuilder.updateMessage(builder as MessageUpdateBuilder));
} else {
await manager.createResponse(id, token, InteractionResponseBuilder.channelMessage(builder));
await manager.createResponse(id, token, InteractionResponseBuilder.channelMessage(builder as MessageBuilder));
}
} else {
assert(updateMessage == _didUpdateMessage || updateMessage == null, 'Cannot change the value of updateMessage between acknowledge and respond');
assert(builder.isEphemeral == _wasEphemeral || builder.isEphemeral == null, 'Cannot change the value of isEphemeral between acknowledge and respond');
assert(isEphemeral == _wasEphemeral || isEphemeral == null, 'Cannot change the value of isEphemeral between acknowledge and respond');

await manager.updateOriginalResponse(token, builder);
if (updateMessage == true) {
await manager.updateOriginalResponse(token, builder as MessageUpdateBuilder);
} else {
await manager.createFollowup(token, builder as MessageBuilder);
}
}
}
}
Expand Down

0 comments on commit 1a46a93

Please sign in to comment.