public override string TryToExecute(CommandReceivedEventArgs eventArgs)
        {
            var word         = eventArgs?.Arguments?.ElementAtOrDefault(1)?.ToLowerInvariant();
            var newAlias     = eventArgs?.Arguments?.ElementAtOrDefault(2)?.ToLowerInvariant();
            var typeName     = _repository.Single(CommandWordPolicy.ByWord(word))?.FullTypeName;
            var existingWord = _repository.Single(CommandWordPolicy.ByWord(newAlias));

            if (string.IsNullOrEmpty(newAlias))
            {
                return("You seem to be missing the new alias you want to set.");
            }

            if (existingWord != null)
            {
                return($"The command word '!{existingWord.CommandWord}' already exists.");
            }

            var newCommand = new CommandWordEntity
            {
                CommandWord  = newAlias,
                FullTypeName = typeName,
                IsPrimary    = false
            };

            _repository.Create(newCommand);

            return($"Created new command alias '!{newAlias}' for '!{word}'.");
        }
        protected override void HandleCommand(IChatClient chatClient, CommandReceivedEventArgs eventArgs)
        {
            var oper = eventArgs?.Arguments?.ElementAtOrDefault(0)?.ToLowerInvariant();
            var word = eventArgs?.Arguments?.ElementAtOrDefault(1)?.ToLowerInvariant();

            if (string.IsNullOrEmpty(oper) || string.IsNullOrEmpty(word))
            {
                chatClient.SendMessage(HelpText);
                return;
            }

            var typeName = Repository.Single(CommandWordPolicy.ByWord(word))?.FullTypeName;

            if (typeName == null)
            {
                chatClient.SendMessage($"The command '!{word}' doesn't exist.");
                return;
            }

            var operationToUse = _operations.SingleOrDefault(x => x.ShouldExecute(oper));

            if (operationToUse != null)
            {
                string resultMessage = operationToUse.TryToExecute(eventArgs);
                chatClient.SendMessage(resultMessage);
                CommandAliasModified?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                chatClient.SendMessage(HelpText);
            }
        }
        private static List <CommandWordEntity> GetMissingCommandWords(IRepository repository)
        {
            const string conventionSuffix = "Command";

            // Access the assembly to make sure it's loaded
            Assembly assembly = typeof(BlastCommand).Assembly;

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            IEnumerable <TypeInfo> allTypes = assemblies.SelectMany(x => x.DefinedTypes);

            var concreteCommands = allTypes
                                   .Where(x => typeof(IBotCommand).IsAssignableFrom(x))
                                   .Where(x => !x.IsAbstract)
                                   .Where(x => !x.IsSubclassOf(typeof(DataEntity)))
                                   .Where(x => x.FullName.EndsWith(conventionSuffix))
                                   .ToList();

            var storedCommandWords = repository.List(CommandWordPolicy.OnlyPrimaries()).Select(x => x.CommandWord);

            List <CommandWordEntity> missingDefaults = concreteCommands
                                                       .Select(commandType => new CommandWordEntity
            {
                CommandWord  = commandType.Name.Substring(0, commandType.Name.Length - conventionSuffix.Length),
                FullTypeName = commandType.FullName,
                IsPrimary    = true
            })
                                                       .Where(x => !storedCommandWords.Contains(x.CommandWord))
                                                       .ToList();

            return(missingDefaults);
        }
Exemple #4
0
 private List <string> RefreshCommandWords()
 {
     return(Repository
            .List(CommandWordPolicy.ByType(GetType()))
            ?.OrderByDescending(x => x.IsPrimary)
            .Select(word => word.CommandWord)
            .ToList() ?? new List <string>());
 }
        public override string TryToExecute(CommandReceivedEventArgs eventArgs)
        {
            if (eventArgs?.Arguments == null ||
                eventArgs.Arguments.Count < 3)
            {
                return(HelpText);
            }

            var word      = eventArgs.Arguments[1].ToLowerInvariant();
            var newAlias  = eventArgs.Arguments[2].ToLowerInvariant();
            var arguments = eventArgs.Arguments.Skip(3).ToList();

            var typeName     = _repository.Single(CommandWordPolicy.ByWord(word))?.FullTypeName;
            var existingWord = _repository.Single(CommandWordPolicy.ByWord(newAlias));

            if (string.IsNullOrEmpty(newAlias))
            {
                return("You seem to be missing the new alias you want to set.");
            }

            if (existingWord != null)
            {
                return($"The command word '!{existingWord.CommandWord}' already exists.");
            }

            var newCommand = new CommandWordEntity
            {
                CommandWord  = newAlias,
                FullTypeName = typeName,
                IsPrimary    = false
            };

            for (int i = 0; i < arguments.Count; i++)
            {
                newCommand.Arguments.Add(new AliasArgumentEntity
                {
                    Argument          = arguments[i],
                    CommandWordEntity = newCommand,
                    Index             = i
                });
            }

            _repository.Create(newCommand);

            return($"Created new command alias '!{newAlias}' for '!{word}'.");
        }
        public override string TryToExecute(CommandReceivedEventArgs eventArgs)
        {
            var word = eventArgs?.Arguments?.ElementAtOrDefault(1)?.ToLowerInvariant();

            var existingWord = _repository.Single(CommandWordPolicy.ByWord(word));

            if (existingWord == null)
            {
                return($"The command word '!{word}' doesn't exist.");
            }

            if (existingWord.IsPrimary)
            {
                return("The primary command cannot be deleted.");
            }

            _repository.Remove(existingWord);

            return($"The command '!{existingWord.CommandWord}' has been deleted.");
        }
Exemple #7
0
        private static List <CommandWordEntity> GetMissingCommandWords(IRepository repository)
        {
            var botCommandTypeAssembly = typeof(IBotCommand).Assembly;
            var conventionSuffix       = "Command";

            var concreteCommands = botCommandTypeAssembly.DefinedTypes
                                   .Where(x => !x.IsAbstract)
                                   .Where(x => !x.IsSubclassOf(typeof(DataEntity)))
                                   .Where(x => x.FullName.EndsWith(conventionSuffix));

            var storedCommandWords = repository.List(CommandWordPolicy.OnlyPrimaries()).Select(x => x.CommandWord);

            List <CommandWordEntity> defaultCommandWords = concreteCommands
                                                           .Select(commandType => new CommandWordEntity
            {
                CommandWord  = commandType.Name.Substring(0, commandType.Name.Length - conventionSuffix.Length),
                FullTypeName = commandType.FullName,
                IsPrimary    = true
            })
                                                           .Where(x => !storedCommandWords.Contains(x.CommandWord))
                                                           .ToList();

            return(defaultCommandWords);
        }