private static CreateCommandResult ToOption
        (
            IChildNode child,
            [NotNullWhen(true)] out IApplicationCommandOption?option,
            ulong depth = 0
        )
        {
            option = null;

            switch (child)
            {
            case CommandNode command:
            {
                var createParameterOptions = CreateCommandParameterOptions(command, out var parameterOptions);
                if (!createParameterOptions.IsSuccess)
                {
                    return(createParameterOptions);
                }

                option = new ApplicationCommandOption
                         (
                    SubCommand,
                    command.Key,
                    command.Shape.Description,
                    default,
                    default,
                    default,
                    new Optional <IReadOnlyList <IApplicationCommandOption> >(parameterOptions)
                         );

                return(CreateCommandResult.FromSuccess());
            }
        /// <summary>
        /// Converts the command tree to a set of Discord application commands.
        /// </summary>
        /// <param name="tree">The command tree.</param>
        /// <param name="commands">The created commands, if any.</param>
        /// <returns>A creation result which may or may not have succeeded.</returns>
        public static CreateCommandResult CreateApplicationCommands
        (
            this CommandTree tree,
            [NotNullWhen(true)] out IReadOnlyList <IApplicationCommandOption>?commands
        )
        {
            commands = null;

            var createdCommands = new List <IApplicationCommandOption>();

            foreach (var child in tree.Root.Children)
            {
                var createOption = ToOption(child, out var option);
                if (!createOption.IsSuccess)
                {
                    return(createOption);
                }

                createdCommands.Add(option !);
            }

            if (createdCommands.Count > MaxRootCommandsOrGroups)
            {
                return(CreateCommandResult.FromError("Too many root-level commands or groups."));
            }

            if (createdCommands.GroupBy(c => c.Name).Any(g => g.Count() > 1))
            {
                return(CreateCommandResult.FromError("Overloads are not supported."));
            }

            commands = createdCommands;
            return(CreateCommandResult.FromSuccess());
        }
Exemplo n.º 3
0
 private void TransmitResult(CreateCommandResult <Account> result, IEnumerable <ICommandActionListener> listeners = null)
 {
     if (listeners != null && result != null)
     {
         foreach (var listener in listeners)
         {
             listener.OnCommand(result);
         }
     }
 }
 public void OnCommand <T>(CreateCommandResult <T> result) where T : IDetailable
 {
     if (result.IsSuccessful)
     {
         Console.WriteLine($"{result.CreatedItem.TypeName} \"{result.CreatedItem.DisplayName}\" successfully created.");
     }
     else
     {
         Console.WriteLine($"Something went wrong. Failed to create object");
     }
 }
Exemplo n.º 5
0
        protected override bool TryDoAction(ILog log, IEnumerable <ICommandActionListener> listeners = null)
        {
            CreateCommandResult <Account> result = new CreateCommandResult <Account>(this, false, null);

            if (Repositories.AccountRepository.DoesNameExist(AccountName.GetValue(String.Empty)))
            {
                TransmitResult(result, listeners);
                return(false);
            }
            else if (CategoryNameOption.IsDataValid &&
                     !Repositories.AccountRepository.DoesNameExist(CategoryNameOption.GetValue(String.Empty)))
            {
                //invalid category name
                TransmitResult(result, listeners);
                return(false);
            }

            AccountDto accountDto = BuildAccountDto();

            bool successful = Repositories.AccountRepository.Upsert(accountDto);

            AccountId.SetData(accountDto.Id.Value);

            if (!accountDto.Id.HasValue)
            {
                log?.WriteLine("Error occurred while adding account. Account was not assigned a valid Id.", LogLevel.Error);
                TransmitResult(result, listeners);
                return(false);
            }

            AccountStateDto accountStateDto = BuildAccountStateDto(accountDto.Id.Value);

            successful &= Repositories.AccountStateRepository.Upsert(accountStateDto);

            if (successful)
            {
                log?.WriteLine($"Added account \"{accountDto.Name}\"", LogLevel.Normal);

                result = new CreateCommandResult <Account>(this, successful, DtoToModelTranslator.FromDto(accountDto, DateTime.Today, Repositories));
            }

            TransmitResult(result, listeners);
            return(successful);
        }
Exemplo n.º 6
0
 public virtual void OnCommand <T>(CreateCommandResult <T> result) where T : IDetailable
 {
     _results.Add(result);
 }