Skip to content

Commit

Permalink
Merge pull request #124 from nyxx-discord/hotfix/component-selection
Browse files Browse the repository at this point in the history
Various component bugs hotfix
  • Loading branch information
abitofevrything authored Apr 3, 2023
2 parents 5fa1adf + 79007be commit f8184ce
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 5.0.1
__Bug fixes__
- Fix component timeouts triggering instantly
- Fix component wrappers causing null assertions to trigger

## 5.0.0
__Breaking changes__
- Removed all deprecated APIs.
Expand Down
10 changes: 10 additions & 0 deletions lib/src/context/component_context.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:nyxx_commands/src/util/util.dart';
import 'package:nyxx_interactions/nyxx_interactions.dart';

import '../util/mixins.dart';
Expand All @@ -16,6 +17,9 @@ abstract class IComponentContextData implements IInteractionContextData {

/// The ID of the component that was interacted with.
String get componentId;

/// If [componentId] is a valid [ComponentId], this is the parsed version of that.
ComponentId? get parsedComponentId;
}

/// A context in which a component was interacted with.
Expand Down Expand Up @@ -43,6 +47,9 @@ class ButtonComponentContext extends ContextBase
@override
String get componentId => interaction.customId;

@override
ComponentId? get parsedComponentId => ComponentId.parse(componentId);

/// Create a new [ButtonComponentContext].
ButtonComponentContext({
required super.user,
Expand Down Expand Up @@ -78,6 +85,9 @@ class MultiselectComponentContext<T> extends ContextBase
/// Will be a [List] if multiple items were selected.
final T selected;

@override
ComponentId? get parsedComponentId => ComponentId.parse(componentId);

/// Create a new [MultiselectComponentContext].
MultiselectComponentContext({
required super.user,
Expand Down
37 changes: 33 additions & 4 deletions lib/src/util/mixins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ mixin InteractiveMixin implements IInteractiveContext, IContextData {
context._parent = this;
_delegate = context;

return idToValue[context.componentId]!;
return idToValue[context.parsedComponentId]!;
} on TimeoutException catch (e, s) {
throw InteractionTimeoutException(
'Timed out waiting for button selection',
Expand Down Expand Up @@ -516,10 +516,26 @@ mixin InteractiveMixin implements IInteractiveContext, IContextData {
context._parent = this;
_delegate = context;

return idToValue[context.selected.single]!;
final result = idToValue[context.selected.single] as T;

final matchingOptionIndex = menu.options.indexWhere(
(option) => option.value == context!.selected.single,
);

if (matchingOptionIndex >= 0) {
final builder = await toMultiSelect(result);

menu.options[matchingOptionIndex] = MultiselectOptionBuilder(
builder.label,
builder.value,
true,
);
}

return result;
} on TimeoutException catch (e, s) {
throw InteractionTimeoutException(
'TImed out waiting for selection',
'Timed out waiting for selection',
_nearestCommandContext,
)..stackTrace = s;
} finally {
Expand Down Expand Up @@ -578,7 +594,8 @@ mixin InteractiveMixin implements IInteractiveContext, IContextData {
allowedUser: authorOnly ? user.id : null,
);

MultiselectBuilder menu = MultiselectBuilder(menuId.toString(), options);
MultiselectBuilder menu = MultiselectBuilder(menuId.toString(), options)
..maxValues = choices.length;
ComponentRowBuilder row = ComponentRowBuilder()..addComponent(menu);

(builder as ComponentMessageBuilder).addComponentRow(row);
Expand All @@ -592,6 +609,18 @@ mixin InteractiveMixin implements IInteractiveContext, IContextData {
context._parent = this;
_delegate = context;

for (final value in context.selected) {
final matchingOptionIndex = menu.options.indexWhere((option) => option.value == value);

if (matchingOptionIndex >= 0) {
menu.options[matchingOptionIndex] = MultiselectOptionBuilder(
menu.options[matchingOptionIndex].label,
value,
true,
);
}
}

return context.selected.map((id) => idToValue[id]!).toList();
} on TimeoutException catch (e, s) {
throw InteractionTimeoutException(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/util/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ class ComponentId {
final Snowflake? allowedUser;

/// The time remaining until the handler for this [ComponentId] expires, if [expiresAt] was set.
Duration? get expiresIn => expiresAt != null ? DateTime.now().difference(expiresAt!) : null;
Duration? get expiresIn => expiresAt?.difference(DateTime.now());

/// The status of this [ComponentId].
///
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: nyxx_commands
version: 5.0.0
version: 5.0.1
description: A framework for easily creating slash commands and text commands for Discord using the nyxx library.

homepage: https://github.com/nyxx-discord/nyxx_commands/blob/main/README.md
Expand Down

0 comments on commit f8184ce

Please sign in to comment.