Skip to content

Commit

Permalink
feature: Implement http interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha committed Oct 27, 2024
1 parent d8d8a78 commit 1389481
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
45 changes: 29 additions & 16 deletions lib/src/commands.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/src/plugin/HttpInteractions.dart';

import 'checks/checks.dart';
import 'checks/guild.dart';
Expand Down Expand Up @@ -170,8 +171,23 @@ class CommandsPlugin extends NyxxPlugin<NyxxGateway> implements CommandGroup<Com
Future<void> afterConnect(NyxxGateway client) async {
_attachedClients.add(client);

client.onMessageComponentInteraction
.map((event) => event.interaction)
var onMessageComponentInteraction =
client.onMessageComponentInteraction.map((event) => event.interaction);
var onApplicationCommandInteraction =
client.onApplicationCommandInteraction.map((event) => event.interaction);
var onApplicationCommandAutocompleteInteraction =
client.onApplicationCommandAutocompleteInteraction.map((event) => event.interaction);

final httpInteractionsPlugin =
client.options.plugins.whereType<HttpInteractionsPlugin>().firstOrNull;
if (httpInteractionsPlugin != null) {
onMessageComponentInteraction = httpInteractionsPlugin.onMessageComponentInteraction;
onApplicationCommandInteraction = httpInteractionsPlugin.onApplicationCommandInteraction;
onApplicationCommandAutocompleteInteraction =
httpInteractionsPlugin.onApplicationCommandAutocompleteInteraction;
}

onMessageComponentInteraction
.where((interaction) => interaction.data.type == MessageComponentType.button)
.listen(
(interaction) async {
Expand All @@ -183,8 +199,7 @@ class CommandsPlugin extends NyxxPlugin<NyxxGateway> implements CommandGroup<Com
},
);

client.onMessageComponentInteraction
.map((event) => event.interaction)
onMessageComponentInteraction
.where((interaction) => interaction.data.type == MessageComponentType.stringSelect)
.listen(
(interaction) async {
Expand All @@ -196,15 +211,7 @@ class CommandsPlugin extends NyxxPlugin<NyxxGateway> implements CommandGroup<Com
},
);

client.onMessageCreate.listen((event) async {
try {
await eventManager.processMessageCreateEvent(event);
} on CommandsException catch (e) {
_onCommandErrorController.add(e);
}
});

client.onApplicationCommandInteraction.map((event) => event.interaction).listen(
onApplicationCommandInteraction.listen(
(interaction) async {
try {
final applicationCommand = registeredCommands.singleWhere(
Expand Down Expand Up @@ -236,9 +243,7 @@ class CommandsPlugin extends NyxxPlugin<NyxxGateway> implements CommandGroup<Com
},
);

client.onApplicationCommandAutocompleteInteraction
.map((event) => event.interaction)
.listen((interaction) async {
onApplicationCommandAutocompleteInteraction.listen((interaction) async {
try {
final applicationCommand = registeredCommands.singleWhere(
(command) => command.id == interaction.data.id,
Expand All @@ -263,6 +268,14 @@ class CommandsPlugin extends NyxxPlugin<NyxxGateway> implements CommandGroup<Com
}
});

client.onMessageCreate.listen((event) async {
try {
await eventManager.processMessageCreateEvent(event);
} on CommandsException catch (e) {
_onCommandErrorController.add(e);
}
});

if (children.isNotEmpty) {
_syncCommands(client);
}
Expand Down
24 changes: 24 additions & 0 deletions lib/src/plugin/HttpInteractions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:nyxx/nyxx.dart';

import 'package:nyxx/src/utils/iterable_extension.dart';

abstract class HttpInteractionsPlugin implements NyxxPlugin<NyxxRest> {
Stream<Interaction<dynamic>> get onInteractionCreate;

/// A [Stream] of [ApplicationCommandInteraction]s received by this client.
Stream<ApplicationCommandInteraction> get onApplicationCommandInteraction =>
onInteractionCreate.whereType<ApplicationCommandInteraction>();

/// A [Stream] of [MessageComponentInteraction]s received by this client.
Stream<MessageComponentInteraction> get onMessageComponentInteraction =>
onInteractionCreate.whereType<MessageComponentInteraction>();

/// A [Stream] of [ModalSubmitInteraction]s received by this client.
Stream<ModalSubmitInteraction> get onModalSubmitInteraction =>
onInteractionCreate.whereType<ModalSubmitInteraction>();

/// A [Stream] of [ApplicationCommandAutocompleteInteraction]s received by this client.
Stream<ApplicationCommandAutocompleteInteraction>
get onApplicationCommandAutocompleteInteraction =>
onInteractionCreate.whereType<ApplicationCommandAutocompleteInteraction>();
}
11 changes: 9 additions & 2 deletions lib/src/util/mixins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:math';

import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/src/plugin/HttpInteractions.dart';

import '../checks/checks.dart';
import '../commands.dart';
Expand Down Expand Up @@ -742,8 +743,14 @@ mixin InteractionRespondMixin
return (_delegate as InteractionInteractiveContext).awaitModal(customId, timeout: timeout);
}

Future<ModalSubmitInteraction> event = client.onModalSubmitInteraction
.map((e) => e.interaction)
var onModalSubmitInteraction = client.onModalSubmitInteraction.map((e) => e.interaction);
final httpInteractionsPlugin =
client.options.plugins.whereType<HttpInteractionsPlugin>().firstOrNull;
if (httpInteractionsPlugin != null) {
onModalSubmitInteraction = httpInteractionsPlugin.onModalSubmitInteraction;
}

Future<ModalSubmitInteraction> event = onModalSubmitInteraction
.where(
(event) => event.data.customId == customId,
)
Expand Down

0 comments on commit 1389481

Please sign in to comment.