예제 #1
0
        /// <summary>
        /// Execute command
        /// </summary>
        /// <param name="commandArguments">Command arguments</param>
        /// <returns></returns>
        public ECommandResult Execute(ICommandArguments commandArguments)
        {
            ECommandResult ret = ECommandResult.Failed;

            if (commandArguments.Arguments.Count == 1)
            {
                CommandsConfiguration commands_configuration = commandArguments.Bot.GetService <CommandsConfiguration>();
                if (commands_configuration != null)
                {
                    string message = null;
                    string command = commandArguments.Arguments[0].Trim().ToLower();
                    CommandConfigurationData command_configuration_data = null;
                    if (commands_configuration.Data.Commands.ContainsKey(command))
                    {
                        command_configuration_data = commands_configuration.Data.Commands[command];
                        if (command_configuration_data == null)
                        {
                            command_configuration_data = new CommandConfigurationData();
                            commands_configuration.Data.Commands[command] = command_configuration_data;
                        }
                    }
                    else
                    {
                        command_configuration_data = new CommandConfigurationData();
                        commands_configuration.Data.Commands.Add(command, command_configuration_data);
                    }
                    if (command_configuration_data.Enabled)
                    {
                        command_configuration_data.Enabled = false;
                        commands_configuration.Save();
                        message = "Command \"" + command + "\" has been successfully disabled.";
                        ret     = ECommandResult.Successful;
                    }
                    else
                    {
                        message = "Command \"" + command + "\" is already disabled.";
                    }
                    if (message != null)
                    {
                        IChat[] chat_services = commandArguments.Bot.GetServices <IChat>();
                        if (chat_services != null)
                        {
                            foreach (IChat chat in chat_services)
                            {
                                if (chat != null)
                                {
                                    chat.SendMessage(message, commandArguments.MessageChannel);
                                }
                            }
                        }
                    }
                }
            }
            return(ret);
        }
예제 #2
0
        /// <summary>
        /// Initialize (asynchronous)
        /// </summary>
        /// <param name="bot">Bot</param>
        /// <returns>Task</returns>
        public Task InitAsync(IBot bot)
        {
            this.bot = bot;
            Commands commands = RebuildCommands();

            if (commands != null)
            {
                commands.CommandsConfiguration = bot.GetService <CommandsConfiguration>();
                DiscordSocketClient discord_client = bot.GetService <DiscordSocketClient>();
                if (discord_client != null)
                {
                    discord_client.MessageReceived += async(message) =>
                    {
                        if (message.Author.Id != discord_client.CurrentUser.Id)
                        {
                            if (message.Content != null)
                            {
                                CommandsConfiguration command_configuration = bot.GetService <CommandsConfiguration>();
                                if (command_configuration != null)
                                {
                                    string possible_command = message.Content.TrimStart();
                                    if (possible_command.StartsWith(command_configuration.Data.Delimiter))
                                    {
                                        string         command        = possible_command.Substring(command_configuration.Data.Delimiter.Length);
                                        ICommandResult command_result = await commands.ExecuteAsync(command, message.Author, message.Channel, bot);

                                        string msg = null;
                                        switch (command_result.Result)
                                        {
                                        case ECommandResult.Failed:
                                            msg = string.Format(command_configuration.Data.FailedText, command_result.CompiledCommand.CommandName, command_result.CompiledCommand.CompiledCommandArguments.RawArguments);
                                            break;

                                        case ECommandResult.Successful:
                                            msg = string.Format(command_configuration.Data.SuccessfulText, command_result.CompiledCommand.CommandName, command_result.CompiledCommand.CompiledCommandArguments.RawArguments);
                                            break;

                                        case ECommandResult.Denied:
                                            msg = string.Format(command_configuration.Data.DeniedText, command_result.CompiledCommand.CommandName, command_result.CompiledCommand.CompiledCommandArguments.RawArguments);
                                            break;

                                        case ECommandResult.Disabled:
                                            msg = string.Format(command_configuration.Data.DisabledText, command_result.CompiledCommand.CommandName, command_result.CompiledCommand.CompiledCommandArguments.RawArguments);
                                            break;
                                        }
                                        if (msg != null)
                                        {
                                            if (msg.Length > 0)
                                            {
                                                IChat[] chat_services = bot.GetServices <IChat>();
                                                if (chat_services != null)
                                                {
                                                    foreach (IChat chat in chat_services)
                                                    {
                                                        await chat.SendMessageAsync(msg, message.Channel);

                                                        if (command_result.Result == ECommandResult.Failed)
                                                        {
                                                            await commands.ExecuteAsync("help " + command, message.Author, message.Channel, bot);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    };
                }
            }
            return(Task.CompletedTask);
        }
예제 #3
0
        /// <summary>
        /// Execute command
        /// </summary>
        /// <param name="commandArguments">Command arguments</param>
        /// <returns>Command result</returns>
        public ECommandResult Execute(ICommandArguments commandArguments)
        {
            ECommandResult ret = ECommandResult.Failed;

            if (commandArguments.Arguments.Count == 3)
            {
                string command   = commandArguments.Arguments[0].Trim().ToLower();
                string privilege = commandArguments.Arguments[1];
                uint   level;
                if (uint.TryParse(commandArguments.Arguments[2], out level))
                {
                    CommandsConfiguration commands_configuration = commandArguments.Bot.GetService <CommandsConfiguration>();
                    if (commands_configuration != null)
                    {
                        string message = null;
                        CommandConfigurationData command_configuration_data = null;
                        if (commands_configuration.Data.Commands.ContainsKey(command))
                        {
                            command_configuration_data = commands_configuration.Data.Commands[command];
                            if (command_configuration_data == null)
                            {
                                command_configuration_data = new CommandConfigurationData();
                                commands_configuration.Data.Commands[command] = command_configuration_data;
                            }
                        }
                        else
                        {
                            command_configuration_data = new CommandConfigurationData();
                            commands_configuration.Data.Commands.Add(command, command_configuration_data);
                        }
                        if (level == 0U)
                        {
                            if (command_configuration_data.Privileges.ContainsKey(privilege))
                            {
                                command_configuration_data.Privileges.Remove(privilege);
                                commands_configuration.Save();
                                message = "Privilege requirement \"" + privilege + "\" for \"" + command + "\" has been successfully removed.";
                                ret     = ECommandResult.Successful;
                            }
                            else
                            {
                                message = "Privilege requirement \"" + privilege + "\" doesn't exist for \"" + command + "\".";
                            }
                        }
                        else
                        {
                            if (command_configuration_data.Privileges.ContainsKey(privilege))
                            {
                                command_configuration_data.Privileges[privilege] = level;
                                commands_configuration.Save();
                                message = "Privilege requirement \"" + privilege + "\" for \"" + command + "\" has been successfully updated to level " + level + ".";
                            }
                            else
                            {
                                command_configuration_data.Privileges.Add(privilege, level);
                                commands_configuration.Save();
                                message = "Privilege requirement \"" + privilege + "\" for \"" + command + "\" has been successfully set to level " + level + ".";
                            }
                            ret = ECommandResult.Successful;
                        }
                        if (message != null)
                        {
                            IChat[] chat_services = commandArguments.Bot.GetServices <IChat>();
                            if (chat_services != null)
                            {
                                foreach (IChat chat in chat_services)
                                {
                                    if (chat != null)
                                    {
                                        chat.SendMessage(message, commandArguments.MessageChannel);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(ret);
        }