From 32b3e595ab61a00cfe6fc9350c1439a2e8479cea Mon Sep 17 00:00:00 2001 From: Abitofevrything Date: Mon, 21 Aug 2023 06:22:17 +0200 Subject: [PATCH] Clean up interaction response code; move isEphemeral from MessageBuilder to InteractionResponseBuilder --- lib/src/builders/interaction_response.dart | 49 +++++++++++++++++++++- lib/src/builders/message/message.dart | 18 ++------ lib/src/models/interaction.dart | 33 +++++++++------ 3 files changed, 71 insertions(+), 29 deletions(-) diff --git a/lib/src/builders/interaction_response.dart b/lib/src/builders/interaction_response.dart index acb85ebc0..4848fc5f5 100644 --- a/lib/src/builders/interaction_response.dart +++ b/lib/src/builders/interaction_response.dart @@ -13,9 +13,23 @@ class InteractionResponseBuilder extends CreateBuilder 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( @@ -56,6 +70,37 @@ class InteractionResponseBuilder extends CreateBuilder 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), diff --git a/lib/src/builders/message/message.dart b/lib/src/builders/message/message.dart index c9eebfa55..7b0a55c0d 100644 --- a/lib/src/builders/message/message.dart +++ b/lib/src/builders/message/message.dart @@ -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 implements MessageUpdateBuilder { - @override +class MessageBuilder extends CreateBuilder { final String? content; final dynamic /* int | String */ nonce; final bool? tts; - @override final List? embeds; - @override final AllowedMentions? allowedMentions; final Snowflake? replyId; final bool? requireReplyToExist; - @override final List? components; final List? stickerIds; - @override final List? attachments; - @override final bool? suppressEmbeds; final bool? suppressNotifications; - final bool? isEphemeral; - MessageBuilder({ this.content, this.nonce, @@ -53,7 +45,6 @@ class MessageBuilder extends CreateBuilder implements MessageUpdateBuil this.attachments, this.suppressEmbeds, this.suppressNotifications, - this.isEphemeral, }); @override @@ -135,10 +126,9 @@ class MessageBuilder extends CreateBuilder 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), }; } } diff --git a/lib/src/models/interaction.dart b/lib/src/models/interaction.dart index c752fff7a..0ca12191c 100644 --- a/lib/src/models/interaction.dart +++ b/lib/src/models/interaction.dart @@ -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'; @@ -111,19 +112,19 @@ mixin MessageResponse on Interaction { } /// Send a response to this interaction. - Future respond(MessageBuilder builder) async { + Future 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); } } @@ -261,27 +262,33 @@ class MessageComponentInteraction extends Interaction respond(MessageBuilder builder, {bool? updateMessage}) async { + Future respond(Builder 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); + } } } }