From 8e8d51be735b41b5e65d70a88f3d823c2fc16a70 Mon Sep 17 00:00:00 2001 From: Szymon Uglis Date: Sun, 18 Apr 2021 02:50:33 +0200 Subject: [PATCH] Update docs; Throw when registering handler for slashcommand if there are subcommands or subcommandgroups --- nyxx_extensions/pubspec.yaml | 2 +- nyxx_interactions/lib/src/Interactions.dart | 11 ++++++++--- .../lib/src/builders/CommandOptionBuilder.dart | 4 ++-- .../lib/src/builders/SlashCommandBuilder.dart | 11 +++++++++-- .../lib/src/exceptions/AlreadyResponded.dart | 4 +--- .../lib/src/exceptions/InteractionExpired.dart | 4 +--- nyxx_interactions/lib/src/internal/utils.dart | 2 ++ nyxx_interactions/lib/src/models/ArgChoice.dart | 3 +++ nyxx_interactions/lib/src/models/CommandOption.dart | 1 + nyxx_interactions/lib/src/models/SlashCommand.dart | 1 + nyxx_interactions/pubspec.yaml | 2 +- 11 files changed, 30 insertions(+), 15 deletions(-) diff --git a/nyxx_extensions/pubspec.yaml b/nyxx_extensions/pubspec.yaml index 68fda545..21f75e38 100644 --- a/nyxx_extensions/pubspec.yaml +++ b/nyxx_extensions/pubspec.yaml @@ -11,4 +11,4 @@ environment: dependencies: http: "^0.13.1" - nyxx: "2.0.0-rc.2" \ No newline at end of file + nyxx: "2.0.0-rc.2" diff --git a/nyxx_interactions/lib/src/Interactions.dart b/nyxx_interactions/lib/src/Interactions.dart index 037d1406..1e5cff4a 100644 --- a/nyxx_interactions/lib/src/Interactions.dart +++ b/nyxx_interactions/lib/src/Interactions.dart @@ -1,6 +1,7 @@ part of nyxx_interactions; -typedef SlashCommandHandlder = FutureOr Function(InteractionEvent); +/// Function that will handle execution of interaction event +typedef SlashCommandHandler = FutureOr Function(InteractionEvent); /// Interaction extension for Nyxx. Allows use of: Slash Commands. class Interactions { @@ -14,7 +15,7 @@ class Interactions { final _commandBuilders = []; final _commands = []; - final _commandHandlers = {}; + final _commandHandlers = {}; /// Emitted when a slash command is sent. late final Stream onSlashCommand; @@ -39,13 +40,16 @@ class Interactions { }); } + /// Syncs commands builders with discord after client is ready. void syncOnReady() { this._client.onReady.listen((_) async { await this.sync(); }); } - /// Syncs command builders with discord + /// Syncs command builders with discord immediately. + /// Warning: Client could not be ready at the function execution. + /// Use [syncOnReady] for proper behavior Future sync() async { final commandPartition = _partition(this._commandBuilders, (element) => element.guild == null); final globalCommands = commandPartition.first; @@ -97,6 +101,7 @@ class Interactions { this._logger.info("Finished registering ${this._commandHandlers.length} commands!"); } + /// Allows to register new [SlashCommandBuilder] void registerSlashCommand(SlashCommandBuilder slashCommandBuilder) { this._commandBuilders.add(slashCommandBuilder); } diff --git a/nyxx_interactions/lib/src/builders/CommandOptionBuilder.dart b/nyxx_interactions/lib/src/builders/CommandOptionBuilder.dart index e778b92e..d8ce1545 100644 --- a/nyxx_interactions/lib/src/builders/CommandOptionBuilder.dart +++ b/nyxx_interactions/lib/src/builders/CommandOptionBuilder.dart @@ -33,7 +33,7 @@ class CommandOptionBuilder implements Builder { /// If the option is a subcommand or subcommand group type, this nested options will be the parameters List? options; - SlashCommandHandlder? _handler; + SlashCommandHandler? _handler; /// Used to create an argument for a [SlashCommandBuilder]. CommandOptionBuilder(this.type, this.name, this.description, @@ -50,7 +50,7 @@ class CommandOptionBuilder implements Builder { }; /// Registers handler for subcommand - void registerHandler(SlashCommandHandlder handler) { + void registerHandler(SlashCommandHandler handler) { if (this.type != CommandOptionType.subCommand) { throw StateError("Cannot register handler for command option with type other that subcommand"); } diff --git a/nyxx_interactions/lib/src/builders/SlashCommandBuilder.dart b/nyxx_interactions/lib/src/builders/SlashCommandBuilder.dart index 22d380d4..3f6d6b7e 100644 --- a/nyxx_interactions/lib/src/builders/SlashCommandBuilder.dart +++ b/nyxx_interactions/lib/src/builders/SlashCommandBuilder.dart @@ -14,7 +14,7 @@ class SlashCommandBuilder implements Builder { /// The arguments that the command takes List options; - SlashCommandHandlder? _handler; + SlashCommandHandler? _handler; /// A slash command, can only be instantiated through a method on [Interactions] SlashCommandBuilder(this.name, this.description, this.options, {this.guild}); @@ -25,5 +25,12 @@ class SlashCommandBuilder implements Builder { if (this.options.isNotEmpty) "options": this.options.map((e) => e._build()).toList() }; - void registerHandler(SlashCommandHandlder handler) => this._handler = handler; + /// Registers handler for command. Note command cannot have handler if there are options present + void registerHandler(SlashCommandHandler handler) { + if (this.options.any((element) => element.type == CommandOptionType.subCommand || element.type == CommandOptionType.subCommandGroup)) { + throw new ArgumentError("Cannot register handler for slash command if command have subcommand or subcommandgroup"); + } + + this._handler = handler; + } } diff --git a/nyxx_interactions/lib/src/exceptions/AlreadyResponded.dart b/nyxx_interactions/lib/src/exceptions/AlreadyResponded.dart index 76f68758..9e84b483 100644 --- a/nyxx_interactions/lib/src/exceptions/AlreadyResponded.dart +++ b/nyxx_interactions/lib/src/exceptions/AlreadyResponded.dart @@ -2,11 +2,9 @@ part of nyxx_interactions; /// Thrown when you have already responded to an interaction class AlreadyRespondedError implements 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)"; + String toString() => "AlreadyRespondedError: Interaction has already been acknowledged, you can now only send channel messages (with/without source)"; @override StackTrace? get stackTrace => StackTrace.empty; diff --git a/nyxx_interactions/lib/src/exceptions/InteractionExpired.dart b/nyxx_interactions/lib/src/exceptions/InteractionExpired.dart index 0c96727c..7b56c742 100644 --- a/nyxx_interactions/lib/src/exceptions/InteractionExpired.dart +++ b/nyxx_interactions/lib/src/exceptions/InteractionExpired.dart @@ -2,11 +2,9 @@ part of nyxx_interactions; /// Thrown when 15 minutes has passed since an interaction was called. class InteractionExpiredError implements Error { - /// Returns a string representation of this object. @override - String toString() => - "InteractionExpiredError: Interaction tokens are only valid for 15mins. It has been over 15mins and the token is now invalid."; + String toString() => "InteractionExpiredError: Interaction tokens are only valid for 15mins. It has been over 15mins and the token is now invalid."; @override StackTrace? get stackTrace => StackTrace.empty; diff --git a/nyxx_interactions/lib/src/internal/utils.dart b/nyxx_interactions/lib/src/internal/utils.dart index f6a64861..9f4e6207 100644 --- a/nyxx_interactions/lib/src/internal/utils.dart +++ b/nyxx_interactions/lib/src/internal/utils.dart @@ -16,6 +16,7 @@ Iterable> _partition(Iterable list, bool Function(T) predicate return [matches, nonMatches]; } +/// Determine what handler should be executed based on [interaction] String _determineInteractionCommandHandler(Interaction interaction) { final commandHash = "${interaction.commandId}|${interaction.name}"; @@ -32,6 +33,7 @@ String _determineInteractionCommandHandler(Interaction interaction) { return "$commandHash${subCommand.name}"; } +/// Groups [SlashCommandBuilder] for registering them later in bulk Map> _groupSlashCommandBuilders(Iterable commands) { final commandsMap = >{}; diff --git a/nyxx_interactions/lib/src/models/ArgChoice.dart b/nyxx_interactions/lib/src/models/ArgChoice.dart index 64ca4839..7891b2d7 100644 --- a/nyxx_interactions/lib/src/models/ArgChoice.dart +++ b/nyxx_interactions/lib/src/models/ArgChoice.dart @@ -1,8 +1,11 @@ part of nyxx_interactions; +/// Choice that user can pick from. For [CommandOptionType.integer] or [CommandOptionType.string] class ArgChoice { + /// Name of choice late final String name; + /// Value of choice late final dynamic value; ArgChoice._new(Map raw) { diff --git a/nyxx_interactions/lib/src/models/CommandOption.dart b/nyxx_interactions/lib/src/models/CommandOption.dart index 73960510..144b7ffd 100644 --- a/nyxx_interactions/lib/src/models/CommandOption.dart +++ b/nyxx_interactions/lib/src/models/CommandOption.dart @@ -23,6 +23,7 @@ class CommandOptionType extends IEnum { const CommandOptionType(int value) : super(value); } +/// An argument for a [SlashCommand]. class CommandOption { /// The type of arg that will be later changed to an INT value, their values can be seen in the table below: /// | Name | Value | diff --git a/nyxx_interactions/lib/src/models/SlashCommand.dart b/nyxx_interactions/lib/src/models/SlashCommand.dart index df18f565..0a065586 100644 --- a/nyxx_interactions/lib/src/models/SlashCommand.dart +++ b/nyxx_interactions/lib/src/models/SlashCommand.dart @@ -1,5 +1,6 @@ part of nyxx_interactions; +/// Represents slash command that is returned from Discord API. class SlashCommand extends SnowflakeEntity { /// Unique id of the parent application late final Snowflake applicationId; diff --git a/nyxx_interactions/pubspec.yaml b/nyxx_interactions/pubspec.yaml index a1482e3c..dc7d56e5 100644 --- a/nyxx_interactions/pubspec.yaml +++ b/nyxx_interactions/pubspec.yaml @@ -1,5 +1,5 @@ name: nyxx_interactions -version: 2.0.0-rc.2 +version: 2.0.0-rc.3 description: Interactions for Nyxx library homepage: https://github.com/l7ssha/nyxx repository: https://github.com/l7ssha/nyxx