Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #74 from nyxx-discord/dev
Browse files Browse the repository at this point in the history
Release 4.5.0
  • Loading branch information
l7ssha authored Dec 12, 2022
2 parents 101af14 + d0dc776 commit 1c237b1
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 190 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 4.5.0
__12.12.022__

- feature: Handle HTTP error response in acknowledge()
- feature: Update nyxx_interactions to work with the new logging system in nyxx (#69)
- feature: Add support for nsfw commands (#66)
- bug: fix slash command model failing with nsfw field (#71)
- bug: Fix nsfw field not present in payload (#73)

## 4.4.0
__14.11.022__

Expand Down
5 changes: 5 additions & 0 deletions lib/src/builders/slash_command_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class SlashCommandBuilder extends Builder {
/// operator, they will be allowed to execute the command.
int? requiredPermissions;

/// Indicates whether the command is age-restricted, defaults to `false`.
bool? isNsfw;

/// A slash command, can only be instantiated through a method on [IInteractions]
SlashCommandBuilder(
this.name,
Expand All @@ -97,6 +100,7 @@ class SlashCommandBuilder extends Builder {
@Deprecated('Use canBeUsedInDm and requiredPermissions instead') this.permissions,
this.localizationsName,
this.localizationsDescription,
this.isNsfw,
}) {
if (!slashCommandNameRegex.hasMatch(name)) {
throw ArgumentError("Command name has to match regex: ${slashCommandNameRegex.pattern}");
Expand All @@ -122,6 +126,7 @@ class SlashCommandBuilder extends Builder {
if (localizationsName != null) "name_localizations": localizationsName!.map((k, v) => MapEntry<String, String>(k.toString(), v)),
if (localizationsDescription != null) "description_localizations": localizationsDescription!.map((k, v) => MapEntry<String, String>(k.toString(), v)),
"default_permission": defaultPermissions,
if (isNsfw != null) 'nsfw': isNsfw,
};

void setId(Snowflake id) => _id = id;
Expand Down
24 changes: 17 additions & 7 deletions lib/src/events/interaction_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ abstract class InteractionEventWithAcknowledge<T extends IInteraction> extends I
@override
Future<IMessage> sendFollowup(MessageBuilder builder, {bool hidden = false}) async {
if (!_hasAcked) {
return Future.error(ResponseRequiredError());
throw ResponseRequiredError();
}
logger.fine("Sending followup for interaction: ${interaction.id}");

Expand Down Expand Up @@ -183,14 +183,24 @@ abstract class InteractionEventWithAcknowledge<T extends IInteraction> extends I
@override
Future<void> acknowledge({bool hidden = false}) async {
if (_hasAcked) {
return Future.error(AlreadyRespondedError());
throw AlreadyRespondedError();
}

if (DateTime.now().isAfter(receivedAt.add(const Duration(seconds: 3)))) {
return Future.error(InteractionExpiredError.threeSecs());
throw InteractionExpiredError.threeSecs();
}

await interactions.interactionsEndpoints.acknowledge(interaction.token, interaction.id.toString(), hidden, _acknowledgeOpCode);
try {
await interactions.interactionsEndpoints.acknowledge(interaction.token, interaction.id.toString(), hidden, _acknowledgeOpCode);
} on IHttpResponseError catch (response) {
// 40060 - Interaction has already been acknowledged
// Catch in case of a desync between server and _hasAcked
if (response.code == 40060) {
throw AlreadyRespondedError();
}

rethrow;
}

logger.fine("Sending acknowledge for for interaction: ${interaction.id}");

Expand All @@ -203,17 +213,17 @@ abstract class InteractionEventWithAcknowledge<T extends IInteraction> extends I
Future<void> respond(MessageBuilder builder, {bool hidden = false}) async {
final now = DateTime.now();
if (_hasAcked && now.isAfter(receivedAt.add(const Duration(minutes: 15)))) {
return Future.error(InteractionExpiredError.fifteenMins());
throw InteractionExpiredError.fifteenMins();
} else if (!_hasAcked && now.isAfter(receivedAt.add(const Duration(seconds: 3)))) {
return Future.error(InteractionExpiredError.threeSecs());
throw InteractionExpiredError.threeSecs();
}

logger.fine("Sending respond for for interaction: ${interaction.id}");
if (_hasAcked) {
await interactions.interactionsEndpoints.respondEditOriginal(interaction.token, client.appId, builder, hidden);
} else {
if (!builder.canBeUsedAsNewMessage()) {
return Future.error(ArgumentError("Cannot sent message when MessageBuilder doesn't have set either content, embed or files"));
throw ArgumentError("Cannot sent message when MessageBuilder doesn't have set either content, embed or files");
}

await interactions.interactionsEndpoints.respondCreateResponse(interaction.token, interaction.id.toString(), builder, hidden, _respondOpcode);
Expand Down
5 changes: 1 addition & 4 deletions lib/src/exceptions/already_responded.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/// Thrown when you have already responded to an interaction
class AlreadyRespondedError implements Error {
class AlreadyRespondedError extends Error {
/// Returns a string representation of this object.
@override
String toString() => "AlreadyRespondedError: Interaction has already been acknowledged, you can now only send channel messages (with/without source)";

@override
StackTrace? get stackTrace => StackTrace.empty;
}
Loading

0 comments on commit 1c237b1

Please sign in to comment.