Ejemplo n.º 1
0
        /// <summary>
        /// Can command execute
        /// </summary>
        /// <param name="command">Command</param>
        /// <returns>"true" if command can be executed, otherwise "false"</returns>
        public bool CanCommandExecute(ICommand command)
        {
            bool ret = false;

            if ((command != null) && (CommandsConfiguration != null))
            {
                string key = command.Name.Trim().ToLower();
                CommandConfigurationData command_configuration_data = null;
                if (CommandsConfiguration.Data.Commands.ContainsKey(key))
                {
                    command_configuration_data = CommandsConfiguration.Data.Commands[key];
                    if (command_configuration_data == null)
                    {
                        command_configuration_data = new CommandConfigurationData();
                        CommandsConfiguration.Data.Commands[key] = command_configuration_data;
                    }
                }
                else
                {
                    command_configuration_data = new CommandConfigurationData();
                    CommandsConfiguration.Data.Commands.Add(key, command_configuration_data);
                }
                ret = command_configuration_data.Enabled;
            }
            return(ret);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get command required privileges
        /// </summary>
        /// <param name="command">Command</param>
        /// <returns>Command</returns>
        public IReadOnlyDictionary <string, uint> GetRequiredCommandPrivileges(ICommand command)
        {
            Dictionary <string, uint> ret = new Dictionary <string, uint>();

            if (command != null)
            {
                if (CommandsConfiguration != null)
                {
                    string key = command.Name.Trim().ToLower();
                    if (CommandsConfiguration.Data.Commands.ContainsKey(key))
                    {
                        CommandConfigurationData command_configuration = CommandsConfiguration.Data.Commands[key];
                        if (command_configuration != null)
                        {
                            foreach (KeyValuePair <string, uint> privilege in command_configuration.Privileges)
                            {
                                if (ret.ContainsKey(privilege.Key))
                                {
                                    if (ret[privilege.Key] < privilege.Value)
                                    {
                                        ret[privilege.Key] = privilege.Value;
                                    }
                                }
                                else
                                {
                                    ret.Add(privilege.Key, privilege.Value);
                                }
                            }
                        }
                    }
                }
                foreach (KeyValuePair <string, uint> force_required_privilege in command.ForceRequiredPrivileges)
                {
                    if (ret.ContainsKey(force_required_privilege.Key))
                    {
                        if (ret[force_required_privilege.Key] < force_required_privilege.Value)
                        {
                            ret[force_required_privilege.Key] = force_required_privilege.Value;
                        }
                    }
                    else
                    {
                        ret.Add(force_required_privilege.Key, force_required_privilege.Value);
                    }
                }
            }
            return(ret);
        }
Ejemplo n.º 4
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);
        }