Update docs; Throw when registering handler for slashcommand if there are subcommands or subcommandgroups

This commit is contained in:
Szymon Uglis 2021-04-18 02:50:33 +02:00
parent 02e71ab27a
commit 8e8d51be73
No known key found for this signature in database
GPG key ID: AA55C76D0E11A7E3
11 changed files with 30 additions and 15 deletions

View file

@ -11,4 +11,4 @@ environment:
dependencies:
http: "^0.13.1"
nyxx: "2.0.0-rc.2"
nyxx: "2.0.0-rc.2"

View file

@ -1,6 +1,7 @@
part of nyxx_interactions;
typedef SlashCommandHandlder = FutureOr<void> Function(InteractionEvent);
/// Function that will handle execution of interaction event
typedef SlashCommandHandler = FutureOr<void> Function(InteractionEvent);
/// Interaction extension for Nyxx. Allows use of: Slash Commands.
class Interactions {
@ -14,7 +15,7 @@ class Interactions {
final _commandBuilders = <SlashCommandBuilder>[];
final _commands = <SlashCommand>[];
final _commandHandlers = <String, SlashCommandHandlder>{};
final _commandHandlers = <String, SlashCommandHandler>{};
/// Emitted when a slash command is sent.
late final Stream<InteractionEvent> 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<void> sync() async {
final commandPartition = _partition<SlashCommandBuilder>(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);
}

View file

@ -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<CommandOptionBuilder>? 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");
}

View file

@ -14,7 +14,7 @@ class SlashCommandBuilder implements Builder {
/// The arguments that the command takes
List<CommandOptionBuilder> 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;
}
}

View file

@ -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;

View file

@ -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;

View file

@ -16,6 +16,7 @@ Iterable<Iterable<T>> _partition<T>(Iterable<T> 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<Snowflake, Iterable<SlashCommandBuilder>> _groupSlashCommandBuilders(Iterable<SlashCommandBuilder> commands) {
final commandsMap = <Snowflake, List<SlashCommandBuilder>>{};

View file

@ -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<String, dynamic> raw) {

View file

@ -23,6 +23,7 @@ class CommandOptionType extends IEnum<int> {
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 |

View file

@ -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;

View file

@ -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