From a3ec0bca6b4422c88641357ff2d4bf56c8339359 Mon Sep 17 00:00:00 2001 From: Abitofevrything <54505189+abitofevrything@users.noreply.github.com> Date: Sun, 24 Sep 2023 11:34:34 -0700 Subject: [PATCH] Handle errors from event manager (#132) --- lib/src/commands.dart | 118 +++++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 43 deletions(-) diff --git a/lib/src/commands.dart b/lib/src/commands.dart index e5bbbc8..cd62c03 100644 --- a/lib/src/commands.dart +++ b/lib/src/commands.dart @@ -173,62 +173,94 @@ class CommandsPlugin extends NyxxPlugin implements CommandGroup event.interaction) .where((interaction) => interaction.data.type == MessageComponentType.button) - .listen(eventManager.processButtonInteraction); + .listen( + (interaction) async { + try { + await eventManager.processButtonInteraction(interaction); + } on CommandsException catch (e) { + _onCommandErrorController.add(e); + } + }, + ); client.onMessageComponentInteraction .map((event) => event.interaction) .where((interaction) => interaction.data.type == MessageComponentType.stringSelect) - .listen(eventManager.processSelectMenuInteraction); - - client.onMessageCreate.listen(eventManager.processMessageCreateEvent); - - client.onApplicationCommandInteraction.map((event) => event.interaction).listen((interaction) { - final applicationCommand = registeredCommands.singleWhere( - (command) => command.id == interaction.data.id, - ); - - if (interaction.data.type == ApplicationCommandType.user) { - eventManager.processUserInteraction( - interaction, - _userCommands[applicationCommand.name]!, - ); - } else if (interaction.data.type == ApplicationCommandType.message) { - eventManager.processMessageInteraction( - interaction, - _messageCommands[applicationCommand.name]!, - ); - } else if (interaction.data.type == ApplicationCommandType.chatInput) { - final (command, options) = _resolveChatCommand(interaction, applicationCommand); - - eventManager.processChatInteraction( - interaction, - options, - command, - ); + .listen( + (interaction) async { + try { + await eventManager.processSelectMenuInteraction(interaction); + } on CommandsException catch (e) { + _onCommandErrorController.add(e); + } + }, + ); + + client.onMessageCreate.listen((event) async { + try { + await eventManager.processMessageCreateEvent(event); + } on CommandsException catch (e) { + _onCommandErrorController.add(e); } }); + client.onApplicationCommandInteraction.map((event) => event.interaction).listen( + (interaction) async { + try { + final applicationCommand = registeredCommands.singleWhere( + (command) => command.id == interaction.data.id, + ); + + if (interaction.data.type == ApplicationCommandType.user) { + await eventManager.processUserInteraction( + interaction, + _userCommands[applicationCommand.name]!, + ); + } else if (interaction.data.type == ApplicationCommandType.message) { + await eventManager.processMessageInteraction( + interaction, + _messageCommands[applicationCommand.name]!, + ); + } else if (interaction.data.type == ApplicationCommandType.chatInput) { + final (command, options) = _resolveChatCommand(interaction, applicationCommand); + + await eventManager.processChatInteraction( + interaction, + options, + command, + ); + } + } on CommandsException catch (e) { + _onCommandErrorController.add(e); + } + }, + ); + client.onApplicationCommandAutocompleteInteraction .map((event) => event.interaction) - .listen((interaction) { - final applicationCommand = registeredCommands.singleWhere( - (command) => command.id == interaction.data.id, - ); + .listen((interaction) async { + try { + final applicationCommand = registeredCommands.singleWhere( + (command) => command.id == interaction.data.id, + ); - final (command, options) = _resolveChatCommand(interaction, applicationCommand); + final (command, options) = _resolveChatCommand(interaction, applicationCommand); - final functionData = loadFunctionData(command.execute); - final focusedOption = options.singleWhere((element) => element.isFocused == true); - final focusedParameter = - functionData.parametersData.singleWhere((element) => element.name == focusedOption.name); + final functionData = loadFunctionData(command.execute); + final focusedOption = options.singleWhere((element) => element.isFocused == true); + final focusedParameter = functionData.parametersData + .singleWhere((element) => element.name == focusedOption.name); - final converter = focusedParameter.converterOverride ?? getConverter(focusedParameter.type); + final converter = focusedParameter.converterOverride ?? getConverter(focusedParameter.type); - eventManager.processAutocompleteInteraction( - interaction, - (focusedParameter.autocompleteOverride ?? converter?.autocompleteCallback)!, - command, - ); + await eventManager.processAutocompleteInteraction( + interaction, + (focusedParameter.autocompleteOverride ?? converter?.autocompleteCallback)!, + command, + ); + } on CommandsException catch (e) { + _onCommandErrorController.add(e); + } }); if (children.isNotEmpty) {