From 5714f9cdd00b0c182d26ba80a6d42214ca5199e4 Mon Sep 17 00:00:00 2001 From: Szymon Uglis Date: Wed, 3 Mar 2021 00:43:03 +0100 Subject: [PATCH] Add subCommand property to event for easier subcommand recognition --- .../lib/src/events/InteractionEvent.dart | 16 ++++++++++++++++ .../lib/src/models/InteractionOption.dart | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/nyxx.interactions/lib/src/events/InteractionEvent.dart b/nyxx.interactions/lib/src/events/InteractionEvent.dart index 38ababf8..334e66f7 100644 --- a/nyxx.interactions/lib/src/events/InteractionEvent.dart +++ b/nyxx.interactions/lib/src/events/InteractionEvent.dart @@ -3,13 +3,29 @@ part of nyxx_interactions; /// The event that you receive when a user types a slash command. class InteractionEvent { late final Nyxx _client; + /// The interaction data, includes the args, name, guild, channel, etc. late final Interaction interaction; + /// The DateTime the interaction was received by the Nyxx Client. final DateTime receivedAt = DateTime.now(); + /// If the Client has sent a response to the Discord API. Once the API was received a response you cannot send another. bool hasResponded = false; + /// Returns subcommand or null if not subcommand + InteractionOption? get subCommand { + if (this.interaction.args.isEmpty) { + return null; + } + + try { + return this.interaction.args.firstWhere((element) => element.type == CommandArgType.subCommand); + } on Error { + return null; + } + } + InteractionEvent._new(this._client, Map rawJson) { this.interaction = Interaction._new(this._client, rawJson); diff --git a/nyxx.interactions/lib/src/models/InteractionOption.dart b/nyxx.interactions/lib/src/models/InteractionOption.dart index cf7be90e..2c81a6cb 100644 --- a/nyxx.interactions/lib/src/models/InteractionOption.dart +++ b/nyxx.interactions/lib/src/models/InteractionOption.dart @@ -6,7 +6,7 @@ class InteractionOption { late final dynamic value; /// Type of interaction - late final InteractionOption type; + late final CommandArgType type; /// Name of option late final String name; @@ -20,6 +20,7 @@ class InteractionOption { InteractionOption._new(Map raw) { this.value = raw["value"] as dynamic; this.name = raw["name"] as String; + this.type = CommandArgType(raw["type"] as int); if (raw["options"] != null) { this.args = (raw["options"] as List>).map((e) => InteractionOption._new(e));